Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 738
  • Last Modified:

Populating ComboBoxes in VS 2008 Using VB.NET

Hello,

I am building a VS 2008 VB.NET Windows Application.  I have a combobox that I would like to populate with different strings for the text and value property of each item in the combobox.  I believe the syntax mycombobox.Items.Add("xxx") would assign "xxx" to both the text and value properties.  But what if I wanted the text property to be "xxx", and the value property to "yyy".  Can anyone tell me the command syntax to use?

Thank you, Stu Engelman
0
stuengelman
Asked:
stuengelman
1 Solution
 
anyoneisCommented:
Generally speaking, you want the item collection to be a collection of objects with multiple properties. You then set DisplayMember to the name of the property you want to display, and ValueMember to the name of the property you want as the value.
David
0
 
stuengelmanAuthor Commented:
Hi David,

If I'm understanding you correctly, my code would probably be something like this:

Dim zzz as New ControlName
zzz.DisplayMember="xxx"
zzz.ValueMember="yyy"
mycombobox.Items.Add(zzz)

My question is: what should I use for ControlName?
0
 
stuengelmanAuthor Commented:
Hi David,

I did some hunting around, and it looks like the object I need is ComboBoxItem.  I.e., the code would be mycombobox.Items.Add(new ComboBoxItem("display","value")).  But ComboBoxItem throws a design time error of "Type Not Defined".  According to the Object Browser, ComboBoxItem is a member of System.Windows.Controls, but I can't create a reference to this class in my Project References tab, and using the statement "Imports System.Windows.Controls" throws a design time error saying the class can't be found or has no public members.

Stu
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
Jaime OlivaresCommented:
what you should add is an object, not a string. Let's say you have a MyData class with properties Name and Value.
Then you have a collection containing multiple objects of class MyData

Dim coll as new ArrayList
Dim zzz as new MyData
zzz.Name = "Mary"
zzz.Value = 100
coll.Add(zzz)

mycombobox.DisplayMember = "Name"
mycombobox.ValueMember = "Value"
mycombobox.DataSource = coll

0
 
stuengelmanAuthor Commented:
OK, I get it.  You're saying I have to bind the combobox to an arraylist.

I found out just now that the ComboBoxItem only exists for with WPF/XAML, explaining why I couldn't declare it on a standard Windows form.

Let me try your method and report back.

Stu
0
 
nffvrxqgrcfqvvcCommented:
You may also create your own class and override ToString() here is an example that might help you.

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ComboBox1.Items.Add(New Stuff("Display1", "123"))
        ComboBox1.Items.Add(New Stuff("Display2", "456"))
        ComboBox1.Items.Add(New Stuff("Display3", "789"))
    End Sub

    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
        Dim item As Stuff = DirectCast(ComboBox1.SelectedItem, Stuff)
        Debug.Print("{0}    {1}", item.DisplayMember, item.ValueMember)
    End Sub

End Class

Public Class Stuff
    Private _displayMember As String
    Private _valueMember As String
    Protected Friend Sub New(ByVal displayMember As String, _
                             ByVal valueMember As String)
        Me._displayMember = displayMember
        Me._valueMember = valueMember
    End Sub
    Public ReadOnly Property DisplayMember As String
        Get
            Return _displayMember
        End Get
    End Property
    Public ReadOnly Property ValueMember As String
        Get
            Return _valueMember
        End Get
    End Property
    Public Overrides Function ToString() As String
        Return _displayMember 'displayed in the combobox
    End Function
End Class

Open in new window

0
 
stuengelmanAuthor Commented:
Hi egl1044,

I attempted your method.  I created a class module with the following code:

Public Class CtlListLoader
    Private _displayMember As String
    Private _valueMember As String
    Protected Friend Sub New(ByVal displayMember As String, ByVal valueMember As String)
        Me._displayMember = displayMember
        Me._valueMember = valueMember
    End Sub
End Class

In my form, I used the syntax for loading the combobox cbMIMember as follows:

cbMIMember.Items.Add(New CtlListLoader(cbtxt, CStr(RDR("MemberID")))),

where cbtxt is a string variable is loaded with a person's name, and RDR("MemberID") is that person's member ID from the database.

I tried displaying the value of cbMIMember.SelectedText and cbMIMember.SelectedValue in a msgbox, but both were blank, even though the code explicitly selects the first combobox listitem.

Also, the text in the combobox's listitems was "MMCApplication.CtlListLoader" for each listitem, instead of the person's name (MMCApplication is the name of the application).

Could you please advise?

Thanks, Stu
0
 
nffvrxqgrcfqvvcCommented:
You need to override ToString() method and return what you would like to see in the display of the combobox like in the example Stuff class. =)
0
 
stuengelmanAuthor Commented:
Hi egl1044,

I'm confused about two points:

(1) I'm not using the ToString() method in the code snippets we are discussing, so I'm confused as to why I need to overrride what is in the BCL for the ToString() method.

(2) If I use the ToString() method within a form for a purpose unrelated to the issue we are discussing, I would want the method to work in the usual way (convert a numeric or date to a string), not return the last "_displayMember" setting from the custom class.

Stu
0
 
nffvrxqgrcfqvvcCommented:
In the example your creating your own class object that you can *Add into the ComboBox when you add the item the ComboBox will call ToString() in this case if your passing an Object(Stuff Class) and you don't override ToString() its going to display ApplicationName.Stuff... If you override ToString() and return what you would like to see displayed in the ComboBox it will use that value instead. You can extend the class with additional properties ... Then when a selection is made the ComboBox will pass the *Object (Stuff Class) which you can cast to a Stuff object because you know thats what the type of Object is expected then you can obtain the information you populated when you added it to the ComboBox.
0
 
stuengelmanAuthor Commented:
Great job, worked perfectly.  Thank you
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now