combobox find item by value

G

Guest

Is there any way in a combobox to find the index of an item or set the item
in a combobox by the value? I know there is a FindString, and a
FindStringExact method in the combobox object, but is there anything simmilar
or any work around to getting the index of an object or setting the
selecteditem by the value? Any help is appreciated, thank you.
 
G

Guest

Hi Jason,

what you want to do is this:

//
// first get the value you want to select
// e.g. from the database
//
string myValue = "4";

//
// In the combobox, find this value and then select it
//

comboBox.Items.FindByValue(myValue).Selected=true



Also, you can do the same thing by the combo box item text

comboBox.Items.FindByText(myValue).Selected=true
 
G

Guest

I'm not sure if your talking web or windows as in windows apps, the list
items in a combobox are objects (so they dont necesarily have a value
property), so if the object you put it there overrides equals, you can just
do comboBox1.Items.IndexOf(value)
On the web there is a FindItemByValue on the items collection. This returns
the actual item so if you wanted the index you could then do an index of.

HTH

Ciaran O'Donnell
 
G

Guest

Rad,

What version of the framework are you talking about? I'm using vb.net
also tried your suggestion in c# just to be sure, but FindByValue (nor
FindByText) does not exist in framework 2.0??? Thanks for your input.

Jason
 
G

Guest

Ok, I see, sorry for the confussion, I'm talking about a Windows app, the
combobox has a ValueMember property and a DisplayMember property, so once I
populate the combo box with say all the states, when the user loads a
particular record in which the state is NY, I need to set the selecteditem to
NY, but in the database I'm only going to store the ID for NY, and if I do a
..IndexOf(state), it will not match up because the state object in the
combobox will have name, id, etc., and the object I'm passing it only has the
ID, which will cause the comparison the fail, anyway I can use the id to set
the selected item or get the index?

Jason
 
R

RobinS

Set the text property equal to what you want it to be. If it's in the
display list, it will select that entry.

I added this to Form_Load for one of mine. When SelectedIndex
changes, my combo box loads a separate part of the screen.

I did this:
myComboBox.SelectedValue = 0 'this makes it have none selected
myComboBox.Text = "TextOfThirdEntry"

Then when I look at myComboBox.SelectedValue, it's the index
for the one I put the text in for, and the separate part of the
screen has loaded for the right entry.

Note: If you don't set SelectedValue to 0 before doing this,
it equals Nothing, and could impact your results, depending
on what you're doing.

Is that what you're trying to accomplish?

Robin S.
----------------------------
 
K

Kevin Yu [MSFT]

Hi Jason,

Another way is that if your combobox is binding to a certain object, for
example DataSet, we can search the value in the DataSource and get the
displayed string. Then use FindStringExact to get the index of item in
combobox.

BTW, FindByValue is a method of ListItemCollection class in web form
projects. It is not suitable here.

If anything is unclear, please feel free to let me know.

Kevin Yu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.
Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
M

Morten Wennevik

Hi,

As Kevin mentioned in another reply, FindByValue is used by
ListItemCollection, which in turn is used by the web equivalent of a
ComboBox, the DropDownList. Although strictly speaking a ComboBox is only
found in a windows application and a DropDownList is only found in a
windows application, it is preferred to specify what kind of application
you are working on as the terms are often mixed. Specifying the type of
application will also prevent solutions not applicable to your problem.
 
G

Guest

Yes, my reply to Ciaran, I realized the problem that caused, and yes I am
writing a Windows Application, again sorry for the confusion, and thanks for
the help. :)
 
G

Guest

Kevin, thanks, although I was trying to avoid having to store the collections
of objects in memory since I'm working with many combo boxes, my work around
was to write a custom function to loop through all objects in the combobox
and extracting them then comparing the id with the id of the object I want to
set it to, and thus getting the index from there, I think it may be a bit
ineficient though.


Jason
 
G

Guest

Robin,

No that is not what I'm trying to do, I'm starting off with a combobox
with no item selected, it has a collection of objects, say STATES objects,
with properties NAME, ID, ABBREVIATION, etc. (many properties), then I set
the display member to "Name", and value member to "Id", when loading data for
a record in my db, in the db I store the state ID, thus I want to set the
selected item to whatever object it is that has ID = X, you see...

myComboBox.SelectedIndex = [some function that returns the index of the
object in combo box with ID = x]

OR

myComboBox.SelectedItem= [some function that returns the object in combo box
with ID = x]

OR

myComboBox.SelectedValue = myState.Id

Any of these variations will do, but it appears I'm out of luck, and
there is no such function, except for the "brute force" solution I created,
as posted on my reply to Kevin, thanks for your help though.

Jason
 
K

Kevin Yu [MSFT]

Nice to know that you have a workaround. If you have any other questions,
please feel free to post them in the community.

Kevin Yu
Microsoft Online Community Support
==================================================

(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top