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. Creating Events with Dynamically created ComboBox(es)

Creating Events with Dynamically created ComboBox(es)

Scheduled Pinned Locked Moved C#
question
8 Posts 6 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.
  • K Offline
    K Offline
    KakitaIppatsu
    wrote on last edited by
    #1

    I have created X number of ComboBoxes (PartNo) dynamically, that depending on the number selected in it, it will pull/populate some textboxes with Part Information. All boxes have been given a Key value of cboBox1, cboBox2, etc. How can I make an event for these comboboxes?

    private void cboBox1_SelectedIndexChanged(object sender, EventArgs e)

    Not sure if it matters, but the comboboxes are all part of a panel (pn), so maybe it would be?

    private void pn.cboBox1_SelectedIndexChanged(object sender, EventArgs e)

    I hate not knowing what I don't know.

    B B N F 4 Replies Last reply
    0
    • K KakitaIppatsu

      I have created X number of ComboBoxes (PartNo) dynamically, that depending on the number selected in it, it will pull/populate some textboxes with Part Information. All boxes have been given a Key value of cboBox1, cboBox2, etc. How can I make an event for these comboboxes?

      private void cboBox1_SelectedIndexChanged(object sender, EventArgs e)

      Not sure if it matters, but the comboboxes are all part of a panel (pn), so maybe it would be?

      private void pn.cboBox1_SelectedIndexChanged(object sender, EventArgs e)

      I hate not knowing what I don't know.

      B Offline
      B Offline
      Brisingr Aerowing
      wrote on last edited by
      #2

      Get a reference to the combo box and use, e.g.

      SomeComboBox.SelectedIndexChanged += SomComboBox_SelectedIndexChanged;

      Where SomeComboBox is the name of your combo box reference, e.g. cboBox1.

      What do you get when you cross a joke with a rhetorical question? The metaphorical solid rear-end expulsions have impacted the metaphorical motorized bladed rotating air movement mechanism. Do questions with multiple question marks annoy you???

      1 Reply Last reply
      0
      • K KakitaIppatsu

        I have created X number of ComboBoxes (PartNo) dynamically, that depending on the number selected in it, it will pull/populate some textboxes with Part Information. All boxes have been given a Key value of cboBox1, cboBox2, etc. How can I make an event for these comboboxes?

        private void cboBox1_SelectedIndexChanged(object sender, EventArgs e)

        Not sure if it matters, but the comboboxes are all part of a panel (pn), so maybe it would be?

        private void pn.cboBox1_SelectedIndexChanged(object sender, EventArgs e)

        I hate not knowing what I don't know.

        B Offline
        B Offline
        BillWoodruff
        wrote on last edited by
        #3

        I suggest that in the code that creates the ComboBoxes at run-time, you wire-up the same 'SelectedIndexChanged EventHandler to each new ComboBox:

        // assume you're inside a loop of some kind creating the ComboBoxes

        cmbx = new ComboBox();
        cmbx.SelectedIndexChanged += CmBoxIndexChanged;

        // outside the loop

        // In the EventHandler, get which ComboBox called it, and take action:

        private void CmBoxIndexChanged(object sender, EventArgs e)
        {
        // probably unnecessary; an example of "defensive" programming
        if (sender == null) throw new ArgumentNullException("Error in ComboBox SelectedIndexChanged");

        ComboBox caller = sender as ComboBox;
        
        switch (caller.Name)
        {
            case "ComboBox0":
                // take action for this ComboBox 
                break;
        
            case "ComboBox1":
                // take action for this ComboBox 
                break;
        
            // and so on ...
        }
        

        }

        «I want to stay as close to the edge as I can without going over. Out on the edge you see all kinds of things you can't see from the center» Kurt Vonnegut.

        K 1 Reply Last reply
        0
        • K KakitaIppatsu

          I have created X number of ComboBoxes (PartNo) dynamically, that depending on the number selected in it, it will pull/populate some textboxes with Part Information. All boxes have been given a Key value of cboBox1, cboBox2, etc. How can I make an event for these comboboxes?

          private void cboBox1_SelectedIndexChanged(object sender, EventArgs e)

          Not sure if it matters, but the comboboxes are all part of a panel (pn), so maybe it would be?

          private void pn.cboBox1_SelectedIndexChanged(object sender, EventArgs e)

          I hate not knowing what I don't know.

          N Offline
          N Offline
          NeillJam
          wrote on last edited by
          #4

          Hi all, If you know all the ComboBoxes are inside the panel, why not just iterate of the Panel's ComboBox controls?

          foreach (ComboBox cBox in panel1.Controls)
          {
          cBox.SelectedIndexChanged += cBox_SelectedIndexChanged;
          }

          Then when any of the ComboBox's SelectedIndexChanged events fire, you capture which one it is and do what needs to be done. eg.

          void cBox_SelectedIndexChanged(object sender, EventArgs e)
          {
          var currentComboBox = sender as ComboBox;

          if (currentComboBox == null)
                return;
          
          MessageBox.Show(currentComboBox.SelectedItem.ToString());
          

          }

          Neill

          1 Reply Last reply
          0
          • K KakitaIppatsu

            I have created X number of ComboBoxes (PartNo) dynamically, that depending on the number selected in it, it will pull/populate some textboxes with Part Information. All boxes have been given a Key value of cboBox1, cboBox2, etc. How can I make an event for these comboboxes?

            private void cboBox1_SelectedIndexChanged(object sender, EventArgs e)

            Not sure if it matters, but the comboboxes are all part of a panel (pn), so maybe it would be?

            private void pn.cboBox1_SelectedIndexChanged(object sender, EventArgs e)

            I hate not knowing what I don't know.

            F Offline
            F Offline
            Foothill
            wrote on last edited by
            #5

            You could also consider using anonymous functions and lambda expressions if the function that is dynamically creating the combo box is also aware of what the drop down needs to do when updated.

            public void AddComboBox(ComboBox cBox, Action callback)
            {
            // your implementation ...

            // and a callback taking the index value or executing an update function
            cBox.SelectedIndexChanged += (s,e) => { callback(cBox.SelectedIndex); };
            }

            P 1 Reply Last reply
            0
            • F Foothill

              You could also consider using anonymous functions and lambda expressions if the function that is dynamically creating the combo box is also aware of what the drop down needs to do when updated.

              public void AddComboBox(ComboBox cBox, Action callback)
              {
              // your implementation ...

              // and a callback taking the index value or executing an update function
              cBox.SelectedIndexChanged += (s,e) => { callback(cBox.SelectedIndex); };
              }

              P Offline
              P Offline
              Pete OHanlon
              wrote on last edited by
              #6

              The downside of this approach is that you've created a non-disposable event handler with that syntax.

              F 1 Reply Last reply
              0
              • P Pete OHanlon

                The downside of this approach is that you've created a non-disposable event handler with that syntax.

                F Offline
                F Offline
                Foothill
                wrote on last edited by
                #7

                Yes, you are correct that it would leak resources. If this were a long running application than that would be a problem otherwise the flexibility gained can potentially outweight a few kb of in memory comboboxes not reclaimed by the garbage collector. Another approach would to have an IDisposable class derive from combobox and reclaim the handler in Dispose() when the dynamically created boxes are deleted from their container.

                1 Reply Last reply
                0
                • B BillWoodruff

                  I suggest that in the code that creates the ComboBoxes at run-time, you wire-up the same 'SelectedIndexChanged EventHandler to each new ComboBox:

                  // assume you're inside a loop of some kind creating the ComboBoxes

                  cmbx = new ComboBox();
                  cmbx.SelectedIndexChanged += CmBoxIndexChanged;

                  // outside the loop

                  // In the EventHandler, get which ComboBox called it, and take action:

                  private void CmBoxIndexChanged(object sender, EventArgs e)
                  {
                  // probably unnecessary; an example of "defensive" programming
                  if (sender == null) throw new ArgumentNullException("Error in ComboBox SelectedIndexChanged");

                  ComboBox caller = sender as ComboBox;
                  
                  switch (caller.Name)
                  {
                      case "ComboBox0":
                          // take action for this ComboBox 
                          break;
                  
                      case "ComboBox1":
                          // take action for this ComboBox 
                          break;
                  
                      // and so on ...
                  }
                  

                  }

                  «I want to stay as close to the edge as I can without going over. Out on the edge you see all kinds of things you can't see from the center» Kurt Vonnegut.

                  K Offline
                  K Offline
                  KakitaIppatsu
                  wrote on last edited by
                  #8

                  Sorry to not respond earlier, but your solution works perfectly. Thanks all for your help!:thumbsup:

                  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