I created a combobox with certain values in it, I need to read the selection from the combobox into a text box. Any suggestions to help me get started?

Dani AI

Generated

Three simple ways exist to get a ComboBox selection into a TextBox, depending on how the ComboBox was filled: read the displayed text, read the selected object, or read a bound value.

As suggested, the control properties show what each approach returns. For a plain list of strings the easiest is the Text property (it always reflects the currently shown text). If the ComboBox Items contain objects, SelectedItem returns the object — call ToString() or cast to your type and read the property you want. If the ComboBox is data-bound, set DisplayMember and ValueMember and read the bound value (use the appropriate event to avoid Nothing).

was right to point at change events: SelectedIndexChanged is the usual place to react to a user selection. If typing into the box must be handled too, use TextChanged. For data-source updates, SelectedValueChanged may be more appropriate. Always guard against no-selection (SelectedIndex = -1) and SelectedItem = Nothing to avoid exceptions.

Example patterns (WinForms):

' read the displayed text
TextBox1.Text = ComboBox1.Text

' safe SelectedItem handling
If ComboBox1.SelectedIndex >= 0 Then
    Dim sel = TryCast(ComboBox1.SelectedItem, MyItem)
    If sel IsNot Nothing Then
        TextBox1.Text = sel.DisplayName
    Else
        TextBox1.Text = ComboBox1.SelectedItem.ToString()
    End If
Else
    TextBox1.Text = String.Empty
End If

Notes and quick troubleshooting: if SelectedItem.ToString() shows the class name, either override ToString() or set DisplayMember. If SelectedValue is Nothing when bound, ensure ValueMember is set. If the value is read too early, populate the ComboBox first or read it from the change event after population. For ASP.NET WebForms the equivalent is DropDownList.SelectedItem.Text or SelectedValue on postback.

Recommended Answers

All 3 Replies

You begin your research reading the methods and properties from msdn (link) Look though the list of properties and method to find the one that returns the text from the selection field of the combobox. Then all you have to do is copy the text into a textbox (a simple assignment)

I could just tell you, but I won't because you need to learn how to find out those things for yourself. It's part of the learning process.

You should look at using the ComboBox_SelectedIndexChanged event.

This can be handled like so:

Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles ComboBox1.SelectedIndexChanged
    Try
        TextBox1.Text = ComboBox1.SelectedValue
    Catch ex As Exception
        MsgBox(ex.ToString)
    End Try
End Sub

I wasn't looking for the code itself. I found part of the code I need, I just need a little push, thank you!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.