Today , we will discuss about step by step creation of User control and How to handle in the Windows Application? Validation is an important part of Development as well as Application side. No Validation Control for windows Application.
Purpose of this Article:
Text Box Validation[Int,Double and String].
Using Enumeraion in the User control.
Call the User control in the another Project.
Create User Control in C#.Net:
Name of the User Control: ValidateTextBox
File Extension : .dll.
In Project Section:
Open your VS 2008/2005.
Select “Class Library” in the VS Template.
Type “Class Library” name in the Name Input Box Eg: ValidateTextBox
Click “Ok” Button.
Right Click on the “Project Name”.
Select Add >>“New Item”
Select “Windows Forms” on the Left side of the Window.
Select “user Control” on the Right side of the Window.
Type “User Control “ Name in the Inpyt Box Eg: ValidateTextBox
Click “Add” button.
Now User control added in the Project.
Drag “Text Box” from Tool Box and Drop into User control.
Now , Copy and paste in the Code View Code.
Go To Project Folder (Bin>>Debug)
File Name:“ValidateTextBox.dll”
Now , You add this DLL in your Project .
How to add?
Go to Add Reference
Click “Browse” button
Select your Path of the DLL .
How to Test it?
Drag “ValidateTextBox” from Toolbar
Drop “ValidateTextBox“ into Form.
Drag “Button” from Toolbar
Drop “Button“ into Form.
Change the Value of “ValidationType” Property in the ValidateTextBox.
If you select “Int” – Only Interger value will type(10).
you select “Double” – Only Double value will type (like 10.10).
you select “None” – string value will type (like GOOD0205).
Conclusion:
Thank you for reading this Article .and I hope , You enjoy it.
Purpose of this Article:
Text Box Validation[Int,Double and String].
Using Enumeraion in the User control.
Call the User control in the another Project.
Create User Control in C#.Net:
Name of the User Control: ValidateTextBox
File Extension : .dll.
In Project Section:
Open your VS 2008/2005.
Select “Class Library” in the VS Template.
Type “Class Library” name in the Name Input Box Eg: ValidateTextBox
Click “Ok” Button.
Right Click on the “Project Name”.
Select Add >>“New Item”
Select “Windows Forms” on the Left side of the Window.
Select “user Control” on the Right side of the Window.
Type “User Control “ Name in the Inpyt Box Eg: ValidateTextBox
Click “Add” button.
Now User control added in the Project.
Drag “Text Box” from Tool Box and Drop into User control.
Now , Copy and paste in the Code View Code.
public enum ValidationType
{
None,
Int,
Double
}
private ValidationType m_align;
public ValidationType ValidationTypes
{
get { return m_align; }
set { m_align = value; }
}
public void ValidateTextNumber(KeyPressEventArgs e)
{
if (e.KeyChar >= 65 && e.KeyChar <= 122 || e.KeyChar == 32)
{
e.Handled = true;
}
else
{
e.Handled = false;
}
}
public bool IsValidNumber(string number)
{
int position = number.IndexOf(".");
if (position != -1)
{
int decdigits = number.Substring(position + 1, number.Length - number.IndexOf('.') - 1).Length;
if (decdigits == 2)
{
return true;
}
else
{
return false;
}
}
return false;
}
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (ValidationTypes == ValidationType.Double)
{
ValidateTextNumber(e);
}
else if (ValidationTypes == ValidationType.Int)
{
ValidateTextNumber(e);
}
}
public void ValidateTextDouble(TextBox t)
{
bool val = IsValidNumber(t.Text);
if (!val)
{
MessageBox.Show("Invalid number");
t.Focus();
}
}
public void ValidateTextInt(TextBox t)
{
bool val = IsValidNumber(t.Text);
if (val)
{
MessageBox.Show("Invalid number");
t.Focus();
}
}
private void textBox1_Leave(object sender, EventArgs e)
{
if (ValidationTypes == ValidationType.Double)
{
ValidateTextDouble(textBox1);
}
else if (ValidationTypes == ValidationType.Int)
{
ValidateTextInt(textBox1);
}
}
Rebuild a Project.{
None,
Int,
Double
}
private ValidationType m_align;
public ValidationType ValidationTypes
{
get { return m_align; }
set { m_align = value; }
}
public void ValidateTextNumber(KeyPressEventArgs e)
{
if (e.KeyChar >= 65 && e.KeyChar <= 122 || e.KeyChar == 32)
{
e.Handled = true;
}
else
{
e.Handled = false;
}
}
public bool IsValidNumber(string number)
{
int position = number.IndexOf(".");
if (position != -1)
{
int decdigits = number.Substring(position + 1, number.Length - number.IndexOf('.') - 1).Length;
if (decdigits == 2)
{
return true;
}
else
{
return false;
}
}
return false;
}
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (ValidationTypes == ValidationType.Double)
{
ValidateTextNumber(e);
}
else if (ValidationTypes == ValidationType.Int)
{
ValidateTextNumber(e);
}
}
public void ValidateTextDouble(TextBox t)
{
bool val = IsValidNumber(t.Text);
if (!val)
{
MessageBox.Show("Invalid number");
t.Focus();
}
}
public void ValidateTextInt(TextBox t)
{
bool val = IsValidNumber(t.Text);
if (val)
{
MessageBox.Show("Invalid number");
t.Focus();
}
}
private void textBox1_Leave(object sender, EventArgs e)
{
if (ValidationTypes == ValidationType.Double)
{
ValidateTextDouble(textBox1);
}
else if (ValidationTypes == ValidationType.Int)
{
ValidateTextInt(textBox1);
}
}
Go To Project Folder (Bin>>Debug)
File Name:“ValidateTextBox.dll”
Now , You add this DLL in your Project .
How to add?
Go to Add Reference
Click “Browse” button
Select your Path of the DLL .
How to Test it?
Drag “ValidateTextBox” from Toolbar
Drop “ValidateTextBox“ into Form.
Drag “Button” from Toolbar
Drop “Button“ into Form.
Change the Value of “ValidationType” Property in the ValidateTextBox.
If you select “Int” – Only Interger value will type(10).
you select “Double” – Only Double value will type (like 10.10).
you select “None” – string value will type (like GOOD0205).
Conclusion:
Thank you for reading this Article .and I hope , You enjoy it.