Step: 1
- Create a Project.
- Select "Class Library" in the Project Template.
- Give a name of the Class library and Right Click on the solution Explore.
- Select a User control in the Template.
- Drag the Text Box form the Tool Box.
- Drop into User control design screen.
- Go to View Code (Right click on the design screen and Click View Code in the Context Menu).
Step: 2 Two questions are raised when I create a User control.
- Create Property
- Create Events.
Now, I answer a First Question. Next class we will discuss about the Creation of Events
Create Property:
Syntax for create Property for User control: Public datatype
{
get { return datatypevalue;}
set{ datatypevalue = value;}
}
What is mean by get and Set?
Get (get) -- return a Value
Set (set) -- give a value
Coding:
#region Members Declaration
public bool clr;
public test tst;
#endregion Members Declaration
#region Create Property
public test validate
{
get { return tst; }
set { tst = value; }
}
#endregion Create Property
#region Declare Enumaration
public enum test
{
Low,
Medium,
High
}
#endregion Declare Enumaration
#region Create Property
public bool Colorchk
{
get { return clr; }
set { clr = value; }
}
#endregion Create Property
#region set background
public void SetColor()
{
if (validate == test.Low)
{
textBox1.BackColor = System.Drawing.Color.Yellow;
}
else if (validate == test.Medium )
{
textBox1.BackColor = System.Drawing.Color.Green;
}
else if (validate == test.High)
{
textBox1.BackColor = System.Drawing.Color.Red;
}
}
#endregion set background
private void UC_Txt_Load(object sender, EventArgs e)
{
SetColor();
}
Step:3
Create a windows Application and Call the user control.
How to call the User control (dll) file in the windows Application?
Right click on the Tool Box >>Choose Items >> Click "Browse" in the window and Select your dll file location and click "Ok".
Drag your user control and Drop into the form. User and Programmer could view the Validate Property in the Property window of User control.
User could choice the validate Option and run the Project. See the difference.
Conclusion:
So today we discussed about the Create and use of User control in C#.net.Thanks for seeing this article.