winform comboBox

Posted on 2008-11-07
Last Modified: 2013-12-17
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

Question by:dotnet0824
    LVL 6

    Expert Comment

    I need to use on of the following properties to programaticallly select a value in a combobox:

    The properties DisplayMember and ValueMember are for binding to datasources.
    LVL 11

    Expert Comment

    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
    LVL 11

    Expert Comment

    To add "Assigned" to your combobox:
    To select it :
    ComboBox1.SelectedItem = "Assigned"

    Author Comment

    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
    LVL 11

    Accepted Solution

    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("Value", GetType(Integer))
            For i = 1 To 10
                Dim nr As DataRow = dt.NewRow
                nr("Name") = "Item " & i
                nr("Value") = i * 16
            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
            End If
        End Sub

    Open in new window


    Author Comment

    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

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
    It’s quite interesting for me as I worked with Excel using for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now