Create User Controls in C#

Working with User Controls 

1)user defined controls are reusable 

2)user defined controls contains reusable logic and reusable design 

3)assembly contains only reusable logic but not design 

4)UDC supports language interoperability i.e, The UDC developed in C# can be used to VB.NET and viceversa, But these controls does not works in asp.net 

5)user defined control will be deployed as a DLL file 

6)to develop UDC, .Net introduced “windows forms control library” project template 

7)when windows forms control library project is opened, then a border-less window will be opened, Which is called as a container 

8)user defined controls are divided into 2 types 

        a)general user control 

        b)inherited user control 

9)if the parent class name is usercontrol, Then it is called as general user control 

10)if the parent class name is any existing control, then it is called as inherited user control 

11)a collection of set and get methods is called as user defined property

 

Syntax to write a property 

int x; 

public int pname 

{ 

Set 

{ 

    x=value; 

} 

Get 

{ 

x=return x; 

} 

}

 

Four steps for developing user defined control 

Step1: 

Open windows forms controls library project, Then design it write the code as required 

 

Step2: 

Add an ICON(optional), Then build the project to get a DLL file 

Note:after step2, the control is ready to reuse

 

Step3: 

Steps for reusing the control

 

Open windows forms application project, Then add the above DLL file into the toolbox. Then a new control will be added into the toolbox

 

Step4: 

Place this newly added control on the form

 

using System; 

using System.Collections.Generic; 

using System.ComponentModel; 

using System.Drawing; 

using System.Data; 

using System.Linq; 

using System.Text; 

using System.Windows.Forms; 

using System.Drawing.Drawing2D;

 

namespace animation_example 

{ 

    public partial class companytitle : UserControl 

    { 

        public companytitle() 

        { 

            InitializeComponent(); 

        }

 

        string s = "microsoft"; 

        Font f = new Font("algerian", 40); 

        Color cs = Color.Red; 

        Color ce = Color.Yellow; 

        Graphics g; 

        int x = 1; 

 

        public string cname 

        { 

            set 

            { 

                s = value; 

                g.Clear(Color.Black); 

            } 

            get 

            { 

                return s; 

            } 

        }

 

        public Color cstartcolor 

        { 

            set 

            { 

                cs = value; 

            } 

            get 

            { 

                return cs; 

            } 

        }

 

        public Color cEndcolor 

        { 

            set 

            { 

                ce = value; 

            } 

            get 

            { 

                return ce; 

            } 

        }

 

        public Font cFont 

        { 

            set 

            { 

                f = value; 

                g.Clear(Color.Black); 

            } 

            get 

            { 

                return f; 

            } 

        }

 

        public int cspeed 

        { 

            set 

            { 

                timer1.Interval = value; 

            } 

            get 

            { 

                return timer1.Interval; 

            } 

        }

 

        private void companytitle_Load(object sender, EventArgs e) 

        { 

            g = this.CreateGraphics(); 

            timer1.Enabled = true; 

        }

 

        private void timer1_Tick(object sender, EventArgs e) 

        { 

            x = x + 3; 

            if (x > 400) 

            { 

                x = 0; 

            }

 

            LinearGradientBrush br = new LinearGradientBrush(new Point(100, x), new Point(x, 300), cs, ce); 

            g.DrawString(s, f, br, 10, 10); 

        } 

    } 

}

 

build the project (build menu->build solution)

 

observation: then animation example, dll is generated under d:\foldername\animationexample\bin\debug folder with a control called as company title

 

testing the control 

open windows form application project 

open toolbox->rightclick in side of toolbox->add tab->then give the name as mycontrols 

then rightclick inside of mycontrols tab->choose items->browse->animationexample.dll 

                       (or) 

Drag animationexample.dll into mycontrols tab 

Observation:then a new control called as company title will be added into the toolbox 

Place company title control on the form, then check ctext, cstart color, cend color, font and cspeed properties