Tuesday, February 25, 2014

Creating Menu in Windows Application

Creating Menu in Windows Application

- Not all events but some events have properties under them. If we want to access the propertied of an event, under its event procedure we can make use of the parameter e, which refers to properties of current executing event.

- In the above case, cancel is a property of validation event when set as true will not allow the focus to leave the control.

- If we want to close the Form even from mandatory field --> Goto properties of cancel button and set causes validation property as false, So that when we click on the button, the code under its click event gets executed before execution of textbox’s validation event.

- Now under the cancel button write the following code
  {
        txtUser.causesValidation = false;
        txtPwd.causesValidation = false;

        this.close();
  }

- When we set the causes validation property of txtBox as false, validation event of txtBox will not occur, so the form gets closed.

- Now, under the login button write the following code:
if(txtUser.Text.Trim() == “Admin” && txtPwd.Text.Trim() == “Admin”)

        MessageBox.Show(“Login successful”);
else
        MessageBox.Show(“Login failed”);

- Now under reset button:
  txtUser.Text = “”;
  txtPwd.Clear();
  txtUser.focus();

Creating Menu in Windows application

To create a menu  add menu strip control to form, which sets on top of it.

- To add a menu on menu strip  click on Left hand site corner of it, which opens a textbox to type here  Enter a name in it  which adds a menu, repeat the same process for adding multiple menus.

- To add a menu item, under a menu  click on the menu  which opens the text box blow assigning to type here  enter a name init, which adds a menu item, repeat the same process for adding multiple menu items.

Note – A menu and menu item are object of the class toolStripMenu item.
 If we want menus to be responding for alt keys of key board, prefix with “&” before the character which should response for alt.

Eg:     &File, &Edit, Format etc..

- To create a short cut for menu item so that it response to key board actions  Goto properties of menu item  Select short cut keys property   click on drop down beside it that displays a window. In it choose a modifier ctrl or alt or shift and then choose a key for ComboBox blow.

- TO group related menu item, under a menu we can add separators between menu item, to do it  Right click on menu item and select insert separator, which adds a separator on top of the menu item.

Note - same as we inserted separator, we can also insert a menu item if required in the middle.

- If we want to display an Image, beside a menu item  Right click on it and select the set image, Which opens a window  select local resource  and click on import button  which opens a DilogBlox , Using it select an image from your hard disk.

- Some times, we find check marks beside menu item to identify a property is on or off. Eg: Word Wrap under notepad. To create check mark beside a menu item --> right click on it and select checked.

Note – To check or uncheck the item in run time we need to write code explicitly under its click event as following
if ( < Control >.Checked == true)           
      < Control >.Checked = false;
else
      < Control >.Checked = true;                   

Dialog Control

These are used for displaying a list of system values from which the users can select; we have five different dialog controls like:
ColorDialog, FolderBrowserDialog, FontDialog, OpenFileDialog and SaveFileDialog

- None of the dialog controls are visible on a form even after click in them. TO make them visible we need to call method ShowDialog explicitly, which is common for all the five controls. 

- Dialog controls never perform any actions; they are only responsible for returning the values that has been chosen by the users. It was the programmer responsibility to capture those values and perform a necessary action.

To capture the values chosen use following properties 
ColorDialog
Color
FolderBrowserDialog
Select Path
FontDialog
Font
OpenFileDialog
FileName
SaveFileDialog.
File name

No comments: