在文本框中显示组合框选定的项目
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
大家别笑我只是随便看VS2010基础教程..
我的问题是组合框中的所选项目没有显示在文本框中..
1st selected
是一个 int
,所以它不能有 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
这是 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();
}
首先,您的 'selected' 变量类型错误。它需要是字符串或双精度类型。如果只是为了提高可读性,则为 String;如果打算在计算中使用,则为 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
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基础教程.. 我的问题是组合框中的所选项目没有显示在文本框中..
1st selected
是一个 int
,所以它不能有 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
这是 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();
}
首先,您的 'selected' 变量类型错误。它需要是字符串或双精度类型。如果只是为了提高可读性,则为 String;如果打算在计算中使用,则为 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