windows forms for beginners part 5

9
Demo Projects Validating Inputs Text Editor What will you learn? Validating Textbox inp Using KeyPress even Using various built in Using various dialogb Using following contro o ReachTextBox o Menustrip o Toolstrip o Statusbar o DialogBoxes: O Windows put using ‘Validating’ event nt to identify input character in Textbox n methods of ReachTextbox boxes ols: OpenFileDialog,SaveFileDialog, FontDialog, Bhu bhushan.mulm www.dotnetv Forms for Beginner Part 5 , ColorDialog ushan Mulmule [email protected] videotutorial.com rs

Upload: bhushan-mulmule

Post on 25-Dec-2014

211 views

Category:

Technology


3 download

DESCRIPTION

• Validating Textbox input using ‘Validating’ event • Using KeyPress event to identify input character in Textbox • Using various built in methods of ReachTextbox • Using various dialogboxes • Using following controls: o ReachTextBox o Menustrip o Toolstrip o Statusbar o DialogBoxes: OpenFileDialog,SaveFileDialog, FontDialog, ColorDialog

TRANSCRIPT

Page 1: Windows Forms For Beginners Part 5

Demo Projects

• Validating Inputs

• Text Editor

What will you learn?

• Validating Textbox input using ‘

• Using KeyPress event to identify input character

• Using various built in methods of

• Using various dialogboxes

• Using following controls:

o ReachTextBox

o Menustrip

o Toolstrip

o Statusbar

o DialogBoxes: OpenFileDialog,SaveFileDialog, FontDialog, ColorDialog

Windows Forms for Beginners

ox input using ‘Validating’ event

Using KeyPress event to identify input character in Textbox

Using various built in methods of ReachTextbox

Using various dialogboxes

Using following controls:

OpenFileDialog,SaveFileDialog, FontDialog, ColorDialog

Bhushan Mulmule

[email protected]

www.dotnetvideotutorial.com

Windows Forms for Beginners

Part 5

OpenFileDialog,SaveFileDialog, FontDialog, ColorDialog

Bhushan Mulmule

[email protected]

www.dotnetvideotutorial.com

Windows Forms for Beginners

Page 2: Windows Forms For Beginners Part 5

Demo Project Demo Project Demo Project Demo Project 1111: : : : Validating InputValidating InputValidating InputValidating Input

Step 1: Design UI

Instructions:

• Constructor of form will get executed when form object will get created (when

form will loaded)

• Constructors are best place to initialization. So in this example we will initialize

Tag property of textbox to false in form’s constructor.

• Tag property can hold any extra information about control. (it can be normal

string)

• Validating event of textbox executes just before Leave event (lost focus) and best

place to put validation logic.

• Validation Logic 1: All the three textboxes should not be empty

o As this logic will be same for all textboxes we will add single Validating

event handler for Validating event of all three textboxes

• Validation Logic 2: Only numeric value should be allowed in Age textbox

o Value will get typed in textbox only if it is numeric

o KeyPress event fires when we press any key in textbox.

o Every key on keyboard has KeyCode.

o KeyPress event passes KeyPressEventArgs with information which key is

pressed. Using which we can only allow numeric keys

Page 3: Windows Forms For Beginners Part 5

Step 2: Adding Event Handlers / Code

1. Right click form ���� view code ���� in constructor set some properties as follows

(insert bold code only)

public partial class frmValidatingInputs : Form { public frmValidatingInputs() { InitializeComponent(); btnOK.Enabled = false; txtName.Tag = false;

txtCity.Tag = false;

txtAge.Tag = false;

}

. . .

}

2. Insert Validating event for all three textboxes

• Select three textboxes (using control key)� Go to property window �

event list � Locate Validating event � type “TextBoxEmpty_Validating”

and press enter � code it as follow

private void TextBoxEmpty_Validating(object sender, CancelEventArgs e) { TextBox txt = (TextBox)sender; if (txt.Text.Length == 0) { txt.BackColor = Color.Red; txt.Tag = false; } else { txt.BackColor = Color.White; txt.Tag = true; } btnOK.Enabled = ((bool)txtName.Tag && (bool)txtCity.Tag && (bool)txtAge.Tag); }

Page 4: Windows Forms For Beginners Part 5

3. To insert key press event for txtAge

• Select txtAge -> Go to property window � Events list � Locate KeyPress

event � double click on it � handler will be inserted � code it as follow:

private void txtAge_KeyPress(object sender, KeyPressEventArgs e) { if ((e.KeyChar < 48 || e.KeyChar > 57) && e.KeyChar != 8) e.Handled = true; }

• Note:

o Numeric key have keychar values between 48 to 57. o 48 for 0 …. 57 for 9 o Backspace key has keycode 8 o Above logic will cancel the event if KeyChar is not between

48 to 57 or 8 o In other words it will only allow keys with keycode 48 to 57

and 8 i.e 0 to 9 and backspace (reqred to delete)

4. Debug using F11 to understand flow

Demo Project 2Demo Project 2Demo Project 2Demo Project 2: : : : Text EditorText EditorText EditorText Editor

Step 1: Design UI

Page 5: Windows Forms For Beginners Part 5

Dilogboxes:

Add SaveFile, OpenFile, Font

and Color dialog boxes

Menustrip:

Add Menu as shown

Also add submenu as shown in second image below

StatusStrip:

Add 2 statuslabels and change

text and name as shown

ToolStrip:

Add Three buttons then separator and

again three buttons.

DisplayStyle: Text (for all)

And For Bold, Italic, Underline

CheckOnClick: True

Page 6: Windows Forms For Beginners Part 5

Step 2: Adding event handlers

1. menuNew:

private void menuNew_Click(object sender, EventArgs e) { txtEditor.Visible = true; txtEditor.Clear(); }

2. menuOpen and OpenFile function

private void menuOpen_Click(object sender, EventArgs e) { OpenFile(); } private void OpenFile() { if (dlgOpen.ShowDialog() == DialogResult.OK) txtEditor.LoadFile(dlgOpen.FileName); }

3. menuSave and SaveFile function

private void menuSave_Click(object sender, EventArgs e) { SaveFile(); } private void SaveFile() { if (dlgSave.ShowDialog() == DialogResult.OK) txtEditor.SaveFile(dlgSave.FileName); }

4. menuClose

private void menuClose_Click(object sender, EventArgs e) { txtEditor.Visible = false; }

Page 7: Windows Forms For Beginners Part 5

5. undo, redo, cut, copy paste and selectall

private void menuUndo_Click(object sender, EventArgs e) { txtEditor.Undo(); } private void menuRedo_Click(object sender, EventArgs e) { txtEditor.Redo(); } private void menuCut_Click(object sender, EventArgs e) { txtEditor.Cut(); } private void menuCopy_Click(object sender, EventArgs e) { txtEditor.Copy(); } private void menuPaste_Click(object sender, EventArgs e) { txtEditor.Paste(); } private void menuSelectAll_Click(object sender, EventArgs e) { txtEditor.SelectAll(); }

6. menuFont

private void menuFont_Click(object sender, EventArgs e) { if(dlgFont.ShowDialog() == DialogResult.OK) txtEditor.SelectionFont = dlgFont.Font; }

7. ForeColor and BackColor

private void menuForeColor_Click(object sender, EventArgs e) { if (dlgColor.ShowDialog() == DialogResult.OK) txtEditor.SelectionColor = dlgColor.Color; }

Page 8: Windows Forms For Beginners Part 5

private void menBackColor_Click(object sender, EventArgs e) { if (dlgColor.ShowDialog() == DialogResult.OK) txtEditor.BackColor = dlgColor.Color; }

8. menuExit

private void menuExit_Click(object sender, EventArgs e) { Application.Exit(); }

9. toolNew

private void toolNew_Click(object sender, EventArgs e) { txtEditor.Visible = true; txtEditor.Clear(); }

10. toolOpen

private void toolOpen_Click(object sender, EventArgs e) { OpenFile(); }

11. toolSave

private void toolSave_Click(object sender, EventArgs e) { SaveFile(); }

12. toolBold

private void toolBold_Click(object sender, EventArgs e) { Font Currentfnt = txtEditor.SelectionFont; if(toolBold.Checked) txtEditor.SelectionFont = new Font (Currentfnt,Currentfnt.Style | FontStyle.Bold); else txtEditor.SelectionFont =

Page 9: Windows Forms For Beginners Part 5

new Font(Currentfnt, Currentfnt.Style & ~FontStyle.Bold); }

13. toolItalic

private void toolItalic_Click(object sender, EventArgs e) { Font Currentfnt = txtEditor.SelectionFont; if (toolItalic.Checked) txtEditor.SelectionFont = new Font(Currentfnt, Currentfnt.Style | FontStyle.Italic); else txtEditor.SelectionFont = new Font(Currentfnt, Currentfnt.Style & ~FontStyle.Italic); }

14. toolUnderline

private void toolUnderline_Click(object sender, EventArgs e) { Font Currentfnt = txtEditor.SelectionFont; if (toolUnderline.Checked) txtEditor.SelectionFont = new Font(Currentfnt, Currentfnt.Style | FontStyle.Underline); else txtEditor.SelectionFont = new Font(Currentfnt, Currentfnt.Style & ~FontStyle.Underline); }

15. MyTextditor_TextChange()

private void MyEditor_TextChanged(object sender, EventArgs e) { statusLines.Text = txtEditor.Lines.Count().ToString() + " Lines"; statusChars.Text = txtEditor.TextLength.ToString() + " Characters"; }