Very quick question on comboxbox items..

  • Thread starter Thread starter Dan Keeley
  • Start date Start date
D

Dan Keeley

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
 
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
 
* "Dan Keeley said:
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/[email protected]>
 
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
 
no hang on i think i may have it...

just need to figure out what the selected item is, hmm.
 
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!
 
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
 
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)
 
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
 
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
 
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
 
Back
Top