Your SlideShare is downloading. ×
  • Like
Windows Forms For Beginners Part 5
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×

Now you can save presentations on your phone or tablet

Available for both IPhone and Android

Text the download link to your phone

Standard text messaging rates apply

Windows Forms For Beginners Part 5

  • 240 views
Published

• Validating Textbox input using ‘Validating’ event …

• 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

Published in Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
    Be the first to like this
No Downloads

Views

Total Views
240
On SlideShare
0
From Embeds
0
Number of Embeds
0

Actions

Shares
Downloads
13
Comments
0
Likes
0

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. 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 bhushan.mulmule@gmail.com www.dotnetvideotutorial.com Windows Forms for Beginners Part 5 OpenFileDialog,SaveFileDialog, FontDialog, ColorDialog Bhushan Mulmule bhushan.mulmule@gmail.com www.dotnetvideotutorial.com Windows Forms for Beginners
  • 2. Demo ProjectDemo ProjectDemo ProjectDemo 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
  • 3. 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); }
  • 4. 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
  • 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
  • 6. 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; }
  • 7. 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; }
  • 8. 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 =
  • 9. 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"; }