473,545 Members | 2,095 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

VB Windows APP: ComboBox SelectedValue

Frinavale
9,735 Recognized Expert Moderator Expert
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..::.Argu mentOutOfRangeE xception 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....th e 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 3283
DrBunchman
979 Recognized Expert Contributor
Hi Frinny,

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

Dr B
Nov 5 '08 #2
Frinavale
9,735 Recognized Expert Moderator Expert
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 Recognized Expert Expert
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<IDTy pe,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 Recognized Expert Moderator Expert
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<IDTy pe,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 Recognized Expert Expert
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 Recognized Expert Moderator Expert
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 Recognized Expert Expert
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 Recognized Expert Moderator Expert
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 Recognized Expert Moderator Expert
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

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

Similar topics

10
2386
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) ComboBox.SelectedValue = db_value; If the db_value was not included in the ComboBox value list the ComboBox.SelectedIndex used to return -1, Now the very same code is
7
23521
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 for a match between cVal and the selectedValue then stop when I do have a match the obvious problem is that each iteration fires the...
6
40889
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: Sub SetLevel(Level as Short) cboLevel.SelectedValue = Level End Sub I try this assignment, and the SelectedValue property remains Nothing. I...
30
4530
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 corresponding field in the lookup table. In my data table we store the ID in what I will call the 'key' field. == Description of the desired...
7
4554
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 (called "Data") with two columns "Id" and "Description". Id contains a code and description contains the description that is displayed in the...
5
3174
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 the value and displaymember properties. This works fine. My combobox contains the correct data. However I'm also binding my SelectedValue...
2
7125
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 filled in automatically by setting its .Text property equal to the .SelectedValue of the combobox. When the user enters an ID in the textbox the...
1
4084
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
2280
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: txtMyTextBox.Text = cboMyBox.SelectedText; //is this right? But it never shows up. I tried SelectedValue, and SelectedValue.
0
7464
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7396
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7656
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7805
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7751
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
4943
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3440
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1874
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
700
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.