This document provides instructions for creating demo projects in Windows Forms for beginners. It includes steps to create projects for validating textbox input and building a basic text editor. The validating inputs project demonstrates validating that textboxes are not empty and allowing only numeric input in one textbox. The text editor project shows how to add menu items, toolstrips, statusbars and dialog boxes to open, save, edit text with font and color options. Event handlers are added to handle user interactions like file operations, text editing and formatting.
What's New in Teams Calling, Meetings and Devices March 2024
Windows Forms Demo Projects
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