Items.Clear() not working in ComboBox control???
-
OK. So I'm trying to inherit from the ComboBox control (desktop) to make a custom time picker based on some unknown interval of time units. Every time the Increment (meaning interval) property is updated, therefore, the list of items needs to be repopulated. The Increment property can also be set using the designer. My item population code is set in a private method:
/// /// Repopulates the combo box based on the current military and increment settings.
///
private void RepopulateChoices()
{
Items.Clear();
//while (Items.Count > 0)
// Items.RemoveAt(0);
int i = 0;
while (i < 1440)
{
DateTime dat = new DateTime(1, 1, 1, i / 60, i % 60, 0);
if (ShowMilitary)
Items.Add(dat.ToString("HH:mm"));
else
Items.Add(dat.ToString("h:mm tt"));
i += Increment;
}
}You can see the very first thing I do is clear the existing items from the control. I then iterate through 1440 minutes in a day, incrementing in "Increment" number of minutes. With a default Increment of 30, there should be 48 items in the final list. However, I always see more than 48, and the number increases for every nested container. I.e., after "11:30 PM" comes another entry of "12:00 AM" and the cycle starts all over again. dropped directly on a form: 96 items (2 days' worth) dropped into a panel on a form: 192 items (4 days' worth) dropped into a panel in a TableLayout container on a form: 284 items (6 days' worth) WHAT THE HECK IS GOING ON HERE? I have ruled out accidental population routines in the primary app because I can duplicate the behavior dropping this control onto a form in the app where no instance of the control already exists. The RepopulateChoices() method is the ONLY place where I am populating items. Since that method starts with an Items.Clear() instruction, how on earth can I be getting massive quantities of duplicate entries???
-
OK. So I'm trying to inherit from the ComboBox control (desktop) to make a custom time picker based on some unknown interval of time units. Every time the Increment (meaning interval) property is updated, therefore, the list of items needs to be repopulated. The Increment property can also be set using the designer. My item population code is set in a private method:
/// /// Repopulates the combo box based on the current military and increment settings.
///
private void RepopulateChoices()
{
Items.Clear();
//while (Items.Count > 0)
// Items.RemoveAt(0);
int i = 0;
while (i < 1440)
{
DateTime dat = new DateTime(1, 1, 1, i / 60, i % 60, 0);
if (ShowMilitary)
Items.Add(dat.ToString("HH:mm"));
else
Items.Add(dat.ToString("h:mm tt"));
i += Increment;
}
}You can see the very first thing I do is clear the existing items from the control. I then iterate through 1440 minutes in a day, incrementing in "Increment" number of minutes. With a default Increment of 30, there should be 48 items in the final list. However, I always see more than 48, and the number increases for every nested container. I.e., after "11:30 PM" comes another entry of "12:00 AM" and the cycle starts all over again. dropped directly on a form: 96 items (2 days' worth) dropped into a panel on a form: 192 items (4 days' worth) dropped into a panel in a TableLayout container on a form: 284 items (6 days' worth) WHAT THE HECK IS GOING ON HERE? I have ruled out accidental population routines in the primary app because I can duplicate the behavior dropping this control onto a form in the app where no instance of the control already exists. The RepopulateChoices() method is the ONLY place where I am populating items. Since that method starts with an Items.Clear() instruction, how on earth can I be getting massive quantities of duplicate entries???
You example works on my side: Try this with TimeSpan, this may be due to local settings (culture) int Elapse = 30; comboBox1.Items.Clear(); int i = 0; DateTime convertedDate = DateTime.SpecifyKind(DateTime.Parse("00:00:00"), DateTimeKind.Local); while (i < 1440) { convertedDate = convertedDate + new TimeSpan(0, Elapse, 0); if (ShowMilitary) comboBox1.Items.Add(convertedDate.ToString("HH:mm")); else comboBox1.Items.Add(convertedDate.ToString("h:mm tt")); i += Elapse; }
-
OK. So I'm trying to inherit from the ComboBox control (desktop) to make a custom time picker based on some unknown interval of time units. Every time the Increment (meaning interval) property is updated, therefore, the list of items needs to be repopulated. The Increment property can also be set using the designer. My item population code is set in a private method:
/// /// Repopulates the combo box based on the current military and increment settings.
///
private void RepopulateChoices()
{
Items.Clear();
//while (Items.Count > 0)
// Items.RemoveAt(0);
int i = 0;
while (i < 1440)
{
DateTime dat = new DateTime(1, 1, 1, i / 60, i % 60, 0);
if (ShowMilitary)
Items.Add(dat.ToString("HH:mm"));
else
Items.Add(dat.ToString("h:mm tt"));
i += Increment;
}
}You can see the very first thing I do is clear the existing items from the control. I then iterate through 1440 minutes in a day, incrementing in "Increment" number of minutes. With a default Increment of 30, there should be 48 items in the final list. However, I always see more than 48, and the number increases for every nested container. I.e., after "11:30 PM" comes another entry of "12:00 AM" and the cycle starts all over again. dropped directly on a form: 96 items (2 days' worth) dropped into a panel on a form: 192 items (4 days' worth) dropped into a panel in a TableLayout container on a form: 284 items (6 days' worth) WHAT THE HECK IS GOING ON HERE? I have ruled out accidental population routines in the primary app because I can duplicate the behavior dropping this control onto a form in the app where no instance of the control already exists. The RepopulateChoices() method is the ONLY place where I am populating items. Since that method starts with an Items.Clear() instruction, how on earth can I be getting massive quantities of duplicate entries???
-
You example works on my side: Try this with TimeSpan, this may be due to local settings (culture) int Elapse = 30; comboBox1.Items.Clear(); int i = 0; DateTime convertedDate = DateTime.SpecifyKind(DateTime.Parse("00:00:00"), DateTimeKind.Local); while (i < 1440) { convertedDate = convertedDate + new TimeSpan(0, Elapse, 0); if (ShowMilitary) comboBox1.Items.Add(convertedDate.ToString("HH:mm")); else comboBox1.Items.Add(convertedDate.ToString("h:mm tt")); i += Elapse; }
Not sure why that would make a difference. I also noticed a syntax difference between your code and my code:
Migounette wrote:
comboBox1.Items.Clear();
I did not use a designer to make a custom control. I inherited directly from ComboBox. I call Items.Clear() from RepopulateChoices(), which is a private method in the inherited class. All of your code starting with "comboBox1..." is not at all how I'm doing things. I'm going to make another post to clarify as there seems to be a tremendous amount of confusion over this from all respondants.