• Now Online : 81
  • admin@codemyne.net

Introduction

When predefined controls are not providing users requirement, then user defined controls need to be developed.
To develop user defined controls, Visual Studio providing WindowsControl Library project template. When WCL Project is opened, then a borderless form will be opened, where the user defined control need to be developed.

User defined controls are form dependent. It is a class which must be derived from user control class.
User controls are divided into two type.

  • General user controls.
  • Inherited user controls.

User controls are reusable.User controls developed in c#.net can be used in vb.net but not in asp.net

Working with User Defined Property

A property is a collection of set and get functions. A property only with Get function is called as Read-only property.

Example: Developing a scrollable user control with LText, LFont, LBColor, LSpeed properties

Open windows control library project with project name scrolling label. Choose properties and set name as 'Scrolling Label'. This text will be displayed in toolbox after adding this dll file to controls toolbox.

Place a Label and two timer controls on to the form.
Copy and paste the code on to usercontrol1.cs file.

public string LText
{
    set 
    {
        label1.Text = value;
    }
    get
    {
        return label1.Text;
    }
}
 public Color LBColor
{
    set
    {
        label1.BackColor = value;
    }
    get
    {
        return label1.BackColor;
    }
}
public Font LFont
{
    set
    {
        label1.Font = value;
    }
    get
    {
        return label1.Font;
    }
}
public int LSpeed
{
    set
    {
        timer1.Interval = value;
        timer2.Interval = value;
    }
    get
    {
        return timer1.Interval;
    }
}
private void timer1_Tick(object sender, EventArgs e)
{
    label1.Top = label1.Top - 4;
    if (label1.Top < 0)
    {
        timer1.Enabled = false;
        timer2.Enabled = true;
    }
}
private void timer2_Tick(object sender, EventArgs e)
{
    label1.Top = label1.Top + 4;
    if (label1.Top >200)
    {
        timer1.Enabled = true;
        timer2.Enabled = false;
    }
}

Build the project. A dll will be created with name 'ScrollingLabel.dll'

Now open a new windows application project. Add 'ScrollingLabel.dll' into ToolBox. Place this control on the form and check user defined properties.

Happy coding..

Comments Post a Comment

Raju 8/7/2010 (IST) / Reply

Simple explanation. Easy to understand

divya 5/30/2011 (IST) / Reply

excellent work..