Example: Creating registration Form in Windows Application using CSharp
Design as following, define a validating event procedure for user name textbox and bind that event procedure with both password textbox's. Now write the following code under the Validating Event Procedure:
Design as following, define a validating event procedure for user name textbox and bind that event procedure with both password textbox's. Now write the following code under the Validating Event Procedure:
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 WinForms7 { public partial class Form4 : Form { public Form4() { InitializeComponent(); } private void txtname_Validating(object sender, CancelEventArgs e) { TextBox tb = sender as TextBox; if (tb.Text.Trim().Length == 0) { MessageBox.Show("Cannot leave empty"); e.Cancel = true; return; } if (tb.Name =="txtpass") { if (tb.Text.Trim().Length < 8) { MessageBox.Show("Pwd should be between 8 to 16 chars"); e.Cancel = true; return; } if(tb.Name=="txtCPass") { if(txtPass.Text .Trim()!=txtPass.Text.Trim()) { MessageBox.Show("Conform pwd should match with pwd"); return; } btnSave.Enabled=true; } } } private void btnClose_Click(object sender, EventArgs e) { txtname.CausesValidation = false; txtPass.CausesValidation = false; txtCPass.CausesValidation = false; this.Close(); } 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 numeric only"); e.Handled = true; } private void btnSave_Click(object sender, EventArgs e) { //need to write code for saving data to database here MessageBox.Show("Data saved to database"); } private void btnClear_Click(object sender, EventArgs e) { foreach (Control ctrl in this.Controls) { if (ctrl.GetType().Name == "TextBox") { TextBox tb = ctrl as TextBox; tb.Clear(); } btnSave.Enabled = false; txtname.Focus(); } } } }
No comments:
Post a Comment