|
ms
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Desing problemIn 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. Alex Bibiano wrote:
Show quoteHide quote > Question about .net 2.0 and C#: Of course interfaces can have property definitions!> 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. 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#) ------------------------------------------------------------------------ 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#) > ------------------------------------------------------------------------
Other interesting topics
newbie: GetType or typeof?
Effective strategy for using VSS in a development team? How to Anonymous IP address in C# Getting Event of Lock Windows OS Why won't you give me your scrollbars?! I hate you, TreeView! Application Idle - Good or bad practice? How to: Access the Managed HTML Document Object Model C++ class in DLL to be imported to C# Getting text box value on key press? file uploading in windows application in .net |
|||||||||||||||||||||||