michael_hk Posted March 9, 2005 Posted March 9, 2005 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 Quote There is no spoon. <<The Matrix>>
mskeel Posted March 9, 2005 Posted March 9, 2005 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. Quote
Tygur Posted March 9, 2005 Posted March 9, 2005 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. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.