none
How to reteieve Tag Value from Combo Box

    Question

  • I have created a combo box as follow. But I could not figure out how can I retrieve Tag values from it.

     

     

    Retreive values

    I am able to retreiev items and Values

    MessageBox.Show("ComboBox--Name: " + objComboBox.Name.ToString() + " ,Item Selected: " + objComboBox.SelectedIndex.ToString() + " ,Value Selected: " + objComboBox.SelectedValue.ToString() );

     

    >>>>Following line does not work????

    MessageBox.Show("ComboBox--Tag: " + objComboBox.Tag.ToString()

     

    Thanks

    DNB1

     

    //////////////////////////////////////////////////////////////////////////////////

     

    ////////////Creating combo box

     

     

    ComboBox cmbBox = new ComboBox();

     

    string stext = "Apples,Banana,Oranges";

    string[] arrWords = stext.Split(splitChars);

    CreateComboBoxItem(cmbBox,"",Convert.ToString(0));

    foreach (string s in arrWords)

    {

    CreateComboBoxItem(cmbBox, s, Convert.ToString(i));

    }

    cmbBox.SelectedIndex = 2;

     

    cmbBox.Name = "ComboBox_" + iFormControlID;

    ......

    ...

    }

     

    //////////////////////////////////////////////////////////////////////////////////

    private void CreateComboBoxItem(ComboBox cmbBox, String sContentItem, string sItemIDTag)

    {

    ComboBoxItem cmbBoxItem1 = new ComboBoxItem();

    cmbBoxItem1.Content = sContentItem;

    cmbBoxItem1.Tag = sItemIDTag;

    cmbBox.Items.Add(cmbBoxItem1);

    }

    Tuesday, May 06, 2008 5:17 PM

Answers

  • The type of ComboBox.SelectedItem property is object. You need to convert it to ComboBoxItem first. Here is an example which shows how to do this.

     

    ((ComboBoxItem)objComboBox.SelectedItem).Tag.ToString()

     

    Best Regards,

    Wei Zhou

    Thursday, May 08, 2008 5:49 AM
    Moderator

All replies

  • Your tag from CreateComboBoxItem() is on the ComboBoxItem, not on the ComboBox itself.  I think you meant...

    Code Snippet

    objComboBox.SelectedItem.Tag.ToString()


    Tuesday, May 06, 2008 5:42 PM
  • I tried what U said..But I am getting following error

    MessageBox.Show(objComboBox.SelectedItem.Tag.ToString() );

     

    Error 6 'object' does not contain a definition for 'Tag' and no extension method 'Tag' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?) 2161 66 

     

    FYI..I am creating this control dynamically


    Thanks

    DNB1

     

     

    Tuesday, May 06, 2008 6:28 PM
  • The type of ComboBox.SelectedItem property is object. You need to convert it to ComboBoxItem first. Here is an example which shows how to do this.

     

    ((ComboBoxItem)objComboBox.SelectedItem).Tag.ToString()

     

    Best Regards,

    Wei Zhou

    Thursday, May 08, 2008 5:49 AM
    Moderator