tags:

views:

509

answers:

4

I can't seem to find an easy to use, .net native way to get Comboboxes on .net winforms to display one value and return another based on the selection without creating my own helper class, with the knowledge that winforms is going to display the ToString method on the object that you put in it.

This is how I'm doing it now, very generically. First, create the helper class.

 Public Class ListItem
     Public Value As Object
     Public DisplayString As String

     Public Sub New(ByVal NewValue As Object, ByVal NewDisplayString As String)
         Value = NewValue
         DisplayString = NewDisplayString
     End Sub

     Public Overrides Function ToString() As String
         Return DisplayString
     End Function
 End Class

then, to load the combobox from a collection or whatever.

    For Each o as WhateverObject In CollectionIwantToaddItemsFrom
        li = New ListItem(o.ValueToReturn, o.ValueToDisplay)
        Me.ComboBox1.Items.Add(li)
    Next

and finally, to use the object

Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles ComboBox1.SelectedIndexChanged
if me.combobox1.selecteditem is nothing then exit sub
Dim li As ListItem = me.ComboBox1.SelectedItem
Dim o as object = li.value 
'do stuff with o.
end sub

I'm sure there is something I'm better to use in the framework that I'm over looking. What is it?

+1  A: 

For lack of better understanding about your application architecture, you're probably taking a good approach.

Normally, I bind the comboboxes to DAL objects where the .ToString() method is overridden. This provides essentially the same feature, though I do have to recast whatever's selected in the combo to its original type to get the "value" (usually the property corresponding to the PK of the entity).

John Rudy
A: 

Well, normally, the object you'd place in the Items collection would have their own ToString() which present the object in a presentable form.

If, however, you want to have a completely unrelated string displayed for your object, then you are going to have to do it the way you right.

James Curran
A: 

I usually end up creating a datatable and setting this datatable as the datasource of the combobox. Then I set the DisplayMember to the column I want displayed and the ValueMember to the value I want returned. There may be something better besides datatables, but those are what I use when I need this functionality.

Aaron Smith
+3  A: 

This is a bit of a hack, but it means you don't have to write your own Name-Value pair class - not a big deal (could be there's something better already in the framework). But what you could do is use the DictionaryEntry class - which is effectively a name value pair. Add the items to a list, then use the DataMember and ValueMember properties on the combobox to bind to the key and value properties of the DictionaryEntry class. Like this:

    var list = new List<System.Collections.DictionaryEntry>();
    list.Add(new System.Collections.DictionaryEntry("one", 1));
    list.Add(new System.Collections.DictionaryEntry("two", 2));
    list.Add(new System.Collections.DictionaryEntry("three", 3));

    comboBox1.DataSource = list;
    comboBox1.DisplayMember = "Key";
    comboBox1.ValueMember = "Value";

Just realised you prefer the vb dialect. The below does the same for VB :-)

Dim list As List(Of DictionaryEntry)

list = New List(Of DictionaryEntry)
list.Add(New DictionaryEntry("One", 1))
list.Add(New DictionaryEntry("Two", 2))
list.Add(New DictionaryEntry("Three", 3))

ComboBox1.DataSource = list
ComboBox1.DisplayMember = "Key"
ComboBox1.ValueMember = "Value"
Sam Meldrum