0

I would like to disable all except for the panel shown below

enter image description here

The triggering of this panel is when i clicked edit button on my datagridview and the panel would be visible.. So basically the panel is not visible in default (here is the code below of the edit button in the datagrid)

    private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
    {
        var senderGrid = (DataGridView)sender;

        if (senderGrid.Columns[e.ColumnIndex] is DataGridViewButtonColumn &&
            e.RowIndex >= 0)
        {

            panel1.Visible = true;
            selectedItenOutputOrderTabGrid.Rows.RemoveAt(selectedItenOutputOrderTabGrid.CurrentRow.Index);

              // HERE I WANT TO ADD A FUNCTION THAT WOULD DISABLE ALL IN THE FORM EXCEPT THE PANEL
        }

    }
Jon
  • 384
  • 6
  • 18
  • possible duplicate of: http://stackoverflow.com/questions/13446129/how-to-disable-all-controls-on-the-form-except-for-a-button – Kaitlyn Sep 04 '15 at 07:07
  • the following link creates another private void which is insufficient for me – Jon Sep 04 '15 at 07:10
  • 1
    Why don't you just show a modal dialog? – György Kőszeg Sep 04 '15 at 07:11
  • You only need to create two additional methods though as according to http://stackoverflow.com/a/13446223/3472690, one which disables all controls, then you just use the enable controls to enable the panel, as well as controls within it. Or what taffer said. – Kaitlyn Sep 04 '15 at 07:13

5 Answers5

2
foreach (Control child in this.Controls)
{
    if (child != panel1)
        child.Enabled = false;
}

Make sure your panel1 is a direct child on the form. Open the Document Outline window. You should see something like this:

Document outline

Or, make a new form and show it with ShowDialog(). You can make it borderless if you wish...

György Kőszeg
  • 17,093
  • 6
  • 37
  • 65
2

To disable all child controls of form except your panel panel1, try this:

foreach (var control in Controls.Cast<Control>().Where(ctrl => ctrl.Name != "panel1"))
{
    control.Enabled = false;
}
NASSER
  • 5,900
  • 7
  • 38
  • 57
  • This disabled all of controls in my forms even the numericUpDown and save and cancel button – Jon Sep 04 '15 at 07:20
  • @andy According to screen shot, the best way is that the panel should be child of form not of gridview. Otherwise OP have to enable gridview as well – NASSER Sep 04 '15 at 07:25
  • Disabled buttons from my panel :( – Jon Sep 04 '15 at 07:30
  • @JonBonJoviMateo OK, enable your button and numeric updown manually or loop on it after disabling all – NASSER Sep 04 '15 at 07:31
  • control.Enabled = false; button6.Enabled = true; button9.Enabled = true; numericUpDownEdit.Enabled = true; ---- Also not working ---- – Jon Sep 04 '15 at 07:39
  • 1
    BTW, why are you not using a Dialog for this instead of Panel? You can hide its border and can achieve same look as panel – NASSER Sep 04 '15 at 07:45
2

I tested this, and it disables all other controls, while leaving panel1 and the controls within still enabled. This is code modified from https://stackoverflow.com/a/13446223/3472690

foreach (Control c in this.Controls)
{
    c.Enabled = false;
}


panel1.Parent.Parent.Enabled = true;
foreach (Control c in panel1.Parent.Parent.Controls)
{
     c.Enabled = false;
}

panel1.Parent.Enabled = true;
foreach (Control c in panel1.Parent.Controls)
{
     c.Enabled = false;
}

panel1.Enabled = true;

What this code does, is pretty simple:

First, the first foreach disables all controls in the form, but not the form itself. Then, it enables the panel's parent's parent, aka the tabcontrol itself, then disables everything in the tab control.

It then enables the tab page that your panel is in, and disables everything else in the tab page.

Finally, it enables the panel itself. This is not a very optimised piece of code, but it works.

enter image description here

That means, as according to the picture above, tabControl1 itself is enabled, but everything in it is disabled except for the tabPage1 that the panel1 is on, while everything in tabPage1 except for panel1 is disabled.

EDIT:

To reverse the disabling of all other controls, just do:

foreach (Control c in this.Controls)
{
    c.Enabled = true;
}

panel1.Enabled = false;
Community
  • 1
  • 1
Kaitlyn
  • 791
  • 1
  • 10
  • 28
  • The first foreach disables every direct child on the form. The second one is completely needless unless they were disabled in the designer. The last line re-enables the panel, which has been disabled in the first foreach. – György Kőszeg Sep 04 '15 at 07:33
  • This also disabled all my controls and also buttons from my panel – Jon Sep 04 '15 at 07:34
  • @JonBonJoviMateo , I hate to ask, but is that panel1 in another groupbox or panel? – Kaitlyn Sep 04 '15 at 07:35
  • Yes.. it is in another tab Panel – Jon Sep 04 '15 at 07:42
  • @JonBonJoviMateo That explains it then. Try my updated code then, it may help. Or taffer's first. – Kaitlyn Sep 04 '15 at 07:44
0
private void ShowFilter(Control parentControl, Control childControl, bool enable)
    {
        foreach (Control control in parentControl.Controls.Cast<Control>().Where(ctrl => ctrl.Name != childControl.Name))
        {
            control.Enabled = !enable;
        }
        childControl.Visible = enable;
    }
vavtech
  • 41
  • 3
0
private void Drucken_Click(object sender, EventArgs e)
{
  foreach (Control c in this.Controls)
  {
    if(!c.HasChildren)
    { 
      c.Enabled = false;
    }
    foreach(Control d in tabPage1.Controls)
      if(!d.HasChildren)
      {
        d.Enabled = false;
      }
    foreach(Control t in panel1.Controls)
    {
      if( t is TextBox)
      {
        t.Enabled = false;
      }
      Drucken.Enabled = false; 
    }
  }
Ovy78
  • 1
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Sep 13 '22 at 16:55