?
Solved

winform comboBox

Posted on 2008-11-07
6
Medium Priority
?
780 Views
Last Modified: 2013-12-17
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


 
0
Comment
Question by:dotnet0824
  • 3
  • 2
6 Comments
 
LVL 6

Expert Comment

by:openshac
ID: 22906029
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
 
LVL 11

Expert Comment

by:ladarling
ID: 22906085
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
 
LVL 11

Expert Comment

by:ladarling
ID: 22906147
To add "Assigned" to your combobox:
ComboBox1.Items.Add("Assigned")
To select it :
ComboBox1.SelectedItem = "Assigned"
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.

 

Author Comment

by:dotnet0824
ID: 22908236
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
 
LVL 11

Accepted Solution

by:
ladarling earned 1000 total points
ID: 22908673
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
 

Author Comment

by:dotnet0824
ID: 22909189
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

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses

830 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