[英]Can't get Label to display value from combo box selection
C# Winforms User control here. C#Winforms用户控件在这里。
So I have a label object named alcohol. 所以我有一个名为酒精的标签对象。 My combo box object is named snryeastTypeComboBox.
我的组合框对象名为snryeastTypeComboBox。 I want to keep the number for alcohol for math later.
我想以后保留酒精的数字供数学使用。 I'm trying to display the number in a label yet it isn't working... any ideas?
我正在尝试在标签中显示该数字,但是它不起作用...有什么想法吗?
public void snryeastTypeComboBox_TextChanged(object sender, EventArgs e)
{
if (snryeastTypeComboBox.SelectedText == "CSM")
{
var alcoholTolerance = 14;
alcohol.Text = alcoholTolerance.ToString();
}
Try the following instead: 请尝试以下操作:
public void snryeastTypeComboBox_TextChanged(object sender, EventArgs e)
{
if (snryeastTypeComboBox.Text == "CSM")
{
var alcoholTolerance = 14;
alcohol.Text = alcoholTolerance.ToString();
}
Replace .SelectedText with just .Text. 用.Text替换.SelectedText。
To understand your question, I think you need to fire the event when you select a value from your combo box. 为了理解您的问题,我认为您需要在组合框中选择一个值时触发该事件。 If that is the case you should subscribe to
SelectedIndexChanged
event instead of TextChanged
. 在这种情况下,您应该订阅
SelectedIndexChanged
事件而不是TextChanged
。
TextChanged
event should be used if the user can change the value by typing in your combo box. 如果用户可以通过在组合框中键入内容来更改值,则应使用
TextChanged
事件。
To achieve this your code needs to be written inside the event SelectedIndexChanged
instead of TextChanged
. 为此,需要将代码编写在事件
SelectedIndexChanged
而不是TextChanged
。
Instead of SelectedText
you should use Text
而不是
SelectedText
您应该使用Text
The below code will work fine. 下面的代码可以正常工作。
private void snryeastTypeComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
if (snryeastTypeComboBox.Text == "CSM")
{
var alcoholTolerance = 14;
alcohol.Text = alcoholTolerance.ToString();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.