winform comboBox

Hi,
I want to assign a text value to the winform combobox and get its value set on click of a button
I want it something like below (When form loads it shows  "Assigned" as selected and when button is clicked it should display 50000. Below code doesnot work

    Me.ComboBox1.DisplayMember = "Assigned"
        Me.ComboBox1.ValueMember = 50000


 
dotnet0824Asked:
Who is Participating?
 
ladarlingConnect With a Mentor Commented:
This example does what you are talking about. It creates a datatable, populates it with Name/Value pairs, and then binds it to the combobox. When the user selects one of the bound items, a msgbox displays its selectedvalue.
To test it, create a form, add a button and a combobox, and paste it in. If you have any questions let me know.

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
 
        Dim dt As New DataTable("ValuePairs")
        dt.Columns.Add("Name")
        dt.Columns.Add("Value", GetType(Integer))
 
        For i = 1 To 10
            Dim nr As DataRow = dt.NewRow
            nr("Name") = "Item " & i
            nr("Value") = i * 16
            dt.Rows.Add(nr)
        Next
        dt.AcceptChanges()
 
 
        With ComboBox1
            .DataSource = dt
            .DisplayMember = "Name"
            .ValueMember = "Value"
        End With
    End Sub
    Private Sub ComboBox1_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox1.TextChanged
        If IsNumeric(ComboBox1.SelectedValue) Then
            MsgBox(ComboBox1.SelectedValue)
        End If
    End Sub

Open in new window

0
 
openshacCommented:
I need to use on of the following properties to programaticallly select a value in a combobox:
SelectedItem
SelectedIndex
SelectedText
SelectedValue

The properties DisplayMember and ValueMember are for binding to datasources.
0
 
ladarlingCommented:
Those two properties are for data source binding. Both properties expect references to datasource field properties, and you wont be able to get the functionality that you are looking for with a standard combobox in that manner.
However, you could use something like:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If ComboBox1.Text = "Assigned" Then ComboBox1.Text = 5000
End Sub
 
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

 
ladarlingCommented:
To add "Assigned" to your combobox:
ComboBox1.Items.Add("Assigned")
To select it :
ComboBox1.SelectedItem = "Assigned"
0
 
dotnet0824Author Commented:
well its this
i want to assign the  Text to "Assigned" and when its selected I shoul dget the selectedValue as 5000
How can we assign using them using  valuemember properties. Hope the question is clear
0
 
dotnet0824Author Commented:
POINT|S INCREASED  Thanks darling .
Actually I am converting a vb code just 3 lines of code to VB.NET Or C#.NET (Any code shown is fine)
   If IsNumeric(Rs1!CustomerID) Then
                    If Rs1!CustomerID = 50000 Then
                        dbcCustomer.BoundText = 50000       In .NET its comboBox
                        dbcCustomer.Text = " Assigned"
                   End If

Can the above example shown by darling made much simpler
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.