Click here to Skip to main content
13,354,132 members (57,643 online)
Click here to Skip to main content
Add your own
alternative version


15 bookmarked
Posted 30 May 2007

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

, 30 May 2007
Rate this:
Please Sign up or sign in to vote.
we can change the textBox,Label's,panel's etc color properties at the runtime


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.


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
//   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
            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
    End Sub

Points of Interest

This code can be utilised in any .net frame work environment you need


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Ravindra Rayudu
Web Developer
India India
Presently working in Esoft consulting as a .net developer

You may also be interested in...


Comments and Discussions

GeneralMy vote of 1 Pin
BooGhost23-Dec-09 9:15
memberBooGhost23-Dec-09 9:15 
Newssimply superb article excellent article Pin
Ravindra Rayudu8-Sep-07 2:53
memberRavindra Rayudu8-Sep-07 2:53 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.180111.1 | Last Updated 30 May 2007
Article Copyright 2007 by Ravindra Rayudu
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid