Hello,
I am beginning to write a custom combobox control. Right now it doesn't do
anything special. I created a class that extends ComboBox:
public class TimeSelector : System.Windows.Forms.ComboBox
and in its constructor I call Populate:
public TimeSelector()
{
try
{
if(!DesignMode)
{
Populate();
}
}
catch(Exception ex_)
{
MessageBox.Show(ex_.Message);
}
}
where Populate is as follows:
private void Populate()
{
Items.Clear();
string[] times = new String[48];
DateTime dt = new DateTime(1,1,1,23,30,0,0);
for(int i = 0; i < 48; i++)
{
dt = dt.Add(new TimeSpan(0,30,0));
times[i] = dt.ToString("h:mm tt");
}
DataSource = times;
}
Now, when I start an empty form that contains this control, the combobox
correctly contains all of the items that I populated it with. However,
whenever I do anything to the control while I am looking at the form that
contains it in design mode, it gets messed up.
Specifically, when I go back to look at InitializeComponent in the form's
code, it seems to have added all of the times to the combobox thru a
DataSource and then it manually adds each time to the combobox's Items
collection right after that:
this.ts.DataSource = new string[] {
"12:00 AM",
"12:30 AM",
"1:00 AM",...
this.ts.Items.AddRange(new object[] {
"12:00 AM",
"12:30 AM",
"1:00 AM",...
This causes an error because you can't modify the combox's Items while it is
data bound. And this happens no matter what I do while in design mode (if I
move the control, set Enabled to false, whatever).
Can someone tell me what I am doing wrong?
Thanks,
-Flack