繁体   English   中英

无法使标签显示组合框选择中的值

[英]Can't get Label to display value from combo box selection

C#Winforms用户控件在这里。

所以我有一个名为酒精的标签对象。 我的组合框对象名为snryeastTypeComboBox。 我想以后保留酒精的数字供数学使用。 我正在尝试在标签中显示该数字,但是它不起作用...有什么想法吗?

public void snryeastTypeComboBox_TextChanged(object sender, EventArgs e)
{
    if (snryeastTypeComboBox.SelectedText == "CSM")
    {
        var alcoholTolerance = 14;
        alcohol.Text = alcoholTolerance.ToString();
    }

请尝试以下操作:

public void snryeastTypeComboBox_TextChanged(object sender, EventArgs e)
{
    if (snryeastTypeComboBox.Text == "CSM")
    {
        var alcoholTolerance = 14;
        alcohol.Text = alcoholTolerance.ToString();
    }

用.Text替换.SelectedText。

为了理解您的问题,我认为您需要在组合框中选择一个值时触发该事件。 在这种情况下,您应该订阅SelectedIndexChanged事件而不是TextChanged

如果用户可以通过在组合框中键入内容来更改值,则应使用TextChanged事件。

为此,需要将代码编写在事件SelectedIndexChanged而不是TextChanged

而不是SelectedText您应该使用Text

下面的代码可以正常工作。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM