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

A control for skinning groups of controls

, 18 Dec 2006 CPOL
Rate this:
Please Sign up or sign in to vote.
A control used to mass change other controls' properties.

Sample screenshot

Introduction

What is a skin control, you may ask? A skin control is a control that can change the properties of many other controls quickly. For instance, in an application, you want to have the ability to have a green color scheme, a blue color scheme, etc. By using this control, you can specify the property (either by name or built-in) and what you want to set the property as. Quick, easy, and saves a ton of code. Here's how...

How it Works

How does this code work. It is actually quite simple. Included are a couple of methods to change some common properties like Name, Backcolor, Forecolor, etc. Along with these, you can also say you only want certain types to change, like only change the Forecolor of controls of type Button. But what if you want to change the property of a custom control? Or one that isn't provided? Here is where Reflection comes in.

During run time, I don't explicitly know the type of the object given. So I have to use Reflection to get it, like so:

Dim objType As Type = ctrl.GetType()

This creates an object objType. The type of objType is that of the control ctrl. Then, we can get the property we want, given its name.

Dim propInfo As PropertyInfo = objType.GetProperty(PropertyName)

This gets the PropertyInfo of that object. By passing in the name of the property we want to change, we can get or set the property. All that's left is to change the property.

propInfo.SetValue(ctrl, val, Nothing)

ctrl is the control whose property we want to change. val is the new value. Nothing (null) is used for the index in the case of an array. I just threw this together, so I did not add the ability to change this. If there is enough demand, I can.

But Wait, There's a Catch...

In order to change the properties of the control, it must be passed ByRef(erence) to the skin control. This means that the skin control still hold a reference so the control cannot be GC'd. Calling the RemoveControl method will remove the reference to the control. That's about it. Hope this can teach a little bit about Reflection.

License

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

Share

About the Author

iwdu150

United States United States
I am iwdu15, a college frosh who will be majoring in Computer Science. I use VB.Net, C#, C++, J#, Java, and some C.

Comments and Discussions

 
GeneralMy vote of 1 PinmemberMohammadAmiry3-Apr-10 5:47 
GeneralShortcomings Pinmembermmansf19-Dec-06 5:21 
GeneralRe: Shortcomings Pinmemberiwdu15019-Dec-06 11:37 

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 | Terms of Use | Mobile
Web04 | 2.8.141223.1 | Last Updated 18 Dec 2006
Article Copyright 2006 by iwdu150
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid