none
how can i invisible all combobox instead current combobox in c#? RRS feed

  • Question

  • I have some combobox in windows form and when I select special radio button some of these combobox have been shown. now, I want to hide previous open combobox when I select other radio button. I do not want to name each one. can I invisible all of them instead  current combobox?

    thatnks.

     
    Saturday, December 23, 2017 9:36 AM

Answers

  • Hello nadianaji,

    I think you could iterate through all combobox to hide each of them and then just show the combobox you want to show. 

            private void radioButton2_CheckedChanged(object sender, EventArgs e)
            {
                if (radioButton2.Checked)
                {
                    foreach (Control ctl in ActiveForm.Controls)
                    {
                        if (ctl is ComboBox)
                        {
                            ctl.Visible = false;
                        }
                    }
                }
            }

    Besides, this forum(Excel for Developers) is for development issues when using Excel Object Model. If you have any issues related to WinForm, i suggest you post on Windows Forms forum.

    Best Regards,

    Terry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Monday, December 25, 2017 7:49 AM
  • Hi nadianaji,

    you had mentioned that,"I have some combobox in windows form and when I select special radio button some of these combobox have been shown. now, I want to hide previous open combobox when I select other radio button."

    you can refer example below.

    public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void radioButton1_CheckedChanged(object sender, EventArgs e)
            {
                if (radioButton1.Checked==true)
                {
                    comboBox1.Visible = true;
                    comboBox2.Visible = false;
                    comboBox3.Visible = false;
                }
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                comboBox1.Visible = false;
                comboBox2.Visible = false;
                comboBox3.Visible = false;
            }
    
            private void radioButton2_CheckedChanged(object sender, EventArgs e)
            {
                if (radioButton2.Checked == true)
                {
                    comboBox1.Visible = false;
                    comboBox2.Visible = true;
                    comboBox3.Visible = false;
                }
            }
    
            private void radioButton3_CheckedChanged(object sender, EventArgs e)
            {
                if (radioButton3.Checked == true)
                {
                    comboBox1.Visible = false;
                    comboBox2.Visible = false;
                    comboBox3.Visible = true;
                }
            }
        }

    Output:

    same thing can be achieved by looping through the combobox controls in form.

    below is the example for the same.

      public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void radioButton1_CheckedChanged(object sender, EventArgs e)
            {
                List<string> Items = new List<string>();
                var comboBoxes = this.Controls
                      .OfType<ComboBox>()
                      .Where(x => x.Name.StartsWith("comboBox"));
                foreach (var cmbBox in comboBoxes)
                {
                   if (cmbBox.Name != comboBox1.Name )
                    {
                        cmbBox.Visible = false;
                    }
                   else
                    {
                        cmbBox.Visible = true;
                    }
                }
    
              
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                comboBox1.Visible = true;
                comboBox2.Visible = true;
                comboBox3.Visible = true;
            }
    
            private void radioButton2_CheckedChanged(object sender, EventArgs e)
            {
                List<string> Items = new List<string>();
                var comboBoxes = this.Controls
                      .OfType<ComboBox>()
                      .Where(x => x.Name.StartsWith("comboBox"));
                foreach (var cmbBox in comboBoxes)
                {
                    if (cmbBox.Name != comboBox2.Name)
                    {
                        cmbBox.Visible = false;
                    }
                    else
                    {
                        cmbBox.Visible = true;
                    }
                }
            }
    
            private void radioButton3_CheckedChanged(object sender, EventArgs e)
            {
                List<string> Items = new List<string>();
                var comboBoxes = this.Controls
                      .OfType<ComboBox>()
                      .Where(x => x.Name.StartsWith("comboBox"));
                foreach (var cmbBox in comboBoxes)
                {
                    if (cmbBox.Name != comboBox3.Name)
                    {
                        cmbBox.Visible = false;
                    }
                    else
                    {
                        cmbBox.Visible = true;
                    }
                }
                }
        }

    Output:

    you can modify the logic and code based on your requirement.

    Regards

    Deepak


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by nadianaji Tuesday, December 26, 2017 7:58 AM
    Tuesday, December 26, 2017 2:23 AM
    Moderator

All replies

  • Hello nadianaji,

    I think you could iterate through all combobox to hide each of them and then just show the combobox you want to show. 

            private void radioButton2_CheckedChanged(object sender, EventArgs e)
            {
                if (radioButton2.Checked)
                {
                    foreach (Control ctl in ActiveForm.Controls)
                    {
                        if (ctl is ComboBox)
                        {
                            ctl.Visible = false;
                        }
                    }
                }
            }

    Besides, this forum(Excel for Developers) is for development issues when using Excel Object Model. If you have any issues related to WinForm, i suggest you post on Windows Forms forum.

    Best Regards,

    Terry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Monday, December 25, 2017 7:49 AM
  • Hi nadianaji,

    you had mentioned that,"I have some combobox in windows form and when I select special radio button some of these combobox have been shown. now, I want to hide previous open combobox when I select other radio button."

    you can refer example below.

    public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void radioButton1_CheckedChanged(object sender, EventArgs e)
            {
                if (radioButton1.Checked==true)
                {
                    comboBox1.Visible = true;
                    comboBox2.Visible = false;
                    comboBox3.Visible = false;
                }
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                comboBox1.Visible = false;
                comboBox2.Visible = false;
                comboBox3.Visible = false;
            }
    
            private void radioButton2_CheckedChanged(object sender, EventArgs e)
            {
                if (radioButton2.Checked == true)
                {
                    comboBox1.Visible = false;
                    comboBox2.Visible = true;
                    comboBox3.Visible = false;
                }
            }
    
            private void radioButton3_CheckedChanged(object sender, EventArgs e)
            {
                if (radioButton3.Checked == true)
                {
                    comboBox1.Visible = false;
                    comboBox2.Visible = false;
                    comboBox3.Visible = true;
                }
            }
        }

    Output:

    same thing can be achieved by looping through the combobox controls in form.

    below is the example for the same.

      public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void radioButton1_CheckedChanged(object sender, EventArgs e)
            {
                List<string> Items = new List<string>();
                var comboBoxes = this.Controls
                      .OfType<ComboBox>()
                      .Where(x => x.Name.StartsWith("comboBox"));
                foreach (var cmbBox in comboBoxes)
                {
                   if (cmbBox.Name != comboBox1.Name )
                    {
                        cmbBox.Visible = false;
                    }
                   else
                    {
                        cmbBox.Visible = true;
                    }
                }
    
              
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                comboBox1.Visible = true;
                comboBox2.Visible = true;
                comboBox3.Visible = true;
            }
    
            private void radioButton2_CheckedChanged(object sender, EventArgs e)
            {
                List<string> Items = new List<string>();
                var comboBoxes = this.Controls
                      .OfType<ComboBox>()
                      .Where(x => x.Name.StartsWith("comboBox"));
                foreach (var cmbBox in comboBoxes)
                {
                    if (cmbBox.Name != comboBox2.Name)
                    {
                        cmbBox.Visible = false;
                    }
                    else
                    {
                        cmbBox.Visible = true;
                    }
                }
            }
    
            private void radioButton3_CheckedChanged(object sender, EventArgs e)
            {
                List<string> Items = new List<string>();
                var comboBoxes = this.Controls
                      .OfType<ComboBox>()
                      .Where(x => x.Name.StartsWith("comboBox"));
                foreach (var cmbBox in comboBoxes)
                {
                    if (cmbBox.Name != comboBox3.Name)
                    {
                        cmbBox.Visible = false;
                    }
                    else
                    {
                        cmbBox.Visible = true;
                    }
                }
                }
        }

    Output:

    you can modify the logic and code based on your requirement.

    Regards

    Deepak


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by nadianaji Tuesday, December 26, 2017 7:58 AM
    Tuesday, December 26, 2017 2:23 AM
    Moderator