How to reteieve Tag Value from Combo Box
-
Tuesday, May 06, 2008 5:17 PM
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);
}
All Replies
-
Tuesday, May 06, 2008 5:42 PMYour tag from CreateComboBoxItem() is on the ComboBoxItem, not on the ComboBox itself. I think you meant...
Code SnippetobjComboBox.SelectedItem.Tag.ToString()
-
Tuesday, May 06, 2008 6:28 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
-
Thursday, May 08, 2008 5:49 AMModerator
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

