C#:Problem in disabling all buttons in a form except few using a method.
-
hi all can any one help me how to disable all buttons in a form except few using a method. I coded to disable all buttons as:
private void disable_buttons() { foreach (Control ctrl in this.Controls) { if (ctrl.GetType() == typeof(Button)) { ctrl.Enabled = false; } } }
but when called from same class load event it doesnt works? pls suggest me for this problem?private void UserMaster_Load(object sender, EventArgs e) { disable_buttons(); }
thank uKssk
-
hi all can any one help me how to disable all buttons in a form except few using a method. I coded to disable all buttons as:
private void disable_buttons() { foreach (Control ctrl in this.Controls) { if (ctrl.GetType() == typeof(Button)) { ctrl.Enabled = false; } } }
but when called from same class load event it doesnt works? pls suggest me for this problem?private void UserMaster_Load(object sender, EventArgs e) { disable_buttons(); }
thank uKssk
Are you calling it before the initialize method which creates the controls and also enables them ? It should work, it will disable all buttons, you need to reenable the ones you don't want to disable.
Christian Graus - Microsoft MVP - C++ "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
-
hi all can any one help me how to disable all buttons in a form except few using a method. I coded to disable all buttons as:
private void disable_buttons() { foreach (Control ctrl in this.Controls) { if (ctrl.GetType() == typeof(Button)) { ctrl.Enabled = false; } } }
but when called from same class load event it doesnt works? pls suggest me for this problem?private void UserMaster_Load(object sender, EventArgs e) { disable_buttons(); }
thank uKssk
this might have the problem that buttons inside panels or frames are not disabled (just use a recursive walker). Concerning your question: as was said before: disable all and then enable the ones you like or give every button a describing Tag (for example a groupname or whatever) and check for this Tag in you foreach-loop to only disable the ones in the given group.
-
hi all can any one help me how to disable all buttons in a form except few using a method. I coded to disable all buttons as:
private void disable_buttons() { foreach (Control ctrl in this.Controls) { if (ctrl.GetType() == typeof(Button)) { ctrl.Enabled = false; } } }
but when called from same class load event it doesnt works? pls suggest me for this problem?private void UserMaster_Load(object sender, EventArgs e) { disable_buttons(); }
thank uKssk
if you replace
if (ctrl.GetType() == typeof(Button))
byif (ctrl is Button)
you get simpler code, that recognizes Buttons as well as controls that inherit from Button. :)Luc Pattyn [Forum Guidelines] [My Articles]
Sorry for any delays in replying, I currently don't always get e-mail notifications.