Finding selected item in a combobox

  • Thread starter Thread starter farseer
  • Start date Start date
F

farseer

Hi,
I have a combobox who's data source i set to an array of objects (call
it MyObject). these objects have get properties: key, value, descr. i
set ValueMember to "key", DisplayMember to "descr", and datasource to
the array of MyObjects.

How can i set the selected value of that combobox based on the
ValueMember OR how can i find the index of an item in the combobox BY
ValueMember?

here's an example. in the example below, the first parameter is
returned by get property "key", the second by "value", the third by
"descr".

MyObject[] arr = new MyObject[]
{
new MyObject( 1111, 100, "myobj1" ),
new MyObject( 2222, 200, "myobj2" ),
new MyObject( 3333, 300, "myobj3" ),
};

cmbBox.DataSoruce = arr;
cmbBox.ValueMember = "key";
cmbBox.DisplayMember = "descr";

Now at this point i have a cmbBox that has shows in it's dropdown
"myobj1","myobj2","myobj3". Now, i'd like to set the selected item
based on the ValueMember. for instance, i read 2222 from a file, so
i'd like to use this value to set the cmbBox selected index to 2...but
to do this, i need to know how to find the index of items by
valuemember.
 
Farseer,

Correct me if I miss something, however has the the selectedindex of your
combobox not to be the same as the index of your array?

Cor
 
I may have confused you. what i want to do is find an item in combobox
based on the ValueMember property and set the selectedIndex to the
index of that item.
THe only way i can do it now is looping and comparing (either the
combobox's item collection or my array). I was hoping there was a more
efficient way such as the FindExact method of the combo or using
SendMessage with CB_FINDSTRING (which does not work for the ValueMember
i believe)
 
Back
Top