Click here to Skip to main content
15,881,248 members
Articles / Programming Languages / Visual Basic
Article

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

Rate me:
Please Sign up or sign in to vote.
1.70/5 (9 votes)
30 May 2007 30.8K   518   15   2
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


Written By
Web Developer
India India
Presently working in Esoft consulting as a .net developer

Comments and Discussions

 
GeneralMy vote of 1 Pin
DanWalker23-Dec-09 8:15
DanWalker23-Dec-09 8:15 
Newssimply superb article excellent article Pin
Ravindra Rayudu8-Sep-07 1:53
Ravindra Rayudu8-Sep-07 1: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.