user3352725
user3352725

Reputation: 3

Show Combobox selected item in textbox

Public Class Form1
Public selected As Integer

Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
    Select Case ComboBox1.SelectedItem
        Case "Philippines (PHP)"
            selected = 1.0
        Case "United States(USD)"
            selected = 45.2
        Case "Japan(JPY)"
            selected = 0.36
        Case "Canada(CAD)"
            selected = 35.01
        Case "Australia(AUD)"
            selected = 33.34
    End Select
End Sub

Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
    TextBox1.Text = ComboBox1.SelectedItem
End Sub
End Class

please dont laugh i am just casually reading basic tutorial in VS2010.. my problem here is nothing from the selected item in combobox shows in the textbox..

Upvotes: 0

Views: 30572

Answers (3)

Karlta05
Karlta05

Reputation: 165

Firstly, your 'selected' variable is the wrong type. It needs to be a string or a double type. String if it's just going to be used for readability and double if you intend on using it in a calculation.

Public selected As Double

Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
    ' Call the ToString() method to get the text.
    Select Case ComboBox1.SelectedItem.ToString()
        Case "Philippines (PHP)"
            selected = 1.0
        Case "United States(USD)"
            selected = 45.2
        Case "Japan(JPY)"
            selected = 0.36
        Case "Canada(CAD)"
            selected = 35.01
        Case "Australia(AUD)"
            selected = 33.34
    End Select
    ' You need to catch if the selected variable has not value set.
    Textbox1.Text = selected.ToString()
End Sub

Upvotes: 0

Josh Davis
Josh Davis

Reputation: 90

This is c# code but I'm sure the concept is the same. Your TextBox1_TextChanged event is never being triggered because you are never setting the text of the textbox so that can be removed, and move that code into your comboBox1_SelectedIndexChanged event.

    private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        switch (comboBox1.SelectedItem.ToString())
        {
            case "Hey":
                selected = 1;
                break;
            case "There":
                selected  = 2;
                break;
            case "You":
                selected  = 33.34;
                break;
        }
        textBox1.Text = ComboBox1.SelectedItem.ToString();
    }

Upvotes: 0

apomene
apomene

Reputation: 14389

1st selected is an int so it cant have values like 1.0,45.2,etc. 2nd, TextBox1_TextChanged is not fired, so try like this:

Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
 ...
 TextBox1.Text = ComboBox1.SelectedItem

Upvotes: 2

Related Questions