-1

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

Raulp
  • 7,758
  • 20
  • 93
  • 155

1 Answers1

1

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";
}
Horus
  • 21
  • 4
  • yes it works fine but yes you are correct, I think i have put the groupBoxes inside each others (while dragging) , when I place it at differen tpositions(non overlapping) it works fine. Do you know a way to overlap the groupboxes one over the other?? – Raulp Aug 18 '20 at 15:44
  • you can do it at runtime, for example if you want groupBox2 and groupBox3 at groupBox1's location and with the same dimensions: groupBox2.Location = groupBox3.Location = groupBox1.Location; You do the same for .Width and .Height. at design you just select groupBox1, groupBox2, groupBox3 (ctrl+click) and align them using the buttons on the design bar – Horus Aug 18 '20 at 16:56