Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

CSharp.NET Tutorial – Day 16

Tags: menu property
In previous article, we have discussed what is Windows Programming, what is Character User Interface (CUI), what is Graphical User Interface (GUI), difference between CUI and GUI, How to Develop Windows Applications, what are Partial Classes, How to Develop a Windows Application using Visual Studio, what are Properties, what are Events, what are Event Procedures, How to Define Event Procedures Manually, How to Set Properties and Define Event Procedure Manually, How to Add a New Form, How to Bind an Event Procedure with Multiple Events of a Control in Visual Studio, How to Place Controls on the Form, How to Bind an Event Procedure with Multiple Controls, How does a Form gets created, How does a control gets placed on the Form, what are types of code In Windows application, definitions of Designer Code and Business Logic along with syntaxes and examples, etc

Please find below the link for accessing the article

CSharp.NET Tutorial - Day 15

Now, in this article we will discuss what are Default Events, what are default events for controls, what is form, what is Button, what is Label, what is TextBox, what are different kind of properties  of TextBox, how to set TextArea, how to set password, how set multiline, how to validate the controls, how to set Accept Button and Control Button of Form, how to Create a Menu to Windows Form, how to set Alt keys in Menu, how to set shortcut keys in Menu, how to set checkmarks in Menu, what are Dialog Controls, what is Color Dialog, what is Font Dialog, what is Folder Browser Dialog, what is Open File Dialog, what is Save File Dialog etc, what is Dock Property, what is RichTextBox along with syntaxes and examples, etc

Default Events
As we know, basically every control has number of events, but usually one event will be acting like as its default event and in most of the cases we write code under default events only.

We can write code under default event of any control by directly double clicking on the control itself, which will take to its matching event procedure.

Below are default events for some controls

Form
Load
Button
Click
TextBox
TextChanged
CheckBox & Radio Button
CheckedChanged
Timer
Tick
ListView, ListBox,|
ComboBox, Checked |
ListBox|
SelectedIndexChanded

Now we will discuss about some controls which we will frequently use in designing of a form.

Form:  Form is the base control and usually it is used for developing windows application.

Button:  Button is used for taking acceptance from the user to perform particular action.

Label:  Label can be used to display static text on user interface.

TextBox:  TextBox can be used for taking text input from the user.  It can be used in three different scenarios.
1)      Single Line Text (Default)
2)      Text Area
3)      Password Field

By default, TextBox takes the input in single line only.  If we want to make it is TextArea, set the 'Multiple Property' of TextBox as 'True'

By default the TextArea does not have any scrollbars to navigate up and down or left and right.  To provide this kind of feature, we need to set the 'Enumerated Property' scrollbars as 'Vertical' or 'Horizontal' or 'Both.'  Default is None.

By default, TextBox's WordWraper property is also True.  It is like WordWrap property in Notepad.

By default, we do not get Horizontal scrollbar to the TextArea because WordWrap property sets as 'True' we can make it by setting as 'False'

In order to make the Control as password field, set the password property of the control with the password character, '*'


While designing GUI application, we need to develop the application in such a way that it can be consumed without mouse.  Generally, we navigate between the controls by using Tab key of the keyboard, which allows us to move between each control in the sequence how these controls are placed on the Form.

If we want to set the sequence of Tab movements between controls, we need to set the Tab order as per requirement.  To do this go to View Menu and Select Tab Order, which displays the current Tab sequence.  Now click on each control in the order that we want to navigate.

In our above Form, perform the following validations.

1)      TextBoxes of Username and Password must be mandatory fields.
2)      Password should range in between 5 to 10 characters.

To perform the first and second validations, we need to do the following things.

Go to properties window of Password Textbox and set MaxLength as 10 so that it do not accept more than 10 characters.

Define a validating event procedure for Username TextBox and bind the event procedure with password TextBox.  Now under the event procedure, write the below code.

TextBox tb = sender as TextBox;
If (tb.Text.Trim ().Length == 0)
{
MessageBox.Show ("Do not leave Empty");
e.Cancel = true;
return;
}
If (tb.Name == "txtpwd")
{
If (tb.Text.Trim() == Length<5)
{
MessageBox.Show("password should be between 5 to 10 Characters");
e.Cancel=true;
}
}

Validating event occurs when the control is losing its focus to validate the Control.

Some of the events do have properties to access the properties of an event.  Under its event procedure, we can make use of parameter 'e' of the event procedure.  'e' is always refer to the current event.

Cancel is a property of the validating event which restricts the focus not or leave the control if it set as 'True'

To let the user close the form even without entering mandatory fields on the click of Cancel button, we need to do the following.

Go to Properties of Cancel Button and set 'Causes Validation Property' as False so that validating event of any other control will not be raised on Click of Cancel button.

Now under Cancel button, write the below code

txtName.CausesValidation = false;
txtpwd.CausesValidation=false;
this. Close ();

In above case, we set Causes Validation Property of both Username and Password as False so when we click on Cancel button, validating event of those two controls will not fire.

Under Login button Click, write the below code

if(txtName.Text.ToUpper()=="ADMIN" && txt.password=="admin")
MessageBox.Show("Valid user");
else
MessageBox.Show("Invalid user");

Under Clear button Click, write the below code

txtName.Text=" "; txtpwd.Clear();
txtName.Focues();

Accept Button and Control Button of Form
If Accept Button property of a Form is set, that Button gets clicked whenever the user presses Enter key on the keyboard.  To set this property, go to Properties of Form -> select Accept button property -> click on the Dropdown besides it, which will display all the buttons of Form -> Select Login button -> so that on click of Enter key, the code under this gets executed.

In the same way, we can set cancel button property also so that the button gets clicked when user presses Escape key on the keyboard.

If we see any Menu, it will have some 'alt' keys like shortcuts.  Now how we will set will see.

How to Create a Menu to Windows Form
·         To create a Menu, first place a 'MenuStrip' Control on the Form which will get added on top of the Form.
·         To add Menu to the MenuStrip, click on the left hand side corner of the MenuStrip, which will open a TextBox asking to Type Here.  Enter Menu name, which will get added to the MenuStrip.  Repeat the same process in order to add multiple menu items.
·         If we want our Menus to respond for 'Alt' keys, prefix the special character '&' before the character that should respond to 'Alt' keys.
Eg:
&File
&Edit
&Format
·         To create a shortcut for menu item, go to properties of Menu item -> select "Shortcut Keys" property click on dropdown besides it which will display a window -> choose a modifier 'Ctrl' or 'Alt' or 'Shift' and then the choose the key form ComboBox below.
·         To group related menu items under a Menu, we can add separators between Menu items.  To do it, Right click on Menu item and select insert -> separator which adds a separator on top of the Menu item.

Note:  Similar to inserting a separator, we can also insert a menu item if required in the middle.

If we want to display any image beside any menu item, Right click on it -> Select 'Set Image' which will open a window -> Select local resource -> Click on Import button which will open a Dialog box, using it Select an image from your desired location.

Sometimes we find checkmark beside a menu item to identify a property whether ON or OFF.
            Eg:  WordWrap under Notepad

To create Checkmark beside menu item, Right click on it and select "Checked"

Note:  To check or uncheck the item in Runtime, we need to write the code explicitly under the click event as below.

If (<Control> . Checked == true)
<Control> . Checked == false;
else
<Control> . Checked = true;

Dialog Controls
Dialog controls are some special controls, which will display a list of values to choose from.  By default, we are provided with five Dialog Controls, which are listed below.

1)      Color Dialog
2)      Font Dialog
3)      Folder Browser Dialog
4)      Open File Dialog
5)      Save File Dialog

A Dialog control when added to a Form comes and sits in the button of Studio, but not directly on the form.  To make it visible in Runtime, we need to call the method ShowDialog, which is common for all the five controls.

Dialog Controls never perform any action. Their responsibility is displaying the values for the user to choose and return back the value selected by the user that should be captured by programmers explicitly to perform a required action.

To capture the values, each control provide a different property which are listed below

ColorDialog                                         Color
FontDialog                                           Font
FolderBrowserDialog                          Selected Path
OpenFileDialog                                   File Name
SaveFileDialog                                                File Name

Dock Property
Dock property defines which borders of the control are bound to the container.  It can be set with six different options which are listed below:

·         None
·         Left
·         Right
·         Top
·         Bottom
·         Fill

Left means Left border bound the container.
None means No border and not bound the container.

RichTextBox
This is same like TextBox which provides advanced Text entry and editing features such as character and Paragraph formatting.

Happy Learning….!!!!!

CSharp.NET Tutorial - Day 17


This post first appeared on Dot Net Programming (C#, Asp.Net, ADO.Net, WCF, WPF, Ajax, LINQ), please read the originial post: here

Share the post

CSharp.NET Tutorial – Day 16

×

Subscribe to Dot Net Programming (c#, Asp.net, Ado.net, Wcf, Wpf, Ajax, Linq)

Get updates delivered right to your inbox!

Thank you for your subscription

×