473,385 Members | 1,901 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Desing problem

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.
Mar 10 '06 #1
2 1362
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#)
------------------------------------------------------------------------
Mar 10 '06 #2
A lot of thanks. It was very helpfull
"Frans Bouma [C# MVP]" <pe******************@xs4all.nl> escribió en el
mensaje news:xn***************@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#)
------------------------------------------------------------------------

Mar 10 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

117
by: Peter Olcott | last post by:
www.halting-problem.com
28
by: Jon Davis | last post by:
If I have a class with a virtual method, and a child class that overrides the virtual method, and then I create an instance of the child class AS A base class... BaseClass bc = new ChildClass();...
2
by: Ed_P. | last post by:
Hello, Up to this point, I've been using C# to create simple applications that only use on Project to store Windows Forms and other Files. The project has been complied into an EXE. The exe file...
3
by: ahaupt | last post by:
Hi all, At the moment we use static methods for retrieving data: Ex. DataTable supplierDT = DAC.Supplier.GetAll(); DataTable customerDT = DAC.Customer.GetAll(); The reason we use static...
0
by: .:: MaStErDoN ::. | last post by:
Hi! I have a problem and i don't know how to fix it! When i insert a MainMenu i can't type the name in the label "Write here", it just makes the form shorter everytime i type a letter, i've try a...
56
by: Luke Matuszewski | last post by:
I am designing the library, which will hidden all its functions within singleton object ... So for clients they will use it like . eg. system.getElementWithId('ruler'); At library side, i...
0
by: Flawio_83 | last post by:
I have an ADSL modem and ipcam. I want to desing a web page in ASP.NET or with using any other technologies, to watch my ipcam on the internet, from everyplaces in the world. I'll be glad to hear...
0
by: gil | last post by:
Hi, I'm a VB newbi and therefor apologize for the ignorance. I'd like to design a simple from in which there are few checkboxes - when singned, the related e-mail address is added to the Bcc. I...
7
freddieMaize
by: freddieMaize | last post by:
Hi, my first day in designing. Since I’m not a very good designer I tried copying a template from the net. Below is my problem. I saved the below web page,...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.