combobox item values?
-
i am currently doing something like the following to a combobox:
(for int i = 0; i < 10; i++)
{
MyObject objData = MyData.GetAt(i);
this.cbMyComboBox.Items.Add(objData.strWord);
}which adds the text to the combobox fine.. but what i really wanna do is set a data value for each item i add.. there are 'tag' values for tree controls, but i dont see anything like this for the combobox.. and if i just add the object then it displays the object type because it doesnt know what text to display.. im sure this is a common problem, just couldn't find the answer in the message boards.. thanks in advance! still a newb.. cut me some slack :P -dz
-
i am currently doing something like the following to a combobox:
(for int i = 0; i < 10; i++)
{
MyObject objData = MyData.GetAt(i);
this.cbMyComboBox.Items.Add(objData.strWord);
}which adds the text to the combobox fine.. but what i really wanna do is set a data value for each item i add.. there are 'tag' values for tree controls, but i dont see anything like this for the combobox.. and if i just add the object then it displays the object type because it doesnt know what text to display.. im sure this is a common problem, just couldn't find the answer in the message boards.. thanks in advance! still a newb.. cut me some slack :P -dz
well one thing you could do is override the tostring method, and throw your own variable in there that would add some meaningful text, have you tried that? Ryan
-
well one thing you could do is override the tostring method, and throw your own variable in there that would add some meaningful text, have you tried that? Ryan
that would require me writing a class derived from combobox that was familiar with class i was adding to the combobox.. so it would work, but only for one type of class.. i dont understand why there is no tag property for the items.. c++ would be like:
for (int i = 0; i < MySet.GetSize(); i++)
{
MyObject* pObj = MySet.GetAt(i);
int nPos = MyComboBox.Add(pObj->m_strText);
MyComboBox.SetDataPtr(nPos, pObj);
}that way when i handled the selection changing i could have a pointer instantly to the item it was changed to.. ah well.. still a newb.. cut me some slack :P -dz
-
that would require me writing a class derived from combobox that was familiar with class i was adding to the combobox.. so it would work, but only for one type of class.. i dont understand why there is no tag property for the items.. c++ would be like:
for (int i = 0; i < MySet.GetSize(); i++)
{
MyObject* pObj = MySet.GetAt(i);
int nPos = MyComboBox.Add(pObj->m_strText);
MyComboBox.SetDataPtr(nPos, pObj);
}that way when i handled the selection changing i could have a pointer instantly to the item it was changed to.. ah well.. still a newb.. cut me some slack :P -dz
if i understand correctly, your trying to get text from objects that you add into the combo box. All objects in c# support the tostring method, it is a method of the base class, so you wouldnt have to do any extra inheritance.
-
if i understand correctly, your trying to get text from objects that you add into the combo box. All objects in c# support the tostring method, it is a method of the base class, so you wouldnt have to do any extra inheritance.
-
if i understand correctly, your trying to get text from objects that you add into the combo box. All objects in c# support the tostring method, it is a method of the base class, so you wouldnt have to do any extra inheritance.
well, i thought this would work, but it doesnt..
this.cbCountyDefault.BeginUpdate();
for (int i = 0; i < m_saCountyRecords.Count; i++)
{
GASTCountyRecord objRecord = (GASTCountyRecord)m_saCountyRecords[i];
this.cbCountyDefault.Items.Add(objRecord);
}this.cbCountyDefault.SelectedIndex = 0;
this.cbCountyDefault.EndUpdate();MessageBox.Show(cbCountyDefault.SelectedItem.ToString());
MessageBox.Show(((GASTCountyRecord)this.cbCountyDefault.SelectedItem).ToString());the first messagebox shows the objects name, and the one where i cast it shows the corect name.. the listing in the combobox lists the class name because it populates this by calling
Object.ToString()
not by casting it to my object type then callingToString()
.. any other suggestions? still a newb.. cut me some slack :P -dz -
well, i thought this would work, but it doesnt..
this.cbCountyDefault.BeginUpdate();
for (int i = 0; i < m_saCountyRecords.Count; i++)
{
GASTCountyRecord objRecord = (GASTCountyRecord)m_saCountyRecords[i];
this.cbCountyDefault.Items.Add(objRecord);
}this.cbCountyDefault.SelectedIndex = 0;
this.cbCountyDefault.EndUpdate();MessageBox.Show(cbCountyDefault.SelectedItem.ToString());
MessageBox.Show(((GASTCountyRecord)this.cbCountyDefault.SelectedItem).ToString());the first messagebox shows the objects name, and the one where i cast it shows the corect name.. the listing in the combobox lists the class name because it populates this by calling
Object.ToString()
not by casting it to my object type then callingToString()
.. any other suggestions? still a newb.. cut me some slack :P -dzhrm.. it was how i was doing it.. ill put this here for future reference how not to do it:
public class GASTCountyRecord : object { public string m\_strCode; public string m\_strName; public GASTCountyRecord(string strCode, string strName) { m\_strCode = strCode; m\_strName = strName; } public string ToString() { return m\_strName; } }
how to do it:
public class GASTCountyRecord : object { public string m\_strCode; public string m\_strName; public GASTCountyRecord(string strCode, string strName) { m\_strCode = strCode; m\_strName = strName; } public **override** string ToString() { return m\_strName; } }
thanks for all your help.. i think the way they have you implement this is a bit wack tho still a newb.. cut me some slack :P -dz