在文本框中显示Combobox所选项目

时间:2015-07-22 13:11:54

标签: vb.net visual-studio-2010 combobox selecteditem

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
请不要笑我只是随便在VS2010中阅读基础教程.. 我在这里的问题与文本框中的组合框显示中的所选项目无关。

3 个答案:

答案 0 :(得分:2)

第一个selectedint所以它不能有像1.0,45.2等的值。 2,TextBox1_TextChanged没有被触发,所以试试这样:

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

答案 1 :(得分:0)

这是c#代码,但我确信这个概念是一样的。您的TextBox1_TextChanged事件永远不会被触发,因为您永远不会设置文本框的文本以便可以将其删除,并将该代码移动到您的comboBox1_SelectedIndexChanged事件中。

    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();
    }

答案 2 :(得分:0)

首先,您选择了'变量是错误的类型。它需要是字符串或双精度类型。字符串,如果它只是用于可读性,如果你打算在计算中使用它,则为double。

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