Connecting Tech Pros Worldwide Forums | Help | Site Map

VB Windows APP: ComboBox SelectedValue

Frinavale's Avatar
Site Moderator
 
Join Date: Oct 2006
Location: The Great White North
Posts: 5,137
#1: Nov 4 '08
This question is going to sound a little crazy but.........How do you set the selected item in a ComboBox?

I am populating a ComboBox with a bunch of instances of a custom private class:
Expand|Select|Wrap|Line Numbers
  1. For Each s as Something In MyListOfSomethings
  2.  MyComboBox.Items.Add(New XYZListItem(s.ID,s.description))
  3. Next
  4. MyComboBox.ValueMember = "ID"
  5. MyComboBox.DisplayMember = "Description"
  6.  
Here's the Private Class
Expand|Select|Wrap|Line Numbers
  1. Private Class XYZListItem
  2.   Private _description As String
  3.         Private _id As Integer
  4.         Public Property Description() As String
  5.             Get
  6.                 Return _description
  7.             End Get
  8.             Set(ByVal value As String)
  9.                 _description = value
  10.             End Set
  11.         End Property
  12.         Public Property ID() As Integer
  13.             Get
  14.                 Return _id
  15.             End Get
  16.             Set(ByVal value As Integer)
  17.                 _id = value
  18.             End Set
  19.         End Property
  20.         Public Sub New(ByVal theID As Integer, ByVal theDescription As String)
  21.             _id = theID
  22.             _description = theDescription
  23.         End Sub
  24. End Class
Now the descriptions of the objects are displayed in the ComboBox and when the user selects an Item I'm able to grab the ID from the SelectedItem and do stuff with it.

My problem is that I want to set the selected item through the SelectedValue property and I can't seem to do so:
Expand|Select|Wrap|Line Numbers
  1.       MyComboBox.SelectedValue = value 'Value being a valid ID for an item listed in the ComboBox
  2.  
According to this article:
Quote:
The SelectedValue property can also be used to select an item in the list control by setting it with the value of the item. If no items in the list control contain the specified value, a System..::.ArgumentOutOfRangeException is thrown.
...using the SelectedValue to set the selected item should be possible...so, since what I'm doing doesn't work (I'm getting no exception....the SelectedValue just remains a value Nothing) how do you set the selected item without having to:
Loop through the items in the ComboBox
Find a reference to the one with the ID matching
And then set the ComboBox SelectedItem to the one I found..
(For example...)
Expand|Select|Wrap|Line Numbers
  1. Dim item As XYZListItem
  2. For Each thing As XYZListItem In MyComboBox.Items
  3.   If value = thing.ID Then
  4.     item = thing
  5.     Exit For
  6.   End If
  7. Next
  8. MyComboBox.SelectedItem = item
  9.  

-Frinny

DrBunchman's Avatar
Moderator
 
Join Date: Jan 2008
Location: Winchester, UK
Posts: 930
#2: Nov 5 '08

re: VB Windows APP: ComboBox SelectedValue


Hi Frinny,

Did you mean to post this in .NET? :-)

Dr B
Frinavale's Avatar
Site Moderator
 
Join Date: Oct 2006
Location: The Great White North
Posts: 5,137
#3: Nov 5 '08

re: VB Windows APP: ComboBox SelectedValue


Quote:

Originally Posted by DrBunchman

Hi Frinny,

Did you mean to post this in .NET? :-)

Dr B

>>blush<<

I gotta stop doing that!

(especially since this question has nothing to do with web development!)

-Frinny
Plater's Avatar
Moderator
 
Join Date: Apr 2007
Location: New England
Posts: 7,161
#4: Nov 5 '08

re: VB Windows APP: ComboBox SelectedValue


What you are doing seems like it should work with the SelectedValue, not sure why it doesn't.
Are you able to maybe switch to a Dictionary<IDType,Something> collection and set it as your data source (or something like that instead. Then you can reference the Dictionary with the ID and get the item to set to SelectedItem?

When the combobox is just strings, I use the .Items.Contains("string value I want")

When the combobox uses a datasource (mine are always DataTables, sorry), I had to jump through some hoops.
Expand|Select|Wrap|Line Numbers
  1. //dd is the DataTable that was used as a datasource
  2. DataRow[] dd = dt.Select("[Client Company] ='Philips Healthcare'");
  3. int rowidx = dt.Rows.IndexOf(dd[0]);
  4. cbClientCompany.SelectedItem = dt.DefaultView[rowidx];
  5.  
Only thing I could think of is maybe you can use the SelectedIndex proeprty instead?
Frinavale's Avatar
Site Moderator
 
Join Date: Oct 2006
Location: The Great White North
Posts: 5,137
#5: Nov 5 '08

re: VB Windows APP: ComboBox SelectedValue


Quote:

Originally Posted by Plater

What you are doing seems like it should work with the SelectedValue, not sure why it doesn't.
Are you able to maybe switch to a Dictionary<IDType,Something> collection and set it as your data source (or something like that instead. Then you can reference the Dictionary with the ID and get the item to set to SelectedItem?

I'll look into this...but I'd really prefer not to use a Dictionary.
I'm working on an example application for other developers to reference so that they understand how to use certain functionality. Since I have no idea how much my audience knows about software development I was even reluctant to introduce the very simple private class described above....

I just didn't feel like adding the ID to the string displayed in the combo box...only to have to parse the string later (I find parsing Strings a chore and it can complicate examples)


Quote:

Originally Posted by Plater

When the combobox is just strings, I use the .Items.Contains("string value I want")

When the combobox uses a datasource (mine are always DataTables, sorry), I had to jump through some hoops.

Expand|Select|Wrap|Line Numbers
  1. //dd is the DataTable that was used as a datasource
  2. DataRow[] dd = dt.Select("[Client Company] ='Philips Healthcare'");
  3. int rowidx = dt.Rows.IndexOf(dd[0]);
  4. cbClientCompany.SelectedItem = dt.DefaultView[rowidx];
  5.  

Interesting.....

Quote:

Originally Posted by Plater

Only thing I could think of is maybe you can use the SelectedIndex proeprty instead?

This wont simplify things because I don't know what index the item's ID exists at....I'd still have to loop through the items in the combo box to find the index.

-Frinny
Plater's Avatar
Moderator
 
Join Date: Apr 2007
Location: New England
Posts: 7,161
#6: Nov 5 '08

re: VB Windows APP: ComboBox SelectedValue


Well how do you know which "id" you want to set?
SelectedItem returns the isntance of your class (and all the properties associated with it) for that item.
Somewhere you have to get the ID value you want to set that combobox to, could you not get some other value (like possibly the entire instance of your private class) and use it to select?
Frinavale's Avatar
Site Moderator
 
Join Date: Oct 2006
Location: The Great White North
Posts: 5,137
#7: Nov 5 '08

re: VB Windows APP: ComboBox SelectedValue


Quote:

Originally Posted by Plater

Well how do you know which "id" you want to set?
SelectedItem returns the isntance of your class (and all the properties associated with it) for that item.
Somewhere you have to get the ID value you want to set that combobox to, could you not get some other value (like possibly the entire instance of your private class) and use it to select?

The above code exists in a user control (let's call it the HelperControl).

The HelperControl is used within many other controls that require the functionality the HelperControl offers.


In order to configure the HelperControl to display "default" data ...the parent control calls a function offered by the HelperControl and passes the ID of the item that should be selected.

The parent control has no idea about the item descriptions listed in the ComboBox in the HelperControl...just the ID of the item.
Plater's Avatar
Moderator
 
Join Date: Apr 2007
Location: New England
Posts: 7,161
#8: Nov 5 '08

re: VB Windows APP: ComboBox SelectedValue


I believe the selected value problem comes from using Items.Add() and not a DataSource.
Testing has confirmed this.
I used an array of XYZListItem ( XYZListItem[] xxx = new XYZListItem[6])
And the SelectedValue was getting/setting correctly and changing the combobox just fine
Frinavale's Avatar
Site Moderator
 
Join Date: Oct 2006
Location: The Great White North
Posts: 5,137
#9: Nov 5 '08

re: VB Windows APP: ComboBox SelectedValue


Quote:

Originally Posted by Plater

I believe the selected value problem comes from using Items.Add() and not a DataSource.
Testing has confirmed this.
I used an array of XYZListItem ( XYZListItem[] xxx = new XYZListItem[6])
And the SelectedValue was getting/setting correctly and changing the combobox just fine

You've got to be kidding me!
I'm going to try it now.
Frinavale's Avatar
Site Moderator
 
Join Date: Oct 2006
Location: The Great White North
Posts: 5,137
#10: Nov 5 '08

re: VB Windows APP: ComboBox SelectedValue


It's working.
Plater I'd hug you if you were here :)

Thanks a lot for looking into this for me because I spent a few hours trying to figure out why it wasn't working.

I'm still not sure why the SelectedValue property works when you set the data source property....and why it doesn't work if you add individual items to the ComboBox...I'll have to look into it further later.

I'm just glad that I don't have to loop through all the items.

Thanks again!

-Frinny
Newbie
 
Join Date: Nov 2008
Posts: 1
#11: Nov 13 '08

re: VB Windows APP: ComboBox SelectedValue


Hi Frinny,
How is it possible when you set the dataset to combo box.I am trying to set my selected value and trying to get the text of the combo box.But i couldnt do that.Canyou please help to fix it.
Thanks
Priya

Quote:

Originally Posted by Frinavale

It's working.
Plater I'd hug you if you were here :)

Thanks a lot for looking into this for me because I spent a few hours trying to figure out why it wasn't working.

I'm still not sure why the SelectedValue property works when you set the data source property....and why it doesn't work if you add individual items to the ComboBox...I'll have to look into it further later.

I'm just glad that I don't have to loop through all the items.

Thanks again!

-Frinny

Plater's Avatar
Moderator
 
Join Date: Apr 2007
Location: New England
Posts: 7,161
#12: Nov 13 '08

re: VB Windows APP: ComboBox SelectedValue


If you are using a dataset as the datasource for a combobox, you have to supply 2 values:
.DisplayMember (column name for display)
.ValueMemeber (column name for value)
Reply