vb.net 在文本框中显示组合框所选项目
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31564186/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Show Combobox selected item in textbox
提问by user3352725
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..
请不要笑我只是在 VS2010 中随意阅读基本教程。
回答by apomene
1st selectedis an intso it cant have values like 1.0,45.2,etc.
2nd, TextBox1_TextChanged is not fired, so try like this:
第一个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
回答by Josh Davis
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.
这是 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();
}
回答by Karlta05
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

