Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Items.Clear() not working in ComboBox control???

Items.Clear() not working in ComboBox control???

Scheduled Pinned Locked Moved C#
dockerquestion
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • X Offline
    X Offline
    Xpnctoc
    wrote on last edited by
    #1

    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???

    M D 2 Replies Last reply
    0
    • X Xpnctoc

      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???

      M Offline
      M Offline
      Migounette
      wrote on last edited by
      #2

      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; }

      X 1 Reply Last reply
      0
      • X Xpnctoc

        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???

        D Offline
        D Offline
        Dan Mos
        wrote on last edited by
        #3

        Xpnctoc wrote:

        private void RepopulateChoices() { Items.Clear();

        What is Items??? It should be something like

        this.myComboCtrl1.Items.Clear();

        or without the this. Who calls the RepopulateChoises method?? Elaborate.

        1 Reply Last reply
        0
        • M Migounette

          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; }

          X Offline
          X Offline
          Xpnctoc
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups