Link to home
Start Free TrialLog in
Avatar of FrankBabz
FrankBabz

asked on

Combobox in windows Form

I want to use a combobox where one column would be an ID and the other column the data,
Here is FillCombo code that I'm trying to make work.  Problem is "selectedValue" always returns nothing.
    Sub FillCombo()
        Dim i As Integer, M As String
        For i = 0 To 19
            cboBookmarks.SelectedValue = "Id" & Str(i)
            M = "Data" & Str(i)
            cboBookmarks.SelectedText = M
            cboBookmarks.Items.Add(M)
        Next (i)
        cboBookmarks.Text = "item"
    End Sub

    Private Sub cboBookmarks_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboBookmarks.SelectedIndexChanged
        Dim index As Integer = cboBookmarks.SelectedIndex
        Dim Item As String = cboBookmarks.SelectedItem
        Dim Value As String = cboBookmarks.SelectedValue
        '
        MsgBox("Selected = '" & Item & "', and the ID Value ='" & Value & "'")
        RunMMtest.Focus()
    End Sub

Open in new window

Can someone explain what I'm doing wrong, and provide a sample that works?

Thanks
Avatar of Tom Beck
Tom Beck
Flag of United States of America image

Line 4 is not doing anything because it does not refer to any particular item in the combobox. You need something a little more complex. Try the code below.
Sub FillCombo()
        Dim i As Integer
        For i = 0 To 19
            cboBookmarks.Items.Add(New ComboBoxItem("Data" & Str(i), "Id" & Str(i)))
        Next (i)
        cboBookmarks.Text = "item"
    End Sub

    Private Sub cboBookmarks_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboBookmarks.SelectedIndexChanged
        Dim selectedItem As ComboBoxItem = CType(cboBookmarks.SelectedItem, ComboBoxItem)
        Dim Item As String = selectedItem.DisplayText
        Dim Value As String = selectedItem.ItemValue
        '
        MsgBox("Selected = '" & Item & "', and the ID Value ='" & Value & "'")
        RunMMtest.Focus()
    End Sub

    Private Structure ComboBoxItem
        Dim DisplayText As String
        Dim ItemValue As String

        Public Sub New(ByVal _DisplayText As String, ByVal _ItemValue As String)
            DisplayText = _DisplayText
            ItemValue = _ItemValue
        End Sub

        Public Overrides Function ToString() As String
            Return DisplayText
        End Function
    End Structure

Open in new window

I think tommyBoy exposes a valid solution.

But do´nt says nothing about the reason to implements "ToString" on Structure ComboBoxItem.

You can manage the Format Event on ComboBox to Display apropiate text on a combobox for selected item.

But the easy solution is to use any class as item for ComboBox.
The Default Text you can see on a ComboBox.Text for selected Item is the Method "ToString" from Item Object.

Note. On this sample, Id (without "Id" prefix) always matches combobox' index
Then You do´nt need create Structure or class, but exposed solution is generic.
SOLUTION
Avatar of Ark
Ark
Flag of Russian Federation image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Oops,
cboItems.Add(New ComboBoxItem(p1, p2, p3, p4)
should be
cboItems.Add(New MyData(p1, p2, p3, p4))
Avatar of FrankBabz
FrankBabz

ASKER

I thank all 3 of you for your time and replies.  I ran TommyBoy's code and excepting that cboBookmarks.Text = "item" does nothing... it worked great.  My problem, due to inexperience, is I am lost in it's apparent elegance and cannot understand it.  I had hoped to learn what is required to make a combobox work, and what I was doing wrong.  What parameter(s) is required for ADDing the ID and Data fields to a combobox?  What is comboboxitem actually generating?  Why is ToString being overridden?  What is the "new" sub purpose?

Thanks in advance for any help.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks to all of you,
Frank
>>excepting that cboBookmarks.Text = "item" does nothing
Actually, I took this from your code and it does do something. It adds the word "item" as the first object in the combo box.

Not sure where "first object" is, but it is not visible anywhere.

In vb.net I never found a combobox control.  Did you mean dropdownbox?  I did find a combobox in Access having column support that I liked a lot.  Are vb Windows Forms considered to be in vb.net?
The title of your question is "Combobox in a Windows Form". This can only mean a Windows Application. Combo boxes are not available to Web Applications.

Vb.net is a programming language that is available to both Windows Applications (Windows forms) and Web Applications. Vb.net should not be confused with asp.net. Asp.net is not a programming language but rather a term to describe the coding of web pages to take advantage of the .net framework. Web pages can be coded in vb.net, C# or J#.

Thanks for the points.
ComboBox.jpg
You deserve more than I have offered because you have taken your valuable time for very little in return....  and I have learned a lot.

Thanks for the jpg, it depicts exactly what I surmised to be the "first object."  In my application, however, it does not display as you depicted.  My "first object" is blank.  In time, I may figure out why, and I will explain later.

God bless!
Follow up:  
Changing the style from "dropdownlist" to either "simple" or "dropdown" will show/display the "first object" in the control.   I prefered "dropdownlist" only for appearance purposes with no real understanding that style may do more than change the apppearance of "first object."  I notice in the control's snippet of help for Style selection it says "Controls the appearance and functionality of the combo box."   Now I know one "functionality" change, but I know nothing more, and find this a strange and annoying function change.   Oh well????