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.