combobox.selectedvalue = nothing

alreadyused

Regular
Joined
Jan 13, 2006
Messages
69
i have data listed in a tree, but got to thinking, it would be better in a combobox. What got me thinking about this was a post here:
http://www.xtremedotnettalk.com/showthread.php?t=95541
and:
http://www.xtremedotnettalk.com/showpost.php?p=414245&postcount=10
from: http://www.xtremedotnettalk.com/showthread.php?t=84983

And I looked around quite a bit, there's a ton of posts on here similar to this, but guess I'm messing up some mundane detail. "Mundane detail?!?"

Anyway, I'll save you the life story. I've attached a txt file showing what I'm trying to do. The interesting thing is that no matter how I do it, .SelectedValue = Nothing, .SelectedText = "", but .SelectedItem = the object I cast to it.

And then I can do .SelectedItem.Serial, .SelectedItem.Name, .SelectedItem.ToString... all of which are properties of the object I cast.

So having access to those properties takes care of what I need... but it probably is not correct, and I'm just wondering what I'm doing wrong so that the .SelectedText and .SelectedValue aren't coming up with anything?
 

Attachments

I'm not entirely sure I understood your question, but from what I understand your trying to say that the SelectedText and the SelectedValue properties are always return an empty string/object? Do the values display in the combobox correctly (i.e displaying the DisplayMember)?
 
Cags said:
I'm not entirely sure I understood your question, but from what I understand your trying to say that the SelectedText and the SelectedValue properties are always return an empty string/object? Do the values display in the combobox correctly (i.e displaying the DisplayMember)?
Cags,
what actually got me started on this was your post in the thread listed above! ;) SO in other words thanks for replying!

I think you did understand correctly. No matter what, SelectedText and SelectedValue always = nothing.
But yes, the combo box does display data in the format of JobSerial - JobName i.e. 1234 - Test; that comes from the overridden ToString.

And I can access all of the properties of the object, so if I do cboJob.SelectedItem.Name (with name being a property of the object) I will get a value.
*EDIT* Instead of doing the above, I'm doing this instead b/c I'm guessing it's more proper
Dim objJob as clsJob = cboJob.SelectedItem
and then can access all of the properties through objJob.
*End EDIT*

So it's correctly adding the objects to the combo box, in which case I can access everything I want. I was just curious why my DisplayMember and ValueMember assignments aren't working.

I got to thinking last night, almost, or maybe all, of the examples are using tables or datasets; I'm trying to use properties of objects, and I saw a thread that mentioned that, but maybe I misunderstood?
 
Last edited:
I've had a quick look and you appear to be correct, if you add the classes directly to the combobox the SelectedValue returns nothing. If however you add the items to an ArrayList and set this as the DataSource of the combobox, then calling SelectedValue.ToString() will return the ValueMember of the selected class.

Having said this theres nothing wrong with the method you are currently using as far as I'm concerned (though a professional may differ in opinion).

On a side note SelectedText returns nothing as this refers to the editable text in a combobox, which cannot be selected during a SelectedIndexChanged Event (I think).
 
Cags said:
I've had a quick look and you appear to be correct, if you add the classes directly to the combobox the SelectedValue returns nothing. If however you add the items to an ArrayList and set this as the DataSource of the combobox, then calling SelectedValue.ToString() will return the ValueMember of the selected class.

Having said this theres nothing wrong with the method you are currently using as far as I'm concerned (though a professional may differ in opinion).
Thanks for checking that out!

If you remember from before, I was using a treeview; one of the problems I was running into was tracking the parent object b/c I had Jobs<-Units<-Revisions. Adding the objects to the comboboxes is really cool because whenever a Job is selected, THEN I populate the Unit cbo based on the Job, and whenever a Unit is Selected, same with Rev; so a lot less calls to the objects to look up info and then find the parent!

So in other words, I'm probably just going to stick w/ adding the objects then. It removed a ton of lines of code, and the objects persist throughout the entire program and already have the methods I need, so I really like this way.

On a side note SelectedText returns nothing as this refers to the editable text in a combobox, which cannot be selected during a SelectedIndexChanged Event (I think).
That makes sense; hadn't thought of that. And I actually have the cbos set to not be editable as well.

Thanks again for looking into it and all of your input!
 
Back
Top