Disable specific controls placed on panel

414 views Asked by At

I am new in Windows Forms Applocation. I am using Visual Studio 2010 with .NET framework 4.0. In my project, I used a Panel to place controls such as TextBox, DatePicker, CheckBox, ComboBox and Button. Here I want to disable only SPECIFIC CONTROLS when I make that panel Visible. How would I achieve this?? I searched a lot on google but it doesn't give me some relevant results.

3

There are 3 answers

0
Hassan On

You can do something like that:

 foreach (Control ctl in panel.Controls)
 {
    if (ctl.Name == "textBox1")
    {
           ctl.Enabled = false;
    }
 }

You can apply switch case on control type within foreach loop as well, to know whether the control is TextBox, DatePicker etc.

0
maddy11 On

let take an example of button as your specific control.

void button1_Click(object s ,Eventargs e)
{
  button1.Enabled= false;
  panel.visible= true;    
}
0
AudioBubble On
bool canEdit = false

private void panel1_VisibleChanged(object sender, EventArgs e)
        {
            button1.Enabled = false
            checkbox1.Enabled = false
            //...
        }

and you can define a bool variable to check what to do.like below:

        bool canEdit = false;

        private void panel1_VisibleChanged(object sender, EventArgs e)
        {
            if (!canEdit)
            {
                button1.Enabled = false;
                //and other components that u want
            }
            else
            {
                button1.Enabled = true;
                //and other components that u want
            }
        }