I create a combobox in a class and want to set the selected value for that combobox. But when I do that, the selectedValue stays null and when I try to set the selectedIndex I get a ArgumentOutOfRangeException.
Code:
public Control GenerateList(Question question)
{
// Clear the local givenAnswer collection
_givenAnswer.Clear();
// Get a list with answer possibilities
List<QuestionAnswer> answers = question.GetAnswerSort();
// Get a collection of given answers
Collection<QuestionnaireAnswer> givenAnswers = question.GetGivenAnswers();
_givenAnswer = givenAnswers;
ComboBox cmb = new ComboBox();
cmb.Name = "cmb";
cmb.DisplayMember = "Answer";
cmb.ValueMember = "Id";
cmb.DataSource = answers;
cmb.Dock = DockStyle.Top;
// Check an answer is given to the question
if (givenAnswers != null && givenAnswers.Count > 0)
{
cmb.Tag = givenAnswers[0].AnswerId;
cmb.SelectedValue = givenAnswers[0].AnswerId; // answerId = 55, but SelectedValue stays null
}
cmb.SelectedIndex = 1; // For testting. This will throw a ArgumentOutOfRangeException
cmb.DropDownStyle = ComboBoxStyle.DropDownList;
cmb.SelectedIndexChanged += new EventHandler(cmb_SelectedIndexChanged);
return cmb;
}
I hope someone can explain to me what is happening so I can understand why it isn't working.
Here is a complete little program what illustrates my problem. As you can see it doesn't set the SelectedValue, this stays null
namespace Dynamic_Create_Combo
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
GenerateControls gc = new GenerateControls();
Control c = gc.GenerateCombo();
this.SuspendLayout();
this.Controls.Add(c);
this.ResumeLayout(true);
}
}
public class GenerateControls
{
public Control GenerateCombo()
{
// Create datasource
Collection<Car> cars = new Collection<Car>();
Car c = new Car();
c.Id = 1;
c.Name = "Car one";
cars.Add(c);
Car c1 = new Car();
c1.Id = 2;
c1.Name = "Car two";
cars.Add(c1);
Car c2 = new Car();
c2.Id = 2;
c2.Name = "Car three";
cars.Add(c2);
ComboBox cmb = new ComboBox();
cmb.DropDownStyle = ComboBoxStyle.DropDownList;
cmb.DataSource = cars;
cmb.DisplayMember = "Name";
cmb.ValueMember = "Id";
cmb.SelectedValue = 2;
return cmb;
}
}
public class Car
{
private int _id;
private string _name;
public int Id
{
get { return _id; }
set { _id = value; }
}
public string Name
{
get { return _name; }
set { _name = value; }
}
}
}