Tuesday, April 7, 2009

Add a property to user control


The .NET Framework has extensive support for programmers to create custom user controls. When you create a user control, also known as custom control, you can add a custom property to the property page that allow the user to modify the properties you created. Let's take a look at how to achieve this goal step-by-step:

Known issues
:
Assume that we create a winform project and a user control which contains a button only. This user control is called "MyUserButton.cs". We want to add a custom property called "TreeView" that should appear on the "Properties" panel.

Detail
:
1. In the Solution Explorer panel, right click on your user control "MyUserButton.cs" file and select "View Code".

2. In the class "MyUserButton.cs", create a private variable "treeView" with type "TreeView".


private TreeView treeView;

3. Then type the following code beneath the variable declarations from last step:


public TreeView MyTreeView
{
get { return treeView; }
set { treeView = value; }
}


4. Save the file and add the "MyUserButton" control to the main window. On the Properties grid panel we can find a property named "TreeView" that is what we just created.


Summary

There are some more advance features about adding a property to the user control. Here is just a simple example that giving you a brief idea on it. For more information, the article "How To Add a Custom Font Property to a User Control" is also useful.

comments

0 Responses to "Add a property to user control"

Post a Comment

BlogUpp!

 

Copyright 2009 All Rights Reserved Revolution Two Lifestyle theme by Brian Gardner | Blogger template converted & enhanced by eBlog Templates