Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm General / March 2007

Tip: Looking for answers? Try searching our database.

How to disable all buttons in a window form?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Andrew - 22 Mar 2007 15:11 GMT
Hello friends,

I tried to use the following code to disable all command buttons in a
windowform, but it was complained when compiling, saying The given expression
is never of the provided ("System.Windows.Forms.Button") type, unreachable
code.

           foreach (Control ctrl in this.Controls)
           {
               if (ctrl.GetType() is Button)
               {
                   ((System.Windows.Forms.Button)ctrl).Enabled = false;
               }
           }

So, my questions are:

(1) Why such message?
(2) If this is not right, then how to do the job (disable all buttons)
correctly?

Thanks a lot.
VJ - 22 Mar 2007 15:28 GMT
Its not this code,  "unreachable code detected...", occurs when you have a
executing statment beyond a return statment within a method... I am sure
this is not the only code you have in the Form, and how is the below called
for disable? C# in VisualStudio if you complie, you will get a line number
beoynd where its not reachable. Look for it.

VJ

> Hello friends,
>
[quoted text clipped - 19 lines]
>
> Thanks a lot.
Andrew - 22 Mar 2007 16:06 GMT
line number indicates this statement:

((System.Windows.Forms.Button)ctrl).Enabled = false;

> Its not this code,  "unreachable code detected...", occurs when you have a
> executing statment beyond a return statment within a method... I am sure
[quoted text clipped - 27 lines]
> >
> > Thanks a lot.
VJ - 22 Mar 2007 16:18 GMT
alright sorry about this..

crtl.Getype() <-- will return object and using "is" will never evaluate to
button.. change as below

if (ctrl is Button)

HTH
VJ

> line number indicates this statement:
>
[quoted text clipped - 35 lines]
>> >
>> > Thanks a lot.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.