You are on page 1of 7

September 13, 2010 1.

Create desktop application

[WINDOW FORM WITH C#]

Drag from tool box to form

CUSC | Ng Tng Dn

September 13, 2010 2. Check all field on form must be input

[WINDOW FORM WITH C#]

/// <summary> /// Check all text box must be input /// </summary> /// <returns>true if all input</returns> private bool ControlCheck() { if (txtName.Text == "") { MessageBox.Show("Name not null", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information); txtName.Focus(); return false; } if (txtAge.Text == "") { MessageBox.Show("Age not null", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information); txtName.Focus(); return false; } if (txtAddress.Text == "") { MessageBox.Show("Address not null", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information); txtAddress.Focus(); return false; } if (txtCity.Text == "") { MessageBox.Show("City not null", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information); txtCity.Focus(); return false; } if (txtCountry.Text == "") { MessageBox.Show("Country not null", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information); txtCountry.Focus(); return false; } if (txtYoE.Text == "") { MessageBox.Show("Years of experience not null", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information); txtYoE.Focus(); return false; }

CUSC | Ng Tng Dn

September 13, 2010

[WINDOW FORM WITH C#]

if (txtQuantification.Text == "") { MessageBox.Show("Quantification not null", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information); txtQuantification.Focus(); return false; } if (txtPhone.Text == "( ) -") { MessageBox.Show("Quantification not null", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information); txtPhone.Focus(); return false; } if (txtEmail.Text == "") { MessageBox.Show("Email not null", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information); txtEmail.Focus(); return false; } if (txtDate.Text == " / /") { MessageBox.Show("Date not null", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information); txtDate.Focus(); return false; } return true; }

3. Text box age only accept digit character: Using keypress event
/// <summary> /// only accept number /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void txtAge_KeyPress(object sender, KeyPressEventArgs e) { if (Char.IsLetter(e.KeyChar)) { MessageBox.Show("Accept only number!", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information); e.Handled = true; } if (!(Char.IsDigit(e.KeyChar) || Char.IsControl(e.KeyChar))) { e.Handled = true; } }

CUSC | Ng Tng Dn

September 13, 2010

[WINDOW FORM WITH C#]

4. Validation for year of experience accept number 0 to 2: Using leave event


private void txtYoE_Leave(object sender, EventArgs e) { if (Convert.ToInt32(txtYoE.Text) > 2 || Convert.ToInt32(txtYoE.Text) < 0) { MessageBox.Show("Value must be > 0 and < 2 !", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information); txtYoE.Focus(); } } But you must sure only number is allowed into text field

5. Now we get value and display by message box a. Check valid all information bay call ControlCheck() method. b. Get value. c. Display Using Click event of submit button
private void btnSubmit_Click(object sender, EventArgs e) { if (!ControlCheck()) return; string infor = "Information of applican: \n"; infor += txtName.Text + "\n"; infor += txtAge.Text + "\n"; infor += txtAddress.Text + "\n"; infor += txtCity.Text + "\n"; infor += txtState.Text + "\n";

CUSC | Ng Tng Dn

September 13, 2010


infor infor infor infor infor infor += += += += += += txtCountry.Text + "\n"; txtYoE.Text + "\n"; txtQuantification.Text + "\n"; txtPhone.Text + "\n"; txtEmail.Text + "\n"; txtDate.Text + "\n";

[WINDOW FORM WITH C#]

MessageBox.Show(infor, "Information", MessageBoxButtons.OK, MessageBoxIcon.Information); }

6. Close the form using Formclosing event


private void btnExit_Click(object sender, EventArgs e) { this.Close(); }

private void Form1_FormClosing(object sender, FormClosingEventArgs e) { if (MessageBox.Show("Are you sure close this form ?", "Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) { e.Cancel = true;

CUSC | Ng Tng Dn

September 13, 2010


} }

[WINDOW FORM WITH C#]

7. Now we assign rule form tab order a. View tab order

b. Set order

CUSC | Ng Tng Dn

September 13, 2010

[WINDOW FORM WITH C#]

CUSC | Ng Tng Dn

You might also like