Home All Groups Group Topic Archive Search About

combobox find item by value

Author
16 Nov 2006 4:01 PM
Jason
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.

Author
16 Nov 2006 4:57 PM
Rad [Visual C# MVP]
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

--


Bits. Bytes.
http://bytes.thinkersroom.com
------------------------------


Show quote
"Jason" wrote:

> 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.
Author
16 Nov 2006 5:05 PM
Jason
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

Show quote
"Rad [Visual C# MVP]" wrote:

> 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
>
> --
>
>
> Bits. Bytes.
> http://bytes.thinkersroom.com
> ------------------------------
>
>
> "Jason" wrote:
>
> > 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.
Author
17 Nov 2006 7:28 AM
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 on=
ly  =

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.

On Thu, 16 Nov 2006 18:05:02 +0100, Jason  =

<Ja***@discussions.microsoft.com> wrote:

Show quote
> 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
>
> "Rad [Visual C# MVP]" wrote:
>
>> Hi Jason,
>>
>> what you want to do is this:
>>
>> //
>> // first get the value you want to select
>> // e.g. from the database
>> //
>> string myValue =3D "4";
>>
>> //
>> // In the combobox, find this value and then select it
>> //
>>
>> comboBox.Items.FindByValue(myValue).Selected=3Dtrue
>>
>>
>>
>> Also, you can do the same thing by the combo box item text
>>
>> comboBox.Items.FindByText(myValue).Selected=3Dtrue
>>
>> --
>>
>>
>> Bits. Bytes.
>> http://bytes.thinkersroom.com
>> ------------------------------
>>
>>
>> "Jason" wrote:
>>
>> > 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 anythin=
g  =

>> 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.



-- =

Happy Coding!
Morten Wennevik [C# MVP]
Author
17 Nov 2006 2:40 PM
Jason
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.  :)

Show quote
"Morten Wennevik" wrote:

> 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.
>
> On Thu, 16 Nov 2006 18:05:02 +0100, Jason 
> <Ja***@discussions.microsoft.com> wrote:
>
> > 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
> >
> > "Rad [Visual C# MVP]" wrote:
> >
> >> 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
> >>
> >> --
> >>
> >>
> >> Bits. Bytes.
> >> http://bytes.thinkersroom.com
> >> ------------------------------
> >>
> >>
> >> "Jason" wrote:
> >>
> >> > 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.
>
>
>
> --
> Happy Coding!
> Morten Wennevik [C# MVP]
>
Author
16 Nov 2006 4:59 PM
Ciaran O''Donnell
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

Show quote
"Jason" wrote:

> 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.
Author
16 Nov 2006 5:18 PM
Jason
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

Show quote
"Ciaran O''Donnell" wrote:

> 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
>
> "Jason" wrote:
>
> > 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.
Author
17 Nov 2006 12:40 AM
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.
----------------------------

Show quote
"Jason" <Ja***@discussions.microsoft.com> wrote in message
news:9E14EF6B-5C5A-41C3-877C-052D872BEDEA@microsoft.com...
> 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
>
> "Ciaran O''Donnell" wrote:
>
>> 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
>>
>> "Jason" wrote:
>>
>> > 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.
Author
17 Nov 2006 1:56 AM
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.)
Author
17 Nov 2006 2:50 PM
Jason
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

Show quote
"Kevin Yu [MSFT]" wrote:

> 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.)
>
>
Author
20 Nov 2006 6:25 AM
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.)
Author
17 Nov 2006 2:56 PM
Jason
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

Show quote
"RobinS" wrote:

> 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.
> ----------------------------
>
> "Jason" <Ja***@discussions.microsoft.com> wrote in message
> news:9E14EF6B-5C5A-41C3-877C-052D872BEDEA@microsoft.com...
> > 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
> >
> > "Ciaran O''Donnell" wrote:
> >
> >> 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
> >>
> >> "Jason" wrote:
> >>
> >> > 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.
>
>
>

AddThis Social Bookmark Button