none
How do I know which combobox is selected?

    Question

  • I have a windowsform with a datagridview. The datagridview has 3 comboboxes.

    When I select a combobox and change the value there must something happen.

    The problem is that I don't know how I can tell whicht combobox is selected.

    I have the followingcode:

    private void NaardataGridView2_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)

    {

       //here we will add the combo box's selected event changed ComboBox cmbBox;

      if (NaardataGridView2.CurrentCell.ColumnIndex == 0)

      {

            cmbBox = e.Control as ComboBox;

             if (cmbBox == null) { return; }

            cmbBox.SelectedIndexChanged += cmbBox_SelectedIndexChanged;

      }

      }

    void cmbBox_SelectedIndexChanged(object sender, EventArgs e)

    {

    int rowNr = NaardataGridView2.CurrentRow.Index;

    ComboBox cmbBox = (ComboBox)sender;

    NaardataGridView2.Rows[rowNr].Cells[4].Value = Convert.ToString(cmbBox.SelectedIndex);

    }

     

    before it does --> NaardataGridView2.Rows[rowNr].Cells[4].Value = Convert.ToString(cmbBox.SelectedIndex);

    I want to know which combobox is selected, how can I do this?

    This code doesn't work because the name is empty--> if (Convert.ToString(cmbBox.Name) == "ab") {}

    Does some one know what I do wrong?

    Thursday, October 21, 2010 12:27 PM

Answers

  • When you create your Comboxes, add something like this:

    using System.Diagnostics; 
    using System.Windows.Forms; 
    
    ComboBox thing = new ComboBox(); 
    Debug.WriteLine( "Created " + thing.GetHashCode().ToString() ); 
    
    Then, in your event handler(s):

    Debug.WriteLine( "Recevied " + sender.GetHashCode().ToString() ); 
    
    The values you see in the event handler(s) should match [one of] those you see as you create the Controls.  If not, then something's getting in the way and "substituting" your Controls with new ones.


    Regards, Phill W.
    • Marked as answer by Kira Qian Tuesday, November 2, 2010 8:49 AM
    Thursday, October 21, 2010 3:56 PM

All replies

  • You're almost there - the sender argument is [a reference to] the ComboBox that raised the event so you just need to somehow identify each one. 

    How did(?) you create these ComboBoxes?  If you used a Designer, then that should give you an opportunity to assign each one a meaningful name, which you can retrieve this through the sender reference. 

    If they're being added in code (and, let's face it, everything is these days; you just have to find the code), simply assign each a meaningful name as it's created.


    Regards, Phill W.
    Thursday, October 21, 2010 1:05 PM
  • The comboboxes are created by code. I also gave them a name, but that is just the problem the following code doesn't work:

    string whichBox = cmbBox.Name;

     

    This gives me a empty string.

    Thursday, October 21, 2010 1:41 PM
  • Compare the values of GetHashCode() for the ComboBoxes you create and those coming through the sender reference. 

    They should be the same but I suspect your events are being sent some other ComboBoxes. 

     


    Regards, Phill W.
    Thursday, October 21, 2010 2:32 PM
  • I'm still a junior programmer, can you give me some code how to do your solution?
    Thursday, October 21, 2010 3:37 PM
  • When you create your Comboxes, add something like this:

    using System.Diagnostics; 
    using System.Windows.Forms; 
    
    ComboBox thing = new ComboBox(); 
    Debug.WriteLine( "Created " + thing.GetHashCode().ToString() ); 
    
    Then, in your event handler(s):

    Debug.WriteLine( "Recevied " + sender.GetHashCode().ToString() ); 
    
    The values you see in the event handler(s) should match [one of] those you see as you create the Controls.  If not, then something's getting in the way and "substituting" your Controls with new ones.


    Regards, Phill W.
    • Marked as answer by Kira Qian Tuesday, November 2, 2010 8:49 AM
    Thursday, October 21, 2010 3:56 PM