Click here to Skip to main content
Click here to Skip to main content

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

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

License

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

Share

About the Author

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

Comments and Discussions

 
GeneralMy vote of 1 PinmemberBooGhost23-Dec-09 8:15 

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

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

| Advertise | Privacy | Mobile
Web04 | 2.8.141029.1 | Last Updated 30 May 2007
Article Copyright 2007 by Ravindra Rayudu
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid