Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / November 2006

Tip: Looking for answers? Try searching our database.

combobox find item by value

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Jason - 16 Nov 2006 16:01 GMT
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.
Rad [Visual C# MVP] - 16 Nov 2006 16:57 GMT
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

Signature

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

> 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.
Jason - 16 Nov 2006 17:05 GMT
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

> Hi Jason,
>
[quoted text clipped - 21 lines]
> > or any work around to getting the index of an object or setting the
> > selecteditem by the value?  Any help is appreciated, thank you.
Morten Wennevik - 17 Nov 2006 07:28 GMT
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.

> Rad,
>
[quoted text clipped - 38 lines]
>> > or any work around to getting the index of an object or setting the
>> > selecteditem by the value?  Any help is appreciated, thank you.

Signature

Happy Coding!
Morten Wennevik [C# MVP]

Jason - 17 Nov 2006 14:40 GMT
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.  :)

> Hi,
>
[quoted text clipped - 48 lines]
> >> > or any work around to getting the index of an object or setting the
> >> > selecteditem by the value?  Any help is appreciated, thank you.
Ciaran O''Donnell - 16 Nov 2006 16:59 GMT
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

> 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.
Jason - 16 Nov 2006 17:18 GMT
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

> 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
[quoted text clipped - 12 lines]
> > or any work around to getting the index of an object or setting the
> > selecteditem by the value?  Any help is appreciated, thank you.
RobinS - 17 Nov 2006 00:40 GMT
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.
----------------------------

> 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
[quoted text clipped - 33 lines]
>> > or any work around to getting the index of an object or setting the
>> > selecteditem by the value?  Any help is appreciated, thank you.
Kevin Yu [MSFT] - 17 Nov 2006 01:56 GMT
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.)
Jason - 17 Nov 2006 14:50 GMT
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

> Hi Jason,
>
[quoted text clipped - 30 lines]
> (This posting is provided "AS IS", with no warranties, and confers no
> rights.)
Kevin Yu [MSFT] - 20 Nov 2006 06:25 GMT
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.)
Jason - 17 Nov 2006 14:56 GMT
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

> Set the text property equal to what you want it to be. If it's in the
> display list, it will select that entry.
[quoted text clipped - 56 lines]
> >> > or any work around to getting the index of an object or setting the
> >> > selecteditem by the value?  Any help is appreciated, thank you.

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.