Raulp
Raulp

Reputation: 8196

Making the Visibility gone for a group Box placed at same position - C#

I have this windows form having three Group Boxes positioned at the same position.

On a selection of different option i want to make only one of them visible and rest invisible

By Default all are invisible .

here is the piece of code . I see only on selecting D1 groupBox1 is visible and on Selecting D2 and D3 groupBox1 disappears but groupBox2 and 3 never appears.

 private void Form1_load(object sender,EventArgs e)
        {
            comboBoxCategory.Items.Add("A");
            comboBoxCategory.Items.Add("B");
            comboBoxCategory.Items.Add("C");
            comboBoxCategory.Items.Add("D");

            groupBox1.Visible = false;
            groupBox2.Visible = false;
            groupBox3.Visible = false;


        }

           
        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            comboBoxMovie.Items.Clear();

            switch(comboBoxCategory.SelectedItem.ToString())
            {

                case "A":
                this.comboBoxMovie.Items.Add("A1");
                this.comboBoxMovie.Items.Add("A2");
                this.comboBoxMovie.Items.Add("A3");
                this.comboBoxMovie.Items.Add("A4");
                break;

                case "B":
                this.comboBoxMovie.Items.Add("B1");
                this.comboBoxMovie.Items.Add("B2");
                this.comboBoxMovie.Items.Add("B3");
                this.comboBoxMovie.Items.Add("B4");
                break;

                case "C":
                this.comboBoxMovie.Items.Add("C1");
                this.comboBoxMovie.Items.Add("C2");
                this.comboBoxMovie.Items.Add("C3");
                this.comboBoxMovie.Items.Add("C4");
                break;

                case "D":
                this.comboBoxMovie.Items.Add("D1");
                this.comboBoxMovie.Items.Add("D2");
                this.comboBoxMovie.Items.Add("D3");
                this.comboBoxMovie.Items.Add("D4");
                break;


            }
          
           
        }




private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            if(comboBoxMovie.SelectedItem.ToString() == "D1")
            {

                groupBox1.Visible = true;
                groupBox2.Visible = false;
                groupBox3.Visible = false;

            }
            if (comboBoxMovie.SelectedItem.ToString() == "D2")
            {

                groupBox1.Visible = false;
                groupBox2.Visible = true;
                groupBox3.Visible = false;

            }

            if (comboBoxMovie.SelectedItem.ToString() == "D3")
            {

                groupBox1.Visible = false;
                groupBox2.Visible = false;
                groupBox3.Visible = true;

            }
        }

enter image description here

Upvotes: -1

Views: 928

Answers (1)

Horus
Horus

Reputation: 21

You can use a TabControl with the TabPages you need for it. Otherwise, check that groupboxes are not one inside the other and afterr clearing and adding items to comboBoxMovie you must set the selectedIndex=0 or ... so that SelectedIndexChange event can be invoked. Tip: you can control groupBoxes visibility just by comparing comboBoxMovie.SelectedItem.ToString() any value instead using 3 if statements:

private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
    groupBox1.Visible = comboBoxMovie.SelectedItem.ToString() == "D1";
    groupBox2.Visible = comboBoxMovie.SelectedItem.ToString() == "D2";
    groupBox3.Visible = comboBoxMovie.SelectedItem.ToString() == "D3";
}

Upvotes: 1

Related Questions