Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Hi guys,

 

Say you need to develop a complex winform which contains many buttons, textboxes, labels, datagrids etc. When the user clicks on button A, you need to enable/disable, set the visibility of some other controls. For button B, you do similar stuff.

 

 

private void btn_A_Click(object sender, System.EventArgs e)
{
ControlA.enable = true;
ControlB.enable = false;
...
}

I find this style of programming is error-prone, ie very easily you will forget to handle some of the controls. Worst still, if you later need to add an extra control, you need to insert

 

 

private void btn_A_Click(object sender, System.EventArgs e)
{
ControlA.enable = true;
ControlB.enable = false;
...
NewControl.enable = true; // handle the new control
}

everywhere in the (eg. Click, Changed) events.

 

My question is how you guys handle this problem?

 

Any idea/comment is appreciated.

 

Cheers, :)

Michael

There is no spoon. <<The Matrix>>
Posted

One way I've handled this is by making a single method that handles all the enabling and disabling.

 

For example, you would make a method called something like SetButtons(bool enabled) and toggle everything by calling that method with the paramater of either true or false.

Posted

Well, if you need to enable/disable/whatever all the controls on a certain Panel or something, you can just loop through all its controls:

Dim Ctrl As Control
For Each Ctrl In MyPanel.Controls
   Ctrl.Enabled = False
Next

 

Otherwise, you can add similar controls to an Array or ArrayList or something, and then just loop through that.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...