根据选定的组合框索引隐藏GroupBox

时间:2015-05-04 15:56:15

标签: c# groupbox

我正在尝试显示/隐藏2个组合框,具体取决于所选的事件类型。 该计划应该做的是:如果选择展览,将显示展览的组合框,同时隐藏俱乐部组合框。(如果选择了俱乐部,反之亦然)

代码:

namespace Promoter.Forms

public partial class eventCreate : Form
{
    public eventCreate()
    {
        InitializeComponent();

        //import enum value to combo box
        cmbEventType.DataSource = Enum.GetValues(typeof(EventType));

    }

    private void eventCreate_Load(object sender, EventArgs e)
    {

        if (cmbEventType.Text == "Exhibition")
        {
            grpClubbing.Visible = false;

            grpExhibition.Visible = true;
            //import enum values to combo box
            cmbExhibitionVenue.DataSource = Enum.GetValues(typeof(ExhibitionVenue));

        }
        else if (cmbEventType.Text == "Clubbing")
        {
            grpExhibition.Visible = false;

            grpClubbing.Visible = true;
            //import enum values to combo box
            cmbClubbingVenue.DataSource = Enum.GetValues(typeof(ClubbingVenue));

        }
    }

2 个答案:

答案 0 :(得分:1)

尝试使用SelectedIndexChanged事件。

    private void cmdEventType_SelectedIndexChanged( object sender, EventArgs e ) {
        ComboBox cb = (ComboBox)sender;
        grpClubbing.Visible=false;
        grpExibition=false;
        switch ( cb.SelectedText ) {
            case "Exhibition":
                grpExhibition.Visible = true;
                //import enum values to combo box
                cmbExhibitionVenue.DataSource = Enum.GetValues(typeof(ExhibitionVenue));
                break;
            case "Clubbing":
                grpClubbing.Visible = true;
                //import enum values to combo box
                cmbClubbingVenue.DataSource = Enum.GetValues(typeof(ClubbingVenue));
                break;
            default:
                break;
        }
    }

答案 1 :(得分:0)

修正了它,我的脑海中缺少一个逻辑。

而不是

private void eventCreate_Load(object sender, EventArgs e)

我不得不推理并在:

private void cmbEventType_SelectedIndexChanged(object sender, EventArgs e)