使用1按钮在每次单击时显示不同的组框

时间:2017-05-22 12:27:43

标签: c# winforms

我正在创建一个排序向导。就像它是一个有4个组框和一个按钮的表单。想法是当表单加载以显示groupbox1然后每按一次按钮它将转到groupbox2,3,4

我尝试过在线发现的几件不同的东西。从groupbox1.visible = groupbox1.visible

开始

似乎没有任何效果。我甚至试过做一个bool类型设置

        bool boxon2 = true;
        bool boxon3 = true;
        bool boxon4 = true;

        if (boxon2)
        {
            groupBox1.Visible = false;
            groupBox2.Visible = true;
            boxon2 = false;
        }

        if (boxon3)
        {
            groupBox2.Visible = false;
            groupBox3.Visible = true;
            boxon3 = false;
        }

        if (boxon4)
        {
            groupBox3.Visible = false;
            groupBox4.Visible = true;
            boxon4 = false;
        }

如果有人有任何想法会很棒。

1 个答案:

答案 0 :(得分:1)

只需使用简单的开关声明:

public partial class Form1 : Form
{
    int counter;
    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        // increment counter for whits groupbox you want to show
        counter++;
        switch (counter)
        {
            case 1:
                groupBox1.Visible = true;
                groupBox2.Visible = false;
                groupBox3.Visible = false;
                groupBox4.Visible = false;
                break;
            case 2:
                groupBox1.Visible = false;
                groupBox2.Visible = true;
                groupBox3.Visible = false;
                groupBox4.Visible = false;
                break;                   
            case 3:
                groupBox1.Visible = false;
                groupBox2.Visible = false;
                groupBox3.Visible = true;
                groupBox4.Visible = false;
                break;
            case 4:
                groupBox1.Visible = false;
                groupBox2.Visible = false;
                groupBox3.Visible = false;
                groupBox4.Visible = true;
                // set to 0 if counter is 4 so groupbox one will be the next grupbox that will be set to visible
                counter = 0;
                break;
        }
    }
}

注意将groupboxes属性设置为false:

enter image description here