Home All Groups Group Topic Archive Search About
Author
10 Mar 2006 8:34 AM
Alex Bibiano
Question about .net 2.0 and C#:



In the open event of my WinForms I do a loop trough all the controls
collection.


I have inherited new controls from the base controls (button, label, .), all
with a new property (security).


Now, I have modified the loop in the open event to check if the control has
the property set to true.



I have used this code:



private void loopControls(Control.ControlCollection ctrls)

{

foreach(Control ctr in ctrls)

{

loopControls (ctr.Controls);

if (ctr.GetType() == typeof(customButton))

{

if (((customButton)ctr).Security == true)

{

// Make something

}

}

}

}





The problem is that now I have 4 or more customControls and I have to check
in the if the type for all the diferent controls.



¿Is there a way to do it more elegant?



I have tried to inherite all my controls from an interface and chek if the
control implment the interface, but dosen't work (a interface can't have a
property). Note: All the custom control unherit from base controls.



A lot of thanks.



Alex B.

Author
10 Mar 2006 9:42 AM
Frans Bouma [C# MVP]
Alex Bibiano wrote:

Show quoteHide quote
> Question about .net 2.0 and C#:
> In the open event of my WinForms I do a loop trough all the controls
> collection.
>
> I have inherited new controls from the base controls (button, label,
> .), all with a new property (security).
>
>
> Now, I have modified the loop in the open event to check if the
> control has the property set to true.
>
> I have used this code:
>
> private void loopControls(Control.ControlCollection ctrls)
> {
>     foreach(Control ctr in ctrls)
>     {
>     loopControls (ctr.Controls);
> if (ctr.GetType() == typeof(customButton))
> {
> if (((customButton)ctr).Security == true)
> {
> // Make something
> }
> }
> }
> }
>
> The problem is that now I have 4 or more customControls and I have to
> check in the if the type for all the diferent controls.
>
> ¿Is there a way to do it more elegant?
>
> I have tried to inherite all my controls from an interface and chek
> if the control implment the interface, but dosen't work (a interface
> can't have a property). Note: All the custom control unherit from
> base controls.

    Of course interfaces can have property definitions!
public interface IFoo
{
    int Count { get; }
}

public class Foo: IFoo
{
    //...

    public int Count
    {
        get { return _count; }
    }
}

    You should define an interface: (this is an example, to show you how
it's done)

public interface ISecurityProvider
{
    bool Security { get;}
}   

then in your control code:
public class MyButton : Button, ISecurityProvider
{
    //..

    public bool Security
    {
        get { return _security;}
    }
}

    and in your form:

private void loopControls(Control.ControlCollection ctrls)
{
    foreach(Control ctr in ctrls)
    {
        loopControls (ctr.Controls);
        ISecurityProvider controlSecurity =
                ctr as ISecurityProvider;
        if((controlSecurity!=null) && controlSecurity.Security)
        {
            // make something
        }
    }
}

        FB


--
------------------------------------------------------------------------
Lead developer of LLBLGen Pro, the productive O/R mapper for .NET
LLBLGen Pro website: http://www.llblgen.com
My .NET blog: http://weblogs.asp.net/fbouma
Microsoft MVP (C#)
------------------------------------------------------------------------
Are all your drivers up to date? click for free checkup

Author
10 Mar 2006 10:01 AM
Alex Bibiano
A lot of thanks. It was very helpfull


Show quoteHide quote
"Frans Bouma [C# MVP]" <perseus.usenetNOSPAM@xs4all.nl> escribió en el
mensaje news:xn0ejk8uw5kg4v004@news.microsoft.com...
> Alex Bibiano wrote:
>
>> Question about .net 2.0 and C#:
>> In the open event of my WinForms I do a loop trough all the controls
>> collection.
>>
>> I have inherited new controls from the base controls (button, label,
>> .), all with a new property (security).
>>
>>
>> Now, I have modified the loop in the open event to check if the
>> control has the property set to true.
>>
>> I have used this code:
>>
>> private void loopControls(Control.ControlCollection ctrls)
>> {
>> foreach(Control ctr in ctrls)
>> {
>> loopControls (ctr.Controls);
>> if (ctr.GetType() == typeof(customButton))
>> {
>> if (((customButton)ctr).Security == true)
>> {
>> // Make something
>> }
>> }
>> }
>> }
>>
>> The problem is that now I have 4 or more customControls and I have to
>> check in the if the type for all the diferent controls.
>>
>> ¿Is there a way to do it more elegant?
>>
>> I have tried to inherite all my controls from an interface and chek
>> if the control implment the interface, but dosen't work (a interface
>> can't have a property). Note: All the custom control unherit from
>> base controls.
>
> Of course interfaces can have property definitions!
> public interface IFoo
> {
> int Count { get; }
> }
>
> public class Foo: IFoo
> {
> //...
>
> public int Count
> {
> get { return _count; }
> }
> }
>
> You should define an interface: (this is an example, to show you how
> it's done)
>
> public interface ISecurityProvider
> {
> bool Security { get;}
> }
>
> then in your control code:
> public class MyButton : Button, ISecurityProvider
> {
> //..
>
> public bool Security
> {
> get { return _security;}
> }
> }
>
> and in your form:
>
> private void loopControls(Control.ControlCollection ctrls)
> {
>  foreach(Control ctr in ctrls)
>  {
> loopControls (ctr.Controls);
> ISecurityProvider controlSecurity =
> ctr as ISecurityProvider;
> if((controlSecurity!=null) && controlSecurity.Security)
> {
> // make something
> }
> }
> }
>
> FB
>
>
> --
> ------------------------------------------------------------------------
> Lead developer of LLBLGen Pro, the productive O/R mapper for .NET
> LLBLGen Pro website: http://www.llblgen.com
> My .NET blog: http://weblogs.asp.net/fbouma
> Microsoft MVP (C#)
> ------------------------------------------------------------------------

Bookmark and Share