Click here to Skip to main content
15,912,204 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hi C# Gurus,

I have created my own custom Textbox control which I intend to use on every form in my application rather than the standard Textbox control.
One of the things I do when the app starts is to call a common routine/method/procedure (call it what you will) which iterates thru all the controls on the form, settings it's colours according to the user selected colour scheme. This all works fine for the standard window's controls, but not for the custom control's new property: FocusColor. I'm using a pretty standard loop to iterate thru the controls:
C#
foreach(Control c in f.Controls){}
When it hits the custom textbox control, setting the Back and ForeColor properties works fine, but I can't set the new FocusColour property because it doesn't appear in intellisense and the program won't run (shows errors) if I manually type the property i.e.
C#
c.FocusColour = Colors.Yellow;
My question is: How can I iterate thru all the custom controls on the form to set the new properties, or how can I include the custom properties once a custom control is found in the above loop?

Thanks in advance,
Walter
Posted
Comments
Sergey Alexandrovich Kryukov 19-Mar-12 0:42am    
There is no such propery, Control.FocusColor (not just "does not appear in Intellisense", who cares about Intellisense if it does not exist at all). What do you mean? Normally, focusing is not related to color.

Besides, your foreach look is not recursive, but it should be.

What is exact type of Control? I mean System.Windows.Forms.Control? System.Windows.Controls.Control? Anything else?
Oh, I see, you mentioned "form". It's hard to notice. You should tag it: Forms or WPF, ASP.NET, etc...

--SA
wkiess01 19-Mar-12 0:48am    
The custom Textbox control has a new property called FocusColour. And yes, I know the foreach loop should be recursive - Thanks - I was trying to keep the question concise, i.e. How to iterate the form for either custom controls only and setting the custom control's properties, or including custom controls in a generic foreach loop.
Walter

1 solution

Maybe this could help you:
C#
foreach (Control c in this.Controls)
{
   if (c is CustomControl)
      ((CustomControl)c).FocusColour = Color.Yellow;
}
 
Share this answer
 
Comments
wkiess01 19-Mar-12 1:23am    
Thank you. That made it work.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900