Dynamically changing the color's of control's on a form






1.70/5 (9 votes)
May 30, 2007

31276

522
we can change the textBox,Label's,panel's etc color properties at the runtime
Introduction
The code provided will solve the problem of every time changing the properties of controls like fore color, background etc.Basically we encounter a problem like every time we have to go to the properties and change the properties for color whenever we design a new form. this code easily solves the above encountered situation.
Background
We had encountered this problem when ever we had to design several number of forms.
Using the code
What you need to do is just copy paste the code whenever you came through this situation and you will see that you will see that forms background and forecolor will change at runtime dynamically with inserting a single word on it.
//<code> written in vb.net // Public Sub disp(ByVal a As Control) a.ForeColor = Color.FromKnownColor(KnownColor.ControlText) a.BackColor = Color.FromKnownColor(KnownColor.Control) For Each ctrl As Control In a.Controls If TypeOf ctrl Is Panel Then disp(ctrl) End If If TypeOf ctrl Is Button Then CType(ctrl, Button).FlatStyle = FlatStyle.Flat CType(ctrl, Button).ImageAlign = ContentAlignment.MiddleLeft End If ctrl.ForeColor = Color.FromKnownColor(KnownColor.ControlText) ctrl.BackColor = Color.FromKnownColor(KnownColor.Control) If TypeOf ctrl Is TextBox Or TypeOf ctrl Is ComboBox Then ctrl.ForeColor = Color.FromKnownColor(KnownColor.WindowText) ctrl.BackColor = Color.FromKnownColor(KnownColor.Window) End If Next End Sub //
Points of Interest
This code can be utilised in any .net frame work environment you need