how to make validations in a textbox ,source code
//try to make forms like this
//try to name as mentioned
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsProject
{
public partial class Form4 : Form
{
TextBox tb;
public Form4()
{
InitializeComponent();
}
private void btnSave_Click(object sender, EventArgs e)
{
//Code to save data into database
MessageBox.Show("Data saved");
}
private void btnClear_Click(object sender, EventArgs e)
{
// txtName.Clear(); txtPwd.Clear();
// txtCPwd.Text = txtAddress.Text = txtPhone.Text = "";
foreach (Control ctrl in this.Controls)
{
if (ctrl.GetType().Name == "TextBox")
{
TextBox tb = ctrl as TextBox;
tb.Clear();
}
}
txtName.Focus();
}
private void btnClose_Click(object sender, EventArgs e)
{
tb.CausesValidation = false;
this.Close();
}
private void txtName_Leave(object sender, EventArgs e)
{
tb = sender as TextBox;
}
private void txtName_Validating(object sender, CancelEventArgs e)
{
if (tb.Text.Trim().Length == 0)
{
MessageBox.Show("Cannot leave empty");
e.Cancel = true;
return;
}
if (tb.Name != "txtName")
{
if (tb.Text.Trim().Length < 6)
{
MessageBox.Show("Pwd should be randing between 6 to 12 chars");
e.Cancel = true; return;
}
}
if (tb.Name == "txtCPwd")
{
if (txtPwd.Text.Trim() != txtCPwd.Text.Trim())
{ MessageBox.Show("Confirm pwd should match with pwd");
e.Cancel = true; }
}
}
private void txtPhone_KeyPress(object sender, KeyPressEventArgs e)
{
//MessageBox.Show(Convert.ToInt32(e.KeyChar).ToString());
if (char.IsDigit(e.KeyChar) == false && Convert.ToInt32(e.KeyChar) != 8)
{
MessageBox.Show("Enter numerics only");
e.Handled = true;
}
}
}
}