24 August 2012

Validate a Usercontrol textbox in C#.net

Today, we will discuss about create a user control in C#.net.It is a powerful and Dynamic control in .NET. It is a flexible control.
Now we will go to our class:
Step: 1
The following steps are the Creation of Usercontrol.
Create Project and Right Click on the Project >> Add>>New Items , Click User control in the window and give a name of the User Control .
Eg: testUsercontrol
Step: 2
Now, Select a Label, Textbox Error Provider in the toolbox.
Step: 3
public string txtChangges
{
get { return textBox1.Text; }
set { textBox1.Text = value; }
}
public System.Drawing.Color txtBackColor
{
get { return textBox1.BackColor; }
set { textBox1.BackColor = value; }
}
public System.Drawing.Color txtForeColor
{
get { return textBox1.ForeColor; }
set { textBox1.ForeColor = value; }
}
public bool txtReadOnly
{
get { return textBox1.ReadOnly; }
set { textBox1.ReadOnly = value; }
}
public bool multipline
{
get { return textBox1.Multiline; }
set { textBox1.Multiline = value; }
}
public int maxlength
{
get { return textBox1.MaxLength; }
set { textBox1.MaxLength = value; }
}
public string lblText
{
get { return label1.Text; }
set { label1.Text = value; }
}
public void textValidate()
{
if (textBox1.Text == "")
{
string error_Message = "Enter your " + " " + label1.Text;
errorProvider1.SetError(textBox1, error_Message);
textBox1.Focus();
}
else
{
errorProvider1.SetError(textBox1, "");
}
}
private void textBox1_Validating(object sender, CancelEventArgs e)
{
textValidate();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
textValidate();
}
Step: 4
Rebuild and Run your Project.
Step: 5
Select a user control from your Tools box
. Drap and Drop of testUsercontrol into your Project.
Step: 6
testUsercontrol1.txtBackColor = Color.Wheat;
testUsercontrol1.txtForeColor = Color.Green;
testUsercontrol1.Visible = true;
testUsercontrol1.multipline = true;
testUsercontrol1.maxlength = 5;
testUsercontrol1.lblText = "Customer Name";

No comments: