By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,573 Members | 1,812 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,573 IT Pros & Developers. It's quick & easy.

Very quick question on comboxbox items..

P: n/a
Hi,

As far as i can tell this can't be done, but thought i'd quickly ask, I have
4 labels in my combobox, and i want to add them to the box with a numeric
"value" and then get that value of the currently selected item. I want the
combobox to display the text not the value of course..

Is this possible with the standard combobox??

Thanks,
Dan
Nov 20 '05 #1
Share this Question
Share on Google+
12 Replies


P: n/a
Hi Dan,

Same question at 10:25am today - I guess it's one of those. ;-)

Assign an object to the combobox rather than a single value. Your override
of the object's ToString will provide the text for display, but accessing the
Item in code will get the whole object.

Regards,
Fergus
Nov 20 '05 #2

P: n/a
* "Dan Keeley" <ma********@hotmail.com> scripsit:
As far as i can tell this can't be done, but thought i'd quickly ask, I have
4 labels in my combobox, and i want to add them to the box with a numeric
"value" and then get that value of the currently selected item. I want the
combobox to display the text not the value of course..


Have a look at this discussion:

<http://www.google.de/groups?selm=bme0qq%24loej5%241%40ID-208219.news.uni-berlin.de>

--
Herfried K. Wagner
MVP VB Classic, VB.NET
<http://www.mvps.org/dotnet>
Nov 20 '05 #3

P: n/a
>
Have a look at this discussion:

<http://www.google.de/groups?selm=bme...19.news.uni-be
rlin.de>
--
Herfried K. Wagner
MVP VB Classic, VB.NET
<http://www.mvps.org/dotnet>


Excellent thanks for both of those very quick replies!!

Rgds,
Dan
Nov 20 '05 #4

P: n/a

Have a look at this discussion:

<http://www.google.de/groups?selm=bme...19.news.uni-be
rlin.de>


Ah, ok so it's not as easy as all that, I dont understand how to get the
value back out of the combobox...

I actually have a "Collection" of combobox's and I need to get the value
from the nth list item...

Help?

Thanks!
Dan

Nov 20 '05 #5

P: n/a
no hang on i think i may have it...

just need to figure out what the selected item is, hmm.

"Dan Keeley" <ma********@hotmail.com> wrote in message
news:05****************@newsfep4-winn.server.ntli.net...

Have a look at this discussion:

<http://www.google.de/groups?selm=bme...19.news.uni-be rlin.de>


Ah, ok so it's not as easy as all that, I dont understand how to get the
value back out of the combobox...

I actually have a "Collection" of combobox's and I need to get the value
from the nth list item...

Help?

Thanks!
Dan

Nov 20 '05 #6

P: n/a
Hi Dan,

In the light of this 'Collection' - could you rephrase your question?
Especially, what does 'nth' mean? Are there 'n' items and 'm' comboboxes? Etc,
etc..

Regards,
Fergus
Nov 20 '05 #7

P: n/a
Right ignore my other post i can't make it work!

Ok, I have a collection of combo boxes which i'm looking through. ComboItem
is the object stored in the combo box, which has a Name property and an
OptionValue property

I have this code:

Function GetValue(ByVal index As Integer)
Dim ACombo As ComboBox
ACombo = Me.List.Item(index) ' This gets the actual combo box from the
list
Dim a As Integer = ACombo.SelectedIndex
If a = -1 Then
'TODO Deal with the fact this is "Unselected"
a = 0
End If
Return i.Items.Item(a).OptionValue
End Function

Now it returns the correct selected item, however in the end it always
returns 0

Any ideas? Does that make any sense at all?!! I basically cannot seem to
access the optionValue which is a property of the custom object class which
is in the combobox!
"Fergus Cooney" <fi******@tesco.net> wrote in message
news:OY**************@tk2msftngp13.phx.gbl...
Hi Dan,

In the light of this 'Collection' - could you rephrase your question?
Especially, what does 'nth' mean? Are there 'n' items and 'm' comboboxes? Etc, etc..

Regards,
Fergus

Nov 20 '05 #8

P: n/a
Hi Dan,

Helping is tricky.
Return i.Items.Item(a).OptionValue
Where did 'i' jump up from? It tells me that the posted code isn't the
running code, and that's where the tricky comes in.

Whatever Items(a) gives, it's going to have to be cast into the correct
type so that you can see the OptionValue property.

How about writing a debug routine which will simply Console.WriteLine each
of the Items in the given ComboBox? You can call it with ACombo to determine
whether you can get <any> valid OptionValues out of it. Then worry about
getting the <correct> one.

Regards,
Fergus
Nov 20 '05 #9

P: n/a
Indeed, sorry about that, I mucked around with my values to make it easier
to understand for me as much as anything!

You're right though, I wasnt entering valid options into my list! I now
have this kind of thing(Actual code this time!)

Dim Coption As New ComboItem
Coption.Name = "Unacceptable"
Coption.OptionValue = 100
aComboBox.Items.Add(Coption)

Dim Coption2 As New ComboItem
Coption2.Name = "Major Non-Compliance"
Coption2.OptionValue = 50
aComboBox.Items.Add(Coption2)

Dim Coption3 As New ComboItem
Coption3.Name = "Minor Non-Compliance"
Coption3.OptionValue = 20
aComboBox.Items.Add(Coption3)

Dim Coption4 As New ComboItem
Coption4.Name = "Satisfactory"
Coption4.OptionValue = 9
aComboBox.Items.Add(Coption4)

and now it works! Thanks a lot, i'm certainly beginning to get my head
around all this stuff now and it's kinda clever !

Cheers,
Dan

PS: Is there any other better way i should be doing the above?

Once again thanks for the help. I now have a dynamic database driven form
with soon to have dynamic options in the combo's too :o)
"Fergus Cooney" <fi******@tesco.net> wrote in message
news:eI**************@tk2msftngp13.phx.gbl...
Hi Dan,

Helping is tricky.
Return i.Items.Item(a).OptionValue
Where did 'i' jump up from? It tells me that the posted code isn't the
running code, and that's where the tricky comes in.

Whatever Items(a) gives, it's going to have to be cast into the correct type so that you can see the OptionValue property.

How about writing a debug routine which will simply Console.WriteLine each of the Items in the given ComboBox? You can call it with ACombo to determine whether you can get <any> valid OptionValues out of it. Then worry about
getting the <correct> one.

Regards,
Fergus

Nov 20 '05 #10

P: n/a
Hi Dan,

While that will certainly work for a few items, here's a couple of
suggestions.

With the code that you have, you use Coption once and then discard it.
Then you declare and use Coption2, and so on. You could simply keep using
Coption each time. Coption = New ComboItem.

What I would do, though, is add a constructor to the ComboItem class

Class ComboItem
: : :
Public Sub New (ThisName As String, ThisOptionValue As Integer)
Name = ThisName
OptionValue = ThisOptionValue
End Sub
End Class

Then it can be initialised like so:
aComboBox.Items.Add (New ComboItem ("Unacceptable", 100))
aComboBox.Items.Add (New ComboItem ("Major Non-Comp", 50))
aComboBox.Items.Add (New ComboItem ("Minor Non-Comp", 20))
aComboBox.Items.Add (New ComboItem ("Satisfactory", 9))

Regards,
Fergus
Nov 20 '05 #11

P: n/a

Ah, Excellent thats exactly the sort of thing i should have been thinking
of.

I did try re-using Coption by the way, and it didnt work, I got the last
item i'd added 4 times.. This confused me a lot! strange.

Thanks for all your help tonight, I owe you a beer!

Dan

"Fergus Cooney" <fi******@tesco.net> wrote in message
news:eu**************@TK2MSFTNGP11.phx.gbl...
Hi Dan,

While that will certainly work for a few items, here's a couple of
suggestions.

With the code that you have, you use Coption once and then discard it.
Then you declare and use Coption2, and so on. You could simply keep using
Coption each time. Coption = New ComboItem.

What I would do, though, is add a constructor to the ComboItem class

Class ComboItem
: : :
Public Sub New (ThisName As String, ThisOptionValue As Integer)
Name = ThisName
OptionValue = ThisOptionValue
End Sub
End Class

Then it can be initialised like so:
aComboBox.Items.Add (New ComboItem ("Unacceptable", 100))
aComboBox.Items.Add (New ComboItem ("Major Non-Comp", 50))
aComboBox.Items.Add (New ComboItem ("Minor Non-Comp", 20))
aComboBox.Items.Add (New ComboItem ("Satisfactory", 9))

Regards,
Fergus

Nov 20 '05 #12

P: n/a
Hi Dan,

I caught myself before it was too late, but I almost recommended that very
mistake!!

The idea is that you can reuse Coption the <variable> but you have to give
it a New ComboItem each time.

This central heating makes me thirsty, so thanks for the beer! We're in
the same timezone so I'll see you in the Dog and Crown in half an hour!! ;-)

Regards,
Fergus
Nov 20 '05 #13

This discussion thread is closed

Replies have been disabled for this discussion.