Search code examples
c#winformsgroupbox

How to reset groupbox items?


I wanna clean my groupbox items after i click an button.

I tried some code blocks, but they don´t work for reset controls.

I don´t wanna remove or delete it, I just wanna reset the items in the groupbox.

This is working for remove groupbox items.

 public void ClearPanels(GroupBox control)
    {
        control.Controls.Clear();
    }

or this

groupBox2.Controls.Clear();

It looks like this, before click. Before Click

And when I click the button, as you can see on the right side.

After Click

It is removed, but I want to reset it.

Any ideas how I can do this ?


Solution

  • I'm going to asume that clear means to leave it all by default. You need to llop all the controls inside the groupbox and depending on what control they are do something or something else.

    foreach (Control ctr in GB.Controls)
    {
        if (ctr is TextBox)
        {
            ctr.Text = "";
        }
        else if (ctr is CheckedListBox)
        {
            CheckedListBox clb = (CheckedListBox)ctr;
            foreach (int checkedItemIndex in clb.CheckedIndices)
            {
                clb.SetItemChecked(checkedItemIndex, false);
            }
        }
        else if (ctr is CheckBox)
        {
            ((CheckBox)ctr).Checked = false;
        }
        else if (ctr is ComboBox)
        {
            ((ComboBox)ctr).SelectedIndex = 0;
        }
    }
    

    I dont know what Deneyim and Not are, but i guess you get the idea of checking what it is and asigning the value you want