473,395 Members | 1,558 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Finding selected item in a combobox

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.

Nov 17 '05 #1
2 10231
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

"farseer" <fa*****@optonline.net> schreef in bericht
news:11*********************@o13g2000cwo.googlegro ups.com...
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.

Nov 17 '05 #2
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)

Nov 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: VM | last post by:
How can I get the selected value of a Windows Listbox? It only has two items (which I added through the Collection property) and everytime I get the selectedIndex, it returns -1. If I use...
9
by: Steffen Laser | last post by:
Hi group, I have a problem that I already have posted to the german C# newsgroup. Since nobody could help me there, I'd like to try it here again: I set the selected item of a combobox like...
4
by: Michael Turner | last post by:
Hi I am having a problem with a combobox the values are generated from a recordset, I need to beable to change the selected item to the value specified in a configuration file, I have stored the...
2
by: Brian Henry | last post by:
say I have a databound combo box, which allows for text entry still (DropDownStype = DropDown in this case). now, If the user doesn't enter manually an item in the data bound list, nothing...
2
by: shumaker | last post by:
I have a combobox that is very much like the one found in the RSS project here: http://msdn.microsoft.com/vstudio/express/visualCSharp/learning/ My projectNameComboBox basically is filled with a...
6
by: Smokey Grindle | last post by:
Say I have a combo box with the following simple object Public class MyObject public ID as integer public Name as string public overrides sub ToString() as string return name end sub end...
2
by: Academia | last post by:
I have a combobox that when it drops down the selected item is not highlighted. I'm guessing there is a property that controls this but can't find one. Is there? Should it always be...
2
by: kurtzky | last post by:
i created a form that should function as follows: i will enter a number in a textbox..then it should query from the database all the records which has that number..these records will have a...
1
by: sbandalli | last post by:
Hello, I have a Datagridview which has a combobox,and 2 textbox, The combobox is bound to a Datasource(Database Sql Server and the table name is Category) ,and Datagridview is not bounded to any...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.