RAMESH SAHU

RAMESH SAHU

  • NA
  • 121
  • 12.7k

how to perform Clear option in this my code

Dec 24 2016 5:36 AM
hi here this is my Registration Form in c# i did validation for all my field but i want to do when i click on clear button  the all field should be clear and should be focus on my name Textbox 
this is not working properly please see my code and help me anyone . where i did mistake
 
 
 
private void txtName_Validating(object sender, CancelEventArgs e)
{
TextBox tb = sender as TextBox;
if (tb.Text.Trim().Length == 0)
{
MessageBox.Show("Can not leave Empty");
e.Cancel = true;
return;
}
else e.Cancel = false;
if (tb.Name != "txtName")
{
if (tb.Text.Trim().Length < 8)
{
MessageBox.Show("Password Should be between 8 to 16 character ");
e.Cancel = true;
return;
}
else e.Cancel = false;
}
else e.Cancel = false;
if (tb.Name == "txtCpwd")
{
if (txtpwd.Text.Trim() != txtCpwd.Text.Trim())
{
MessageBox.Show("confirm password should be match with password");
e.Cancel = true;
return;
}
else e.Cancel = false;
}
else e.Cancel = false;return;
}
private void txtFname_Validating(object sender, CancelEventArgs e)
{
TextBox tb = sender as TextBox;
if (tb.Text.Trim().Length == 0)
{
MessageBox.Show("Please Enter Father Name");
e.Cancel = true;
return;
}
else e.Cancel = false; return;
}
private void txtFname_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.Handled = !(char.IsLetter(e.KeyChar) || e.KeyChar == (char)Keys.Back || e.KeyChar == (char)Keys.Space))
{
e.Handled = true;
base.OnKeyPress(e);
MessageBox.Show("Enter Characters Only");
return;
}
}
private void txtaddress_Validating(object sender, CancelEventArgs e)
{
TextBox tb = sender as TextBox;
if (tb.Text.Trim().Length == 0)
{
MessageBox.Show("Please Enter Address");
e.Cancel = true;
return;
}
else e.Cancel = false; return;
}
private void txtmo_Validating(object sender, CancelEventArgs e)
{
TextBox tb = sender as TextBox;
if (tb.Text.Trim().Length == 0)
{
MessageBox.Show("Please Enter Mobile Number");
e.Cancel = true;
return;
}
if (txtmo.Text.Trim().Length < 10)
{
MessageBox.Show("Mobile Number should not less than 10 digit");
e.Cancel = true;
return;
}
}
private void txtmo_KeyPress(object sender, KeyPressEventArgs e)
{
if (char.IsDigit(e.KeyChar) == false && Convert.ToInt32(e.KeyChar) != 8)
{
MessageBox.Show("Enter Numerics only");
e.Handled = true;
}
}
private void txtemail_Validating(object sender, CancelEventArgs e)
{
TextBox tb = sender as TextBox;
if (tb.Text.Trim().Length == 0)
{
MessageBox.Show("Please Enter E-Mail");
e.Cancel = true;
return;
}
else
e.Cancel = false;
//if (string.IsNullOrEmpty(txtemail.Text))
//{
// e.Cancel = true; txtemail.Focus(); errorProvider1.SetError(txtemail, " Please Enter E-Mail");
// return;
//}
//else
//{
// e.Cancel = false; errorProvider1.SetError(txtUID, "");
//}
System.Text.RegularExpressions.Regex rEmail = new System.Text.RegularExpressions.Regex(@"^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$");
if (txtemail.Text.Length > 0 && txtemail.Text.Trim().Length != 0)
{
if (!rEmail.IsMatch(txtemail.Text.Trim()))
{
MessageBox.Show("Please Check E-Mail");
txtemail.SelectAll();
e.Cancel = true;
return;
}
}
else e.Cancel = false;return;
}
private void txtUID_Validating(object sender, CancelEventArgs e)
{
//if (string.IsNullOrEmpty(txtUID.Text))
//{
// e.Cancel = true; txtUID.Focus(); errorProvider1.SetError(txtUID, " Please Enter Address");
// return;
//}
//else
//{
// e.Cancel = false; errorProvider1.SetError(txtUID, ""); return;
//}
TextBox tb = sender as TextBox;
if (tb.Text.Trim().Length == 0)
{
MessageBox.Show("Please Enter User Name");
e.Cancel = true;
return;
}
else e.Cancel = false;return;
}
for all validtion working properly but when my cursor is password or some other textbox and that time when i want to clear then it will be clear properly but focused not show on Name TextBox  .
 
this is my clear button code
 
txtName.Clear();
txtFname.Clear();
txtaddress.Clear();
txtmo.Clear();
txtemail.Clear();
radioButton1.Checked = false;
radioButton2.Checked = false;
txtUID.Clear();
txtpwd.Clear();
txtCpwd.Clear();
txtName.Focus();
 
 
 
 
 

Answers (1)