473,819 Members | 3,025 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(Co ntrol.ControlCo llection ctrls)

{

foreach(Control ctr in ctrls)

{

loopControls (ctr.Controls);

if (ctr.GetType() == typeof(customBu tton))

{

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 1384
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(Co ntrol.ControlCo llection ctrls)
{
foreach(Control ctr in ctrls)
{
loopControls (ctr.Controls);
if (ctr.GetType() == typeof(customBu tton))
{
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 ISecurityProvid er
{
bool Security { get;}
}

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

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

and in your form:

private void loopControls(Co ntrol.ControlCo llection ctrls)
{
foreach(Control ctr in ctrls)
{
loopControls (ctr.Controls);
ISecurityProvid er controlSecurity =
ctr as ISecurityProvid er;
if((controlSecu rity!=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.n l> escribió en el
mensaje news:xn******** *******@news.mi crosoft.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(Co ntrol.ControlCo llection ctrls)
{
foreach(Control ctr in ctrls)
{
loopControls (ctr.Controls);
if (ctr.GetType() == typeof(customBu tton))
{
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 ISecurityProvid er
{
bool Security { get;}
}

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

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

and in your form:

private void loopControls(Co ntrol.ControlCo llection ctrls)
{
foreach(Control ctr in ctrls)
{
loopControls (ctr.Controls);
ISecurityProvid er controlSecurity =
ctr as ISecurityProvid er;
if((controlSecu rity!=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
7278
by: Peter Olcott | last post by:
www.halting-problem.com
28
5229
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(); .... and then call the virtual method, why is it that the base class's method is called instead of the overridden method? How do I fix this if I don't know at runtime what the child class is? I'm using Activator.CreateInstance() to load the...
2
1736
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 that is generated has balooned to about 500kb, which is something I don't want to happen! Although this has approach has been ok for the small applications I've built so far I'd like to move to using the "One Solution with Multiple Projects"...
3
1425
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 methods is that it's not really worth the
0
765
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 lot of things as changing MainMenu properties but i can't sove it. what's the matter? Thanks! Andres P.S. Sorry my english, i speak spanish
56
4008
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 will use constructs like follows (at global scope) eg.
0
1318
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 from you soon. Thanks, Ali
0
1278
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 created a check box, using the "filed chooser" but could'nt see how to attached any VB code to the checkbox. can anyone please help me with this problem???
7
2182
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, http://osc3.template-help.com/cre_20885/ It got saved as an html page and a folder with images (which is usual). I then changed the image folder as ‘images’ from ‘CRE Loaded 6 - CRE Loaded Store_files’ and also changed the path in the <img> tag’s ‘scr’ accordingly. Also I changed...
0
9752
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10435
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10463
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7702
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6911
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5590
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5725
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4381
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3049
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.