组合框项目索引更改时显示组框 [英] show groupbox when combobox item index change

查看:94
本文介绍了组合框项目索引更改时显示组框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当组合框项目索引更改时如何显示和隐藏组框



how to show and hide groupbox when combobox item index change

if (this.cmbMet.SelectedIndexChanged != null)
         {
             grpMetName.Visible = true;

         }
      else 
         {
            grpMetName.Visible = false;
         }





我收到错误。请记住,我还是.NET框架和C#的新手。谢谢



I'm getting error. Bear in mind, I'm still new to .NET framework and C#. Thank you

推荐答案

你做错了。 SelectedIndexChanged是ComboBox上的一个事件。你可以检查一个事件是否为空,但所有告诉你的是一个或多个代表是否已被附加到它。



你没有确切地说你是什么'试图完成。你也没有说出错误是什么。缺乏信息使您很难帮助您。但是,我假设您正在尝试显示GroupBox,如果从ComboBox中选择了一个项目。



1.为SelectedIndexChanged事件添加一个事件处理程序。您可以从Windows窗体设计器执行此操作。但是,您也可以使用以下代码执行此操作:

You're doing it wrong. SelectedIndexChanged is an event on the ComboBox. You can check if an event is null, but all that tells you is if one or more delegates have been attached to it.

You didn't say exactly what you're trying to accomplish. You also didn't say what the error is. The lack of information makes it hard to help you. But, I'll assume you're trying to show the GroupBox if an item is selected from the ComboBox.

1. Add an event handler for the SelectedIndexChanged event. You can do this from the Windows Forms designer. But, you can also do it in code like this:
comboBox1.SelectedIndexChanged += comboBox1_SelectedIndexChanged;





2.添加处理事件的函数



2. Add the function that handles the event

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    bool hasSelection = (comboBox1.SelectedIndex >= 0);
    groupBox1.Visible = hasSelection;
}


ComboBox SelectedIndexChanged是一个EventHandler:只要运行时用户更改ComboBox中的Selected Item,就会触发/触发/调用它。如果您这样写:
ComboBox SelectedIndexChanged is an EventHandler: it is triggered/fired/invoked anytime the run-time user changes the Selected Item in a ComboBox. If you write this:
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    groupBox1.Visible = true;
}

然后,每次用户更改ComboBox中的Selected Item时,GroupBox的Visible属性都将设置为true。那可能不是你想要的。



我的猜测是你想让GroupBox在中只显示如果在ComboBox中选择了一个Item,如果选择了任何其他项目,则隐藏。

Then, every time the user changes the Selected Item in the ComboBox, the GroupBox will have its Visible Property set to 'true. That's probably not what you want.

My guess is that you want the GroupBox made visible only if one Item is selected in the ComboBox, and hidden if any other Item is selected.

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    groupBox1.Visible = comboBox1.SelectedIndex == 0;

    switch (comboBox1.SelectedIndex)
    {
        case 0:
            // more things to do if the first Item is selected
            break;
        case 1:
            // stuff to do if Item two is selected
            break;
    }
}


这篇关于组合框项目索引更改时显示组框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆