473,321 Members | 1,667 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,321 software developers and data experts.

VB Windows APP: ComboBox SelectedValue

Frinavale
9,735 Expert Mod 8TB
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:
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
Nov 4 '08 #1
11 3256
DrBunchman
979 Expert 512MB
Hi Frinny,

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

Dr B
Nov 5 '08 #2
Frinavale
9,735 Expert Mod 8TB
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
Nov 5 '08 #3
Plater
7,872 Expert 4TB
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?
Nov 5 '08 #4
Frinavale
9,735 Expert Mod 8TB
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)


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

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
Nov 5 '08 #5
Plater
7,872 Expert 4TB
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?
Nov 5 '08 #6
Frinavale
9,735 Expert Mod 8TB
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.
Nov 5 '08 #7
Plater
7,872 Expert 4TB
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
Nov 5 '08 #8
Frinavale
9,735 Expert Mod 8TB
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.
Nov 5 '08 #9
Frinavale
9,735 Expert Mod 8TB
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
Nov 5 '08 #10
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

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
Nov 13 '08 #11
Plater
7,872 Expert 4TB
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)
Nov 13 '08 #12

Sign in to post your reply or Sign up for a free account.

Similar topics

10
by: BBFrost | last post by:
We just recently moved one of our major c# apps from VS Net 2002 to VS Net 2003. At first things were looking ok, now problems are starting to appear. So far ... (1) ...
7
by: NCrum | last post by:
I want to set the Default value of a Combobox for any changeable record and have got this working but it is totaly unsatisfactory see the code below I loop through the items in the Combo looking...
6
by: Mike Caputo | last post by:
Shouldn't I be able to set the selected item of a combobox with the SelectedValue property? The ComboBox has four items, the values are 1 through 4. Here's an example of what I'm trying to do: ...
30
by: dbuchanan | last post by:
ComboBox databindng Problem == How the ComboBox is setup and used: My comboBox is populated by a lookup table. The ValueMember is the lookup table's Id and the DisplayMember is the text from a...
7
by: Simon Verona | last post by:
I posted this in dotnet.languages.vb.controls but thought I'd post here as well.. I have a combobox that is bound to a dataview generated from a dataset. The dataset has a single table...
5
by: Peter M. | last post by:
I'm struggling with combobox databinding with something I consider a bug... I'm binding my combobox to an array of structs. The struct exposes two public properties, ID and Name, to be used as...
2
by: DesCF | last post by:
I have a textbox and a combobox on a toolstrip. The user enters either an ID in the textbox or selects a name from the combobox. When the user selects a name from the combobox the textbox is...
1
by: Aleksey Timonin | last post by:
Hi guys, I have a comboBox binded to DataTable: comboBox.DataSource = MyDataTable; comboBox.ValueMember = "id"; comboBox.DisplayMember = "id";
5
by: jmDesktop | last post by:
I know this should be straight forward, but I have not gotten it yet. I have a combobox and a textbox. I want to display what is selected in the combobox in the textbox. I have: ...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.