You are on page 1of 31

UNIT 2

BUILDING WINDOWS APPLICATIONS

Methods
A method is an action that an object can perform. For example, Add is a method of the
ComboBox object, because it adds a new entry to a combo box.

The following procedure uses the Add method to add a new item to a ComboBox.

Sub AddEntry(newEntry as String)


Combo1.Add newEntry
End Sub

Properties
A property is an attribute of an object that defines one of the object's characteristics, such
as size, color, or screen location, or an aspect of its behavior, such as whether it is enabled
or visible. To change the characteristics of an object, you change the values of its
properties.

To set the value of a property, follow the reference to an object with a period, the property
name, an equal sign (=), and the new property value. For example, the following procedure
changes the caption of a Visual Basic form by setting the Caption property.

VB

Sub ChangeName(newTitle)
myForm.Caption = newTitle
End Sub

Events
An event is an action recognized by an object, such as clicking the mouse or pressing a key,
and for which you can write code to respond. Events can occur as a result of a user action
or program code, or they can be triggered by the system. Most common events are: click,
keydown, keypress, mousemove, mouseclick etc.

EVENT HANDLING
Events are basically a user action like key press, clicks, mouse movements, etc., or some
occurrence like system generated notifications. Applications need to respond to events
when they occur.

Clicking on a button, or entering some text in a text box, or clicking on a menu item, all are
examples of events. An event is an action that calls a function or may cause another event.
Event handlers are functions that tell how to respond to an event.

GUI PROGRAMMING: Govt. Zirtiri Residential Science College Page 1


VB.Net is an event-driven language. There are mainly two types of events −

 Mouse events
 Keyboard events

Handling Mouse Events


Mouse events occur with mouse movements in forms and controls. Following are the
various mouse events related with a Control class −

 MouseDown − it occurs when a mouse button is pressed


 MouseEnter − it occurs when the mouse pointer enters the control
 MouseHover − it occurs when the mouse pointer hovers over the control
 MouseLeave − it occurs when the mouse pointer leaves the control
 MouseMove − it occurs when the mouse pointer moves over the control
 MouseUp − it occurs when the mouse pointer is over the control and the mouse
button is released
 MouseWheel − it occurs when the mouse wheel moves and the control has focus

Handling Keyboard Events


Following are the various keyboard events related with a Control class −

 KeyDown − occurs when a key is pressed down and the control has focus
 KeyPress − occurs when a key is pressed and the control has focus
 KeyUp − occurs when a key is released while the control has focus

The event handlers of the KeyDown and KeyUp events get an argument of type
KeyEventArgs. This object has the following properties −

 Alt − it indicates whether the ALT key is pressed


 Control − it indicates whether the CTRL key is pressed
 Handled − it indicates whether the event is handled
 KeyCode − stores the keyboard code for the event
 KeyData − stores the keyboard data for the event
 KeyValue − stores the keyboard value for the event
 Modifiers − it indicates which modifier keys (Ctrl, Shift, and/or Alt) are pressed
 Shift − it indicates if the Shift key is pressed

WORKING WITH FORMS


Visual Basic Form is the container for all the controls that make up the user interface.
Every window you see in a running visual basic application is a form, thus the terms form
and window describe the same entity. Visual Studio creates a default form for you when
you create a Windows Forms Application. Every form will have title bar on which the
form's caption is displayed and there will be buttons to close, maximize and minimize the
form shown below:

GUI PROGRAMMING: Govt. Zirtiri Residential Science College Page 2


If you click the icon on the top left corner, it opens the control menu, which contains the
various commands to control the form like to move control from one place to another
place, to maximize or minimize.

Form Properties
Following table lists down a few important properties related to a form. These properties
can be set or read during application execution:

S.N Properties Description


1 AutoScale This Boolean property determines whether the controls you place
on the form are automatically scaled to the height of the current
font. The default value of this property is True. This is a property of
the form, but it affects the controls on the form.
2 AutoScroll This Boolean property indicates whether scroll bars will be
automatically attached to the form if it is resized to a point that not
all its controls are visible.
3 BackColor Sets the form background color.
4 BorderStyle The BorderStyle property determines the style of the form's border
and the appearance of the form:
 None: Borderless window that can't be resized.
 Sizable: This is default value and will be used for resizable
window that's used for displaying regular forms.
 Fixed3D: Window with a visible border, "raised" relative to
the main area. In this case, windows can't be resized.
 FixedDialog: A fixed window, used to create dialog boxes.
 FixedSingle: A fixed window with a single line border.
 FixedToolWindow: A fixed window with a Close button
only.
 SizableToolWindow: Same as the FixedToolWindow but
resizable.
5 Enabled If True, allows the form to respond to mouse and keyboard events;
if False, disables form.
6 Font This property specify font type, style, size

GUI PROGRAMMING: Govt. Zirtiri Residential Science College Page 3


7 Name This is the actual name of the form.
8 Text The text, which will appear at the title bar of the form.
9 Width This is the width of the form in pixel.

Form Events
Following table lists down various important events related to a form.
S.N Event Description
1 Click Occurs when the form is clicked.
2 DoubleClick Occurs when the form control is double-clicked.
3 GotFocus Occurs when the form control receives focus.
4 KeyDown Occurs when a key is pressed while the form has focus.
5 KeyPress Occurs when a key is pressed while the form has focus.
6 KeyUp Occurs when a key is released while the form has focus.
7 LostFocus Occurs when the form loses focus.
8 MouseDown Occurs when the mouse pointer is over the form and a mouse
button is pressed.
9 MouseEnter Occurs when the mouse pointer enters the form.
10 MouseMove Occurs when the mouse pointer is moved over the form.
11 MouseUp Occurs when the mouse pointer is over the form and a mouse
button is released.

TEXTBOX
Text box controls allow entering text on a form at runtime. By default, it takes a single line
of text, however, you can make it accept multiple texts and even add scroll bars to it.
Let's create a text box by dragging a Text Box control from the Toolbox and dropping it on
the form.

The Properties of the TextBox Control


The following are some of the commonly used properties of the TextBox control:

GUI PROGRAMMING: Govt. Zirtiri Residential Science College Page 4


S.N Property Description
1 Font Gets or sets the font of the text displayed by the
control.
2 ForeColor Gets or sets the foreground color of the control.
3 Multiline Gets or sets a value indicating whether this is a
multiline TextBox control.
4 PasswordChar Gets or sets the character used to mask characters of a
password in a single-line TextBox control.
5 ReadOnly Gets or sets a value indicating whether text in the text
box is read-only.
6 ScrollBars Gets or sets which scroll bars should appear in a
multiline TextBox control. This property has values:
 None * Horizontal
 Vertical * Both
7 Text Gets or sets the current text in the TextBox.
8 TextAlign Gets or sets how text is aligned in a TextBox control.
This property has values: Left, Right and Center
9 Name Name of textbox control.

Events of the TextBox Control


The following are some of the commonly used events of the Text control:
S.N Event Description
1 Click Occurs when the control is clicked.
2 DoubleClick Occurs when the control is double-clicked.
3 KeyDown Occurs when a key is pressed while the textbox has focus.
4 KeyPress Occurs when a key is pressed while the textbox has focus.
5 KeyUp Occurs when a key is released while the textbox has focus.
6 LostFocus Occurs when the textbox loses focus.

LABEL
The Label control represents a standard Windows label. It is generally used to display
some informative text on the GUI which is not changed during runtime.

GUI PROGRAMMING: Govt. Zirtiri Residential Science College Page 5


Properties of the Label Control
The following are some of the commonly used properties of the Label control:
S.N Property Description
1 Name Name of the Label Control
2 BorderStyle Gets or sets the border style for the control.
3 Font Gets or sets the font of the text displayed by the control.
4 ForeColor Gets or sets the foreground color of the control.
5 Text Gets or sets the text associated with this control.
6 TextAlign Gets or sets the alignment of text in the label.

Events of the Label Control


The following are some of the commonly used events of the Label control:
S.N Event Description
1 Click Occurs when the control is clicked.
2 DoubleClick Occurs when the control is double-clicked.
3 GotFocus Occurs when the control receives focus.
4 Leave Occurs when the input focus leaves the control.
5 LostFocus Occurs when the control loses focus.
6 TextChanged Occurs when the Text property value changes.

BUTTON
The Button control represents a standard Windows button. It is generally used to generate
a Click event by providing a handler for the Click event.

GUI PROGRAMMING: Govt. Zirtiri Residential Science College Page 6


Properties of the Button Control
The following are some of the commonly used properties of the Button control:
S.N Property Description
1 Enabled If True, allows the form to respond to mouse and keyboard
events; if False, disables form.

2 BackColor Gets or sets the background color of the control.


3 ForeColor Gets or sets the foreground color of the control.
4 Image Gets or sets the image that is displayed on a button control.
5 TabIndex Gets or sets the tab order of the control within its container.
6 Text Gets or sets the text associated with this control.
7 Name Name of the Button Control

Events of the Button Control


The following are some of the commonly used events of the Button control:
S.N Event Description
1 Click Occurs when the control is clicked.
2 DoubleClick Occurs when the user double-clicks the Button control.
3 GotFocus Occurs when the control receives focus.
4 MouseMove Occurs when the mouse pointer is moved over the control.
5 MouseUp Occurs when the mouse pointer is over the control and a mouse
button is released.

LISTBOX

GUI PROGRAMMING: Govt. Zirtiri Residential Science College Page 7


The ListBox represents a Windows control to display a list of items to a user. A user can
select an item from the list. It allows the programmer to add items at design time by using
the properties window or at the runtime.

You can populate the list box items either from the properties window or at runtime. To
add items to a ListBox, select the ListBox control and get to the properties window, for the
properties of this control. Click the ellipses (...) button next to the Items property. This
opens the String Collection Editor dialog box, where you can enter the values one at a line.

Properties of the ListBox Control


The following are some of the commonly used properties of the ListBox control:
S.N Property Description
1 Name This is the actual name of the control.

2 Items Gets the items of the list box.


3 MultiColumn Gets or sets a value indicating whether the list box supports
multiple columns.
4 SelectedIndex Gets or sets the zero-based index of the currently selected
item in a list box.
5 SelectedItem Gets or sets the currently selected item in the list box.
6 size This is the size (heightxwidth) of the control in pixel.

7 visible If true, the control is visible at runtime, if false the control


hides or invisible.

Events of the ListBox Control


The following are some of the commonly used events of the ListBox control:
S.N Event Description
1 Click Occurs when a list box is selected.

GUI PROGRAMMING: Govt. Zirtiri Residential Science College Page 8


Occurs when the SelectedIndex property of a list box is
2 SelectedIndexChanged
changed.
3 DoubleClick Occurs when the control is double-clicked.
4 GotFocus Occurs when the control receives focus.
5 LostFocus Occurs when the control loses focus.

COMBOBOX
The ComboBox control is used to display a drop-down list of various items. It is a
combination of a text box in which the user enters an item and a drop-down list from
which the user selects an item.

You can populate the list box items either from the properties window or at runtime. To
add items to a ComboBox, select the ComboBox control and go to the properties window,
click on item properties and you can add the item list of this control.

Properties of the ComboBox Control


The following are some of the commonly used properties of the ComboBox control:
S.N Property Description
1 Enabled If True, allows the control to respond to mouse and keyboard
events; if False, disables control.

2 Font This property specify font type, style, size

3 Name This is the actual name of the control.

4 Items Gets an object representing the collection of the items contained in


this ComboBox.
5 MaxLength Gets or sets the maximum number of characters a user can enter
in the editable area of the combo box.
6 SelectedIndex Gets or sets the index specifying the currently selected item.
7 SelectedItem Gets or sets currently selected item in the ComboBox.

Events of the ComboBox Control

GUI PROGRAMMING: Govt. Zirtiri Residential Science College Page 9


The following are some of the commonly used events of the ComboBox control:
S.N Event Description
1 SelectedIndexChanged Occurs when the SelectedIndex property of a
ComboBox control has changed.
2 Click Occurs when the control is clicked.
3 DoubleClick Occurs when the control is double-clicked.
4 GotFocus Occurs when the control receives focus.
5 KeyDown Occurs when a key is pressed while the control has
focus.
6 KeyPress Occurs when a key is pressed while the control has
focus.

CHECKBOX
The CheckBox control allows the user to set true/false or yes/no type options. The user
can select or deselect it. When a check box is selected it has the value True, and when it is
cleared, it holds the value False.

The CheckBox control has three states, checked, unchecked and indeterminate. In the
indeterminate state, the check box is grayed out. To enable the indeterminate state,
the ThreeState property of the check box is set to be True.

Properties of the CheckBox Control


The following are some of the commonly used properties of the CheckBox control:
S.N Property Description
1 Font This property specify font type, style, size

2 Name This is the actual name of the control.

3 CheckAlign Gets or sets the horizontal and vertical alignment of the check mark
on the check box.
4 Checked Gets or sets a value indicating whether the check box is selected.
5 Text Gets or sets the caption of a check box.

GUI PROGRAMMING: Govt. Zirtiri Residential Science College Page 10


Events of the CheckBox Control
The following are some of the commonly used events of the CheckBox control:
S.N Event Description
1 Click Occurs when the control is clicked.
2 DoubleClick Occurs when the control is double-clicked.
3 GotFocus Occurs when the control receives focus.
4 KeyDown Occurs when a key is pressed while the control has
focus.
5 KeyPress Occurs when a key is pressed while the control has
focus.

PICTUREBOX
The PictureBox control is used for displaying images on the form. The Image property of
the control allows you to set an image both at design time and at run time.

Properties of the PictureBox Control


The following are some of the commonly used properties of the PictureBox control:
S.N Property Description
1 ErrorImage Gets or specifies an image to be displayed when an error
occurs during the image-loading process .
2 Image Gets or sets the image that is displayed in the control.
3 ImageLocation Gets or sets the path or the URL for the image displayed in
the control.
4 SizeMode Determines the size of the image to be displayed in the
control. This property takes its value from the
PictureBoxSizeMode enumeration, which has values:
 Normal - the upper left corner of the image is placed
at upper left part of the picture box

GUI PROGRAMMING: Govt. Zirtiri Residential Science College Page 11


 StrechImage - allows stretching of the image
 AutoSize - allows resizing the picture box to the size
of the image
 CenterImage - allows centering the image in the
picture box
 Zoom - allows increasing or decreasing the image size
to maintain the size ratio.
5 Name This is the actual name of the control.

6 Text Gets or sets the text for the picture box.


7 Visible If true, the control is visible at runtime, if false the control
hides or invisible.

8 Enabled If True, allows the control to respond to mouse and keyboard


events; if False, disables control.

Events of the PictureBox Control


The following are some of the commonly used events of the PictureBox control:
S.N Event Description
1 Click Occurs when the control is clicked.
2 DoubleClick Occurs when the control is double-clicked.
3 FontChanged Occurs when the value of the Font property changes.
4 ForeColorChanged Occurs when the value of the ForeColor property changes.
5 KeyPress Occurs when a key is pressed when the control has focus.
6 KeyUp Occurs when a key is released when the control has focus.

RADIOBUTTON
The RadioButton control is used to provide a set of mutually exclusive options. The user
can select one radio button in a group. If you need to place more than one group of radio
buttons in the same form, you should place them in different container controls like a
GroupBox control.

GUI PROGRAMMING: Govt. Zirtiri Residential Science College Page 12


Properties of the RadioButton Control
The following are some of the commonly used properties of the RadioButton control:
S.N Property Description
1 Checked Gets or sets a value indicating whether the control is checked.
2 Text Gets or sets the caption for a radio button.
3 Name This is the actual name of the control.

4 Visible If true, the control is visible at runtime, if false the control hides
or invisible.

5 Enabled If True, allows the control to respond to mouse and keyboard


events; if False, disables control.

6 Font This property specify font type, style, size

Events of the RadioButton Control


The following are some of the commonly used events of the RadioButton control:
S.N Event Description
1 CheckedChanged Occurs when the value of the Checked property of the
RadioButton control is changed.
2 Click Occurs when the control is clicked.
3 DoubleClick Occurs when the control is double-clicked.
4 GotFocus Occurs when the control receives focus.
5 KeyDown Occurs when a key is pressed while the control has focus.
6 KeyPress Occurs when a key is pressed while the control has focus.

GUI PROGRAMMING: Govt. Zirtiri Residential Science College Page 13


PANEL
The Panel control is a container control that is used to host a group of similar child
controls. A panel allows you to design good-looking forms by adjusting colors and other
properties. A panel is also a regularly used control container because it holds and carries
controls placed on it. When a panel moves, it does so with the controls placed on it. When a
panel is visible, the controls placed on it can be visible too, unless they have their own
visibility removed. When a panel is hidden, its child controls are hidden too, regardless of
their own visibility status. Panels are not transparent. Therefore, their color can be
changed to control their background display.

Properties of Panel
SN Name Description

1 BackColor Gets or sets the background color for the control.

2 Enabled Gets or sets a value indicating whether the control can


respond to user interaction.

3 Font Gets or sets the font of the text displayed by the control.

4 ForeColor Gets or sets the foreground color of the control.

5 Name Gets or sets the name of the control.

6 Size Gets or sets the height and width of the control.

7 Text Gets or sets the text of the control

8 Visible Gets or sets a value indicating whether the control and all
its child controls are displayed.

Events of Panels
SN Name Description

1 Click Occurs when the control is clicked.

2 DoubleClick Occurs when the control is double-clicked.

3 DragDrop Occurs when a drag-and-drop operation is completed.

4 KeyPress Occurs when any key is pressed

5 KeyUp Occurs when any key is released

6 LostFocus Occurs when the control loses focus.

GUI PROGRAMMING: Govt. Zirtiri Residential Science College Page 14


7 MouseDown Occurs when the mouse pointer is over the control and a mouse
button is pressed.

8 MouseMove Occurs when the mouse pointer is moved over the control.

9 MouseUp Occurs when the mouse pointer is over the control and a mouse
button is released.

TIMER
With the Timer Control, we can control programs in millisecond, seconds, minutes and
even in hours. The Timer Control allows us to set Interval property in milliseconds (1
second is equal to 1000 milliseconds). For example, if we want to set an interval of two
minute we set the value at Interval property as 120000, means 120x1000 .
The Timer Control starts its functioning only after its Enabled property is set to True, by
default Enabled property is False.

Properties of Timer
SN Name Description

1 Name This is the actual name of the control.

2 Enabled Gets or sets a value indicating whether the Timer should raise
the Elapsed event.

3 Interval Gets or sets the interval, expressed in milliseconds, at which to raise


the Elapsed event.

4 Modifier Indicates the visibility level of the object

5 Tag User Defined data associated with the object

Events of Timer
SN Name Description

1 Disposed Occurs when the component is disposed by a call to


the Dispose method.

2 Tick Occurs when the timer’s time change.

GUI PROGRAMMING: Govt. Zirtiri Residential Science College Page 15


LISTVIEW
The ListView control is used to display a list of items. Along with the TreeView control, it
allows you to create a Windows Explorer like interface.

Properties of the ListView Control


The following are some of the commonly used properties of the ListView control:
S.N Property Description
1 Alignment Gets or sets the alignment of items in the control.
2 BackColor Gets or sets the background color.
3 Columns Gets the collection of all column headers that appear in the
control.
4 Name This is the actual name of the control.

5 Visible If true, the control is visible at runtime, if false the control hides
or invisible.

6 Enabled If True, allows the control to respond to mouse and keyboard


events; if False, disables control.

7 SelectedItems Gets the items that are selected in the control.


8 ShowGroups Gets or sets a value indicating whether items are displayed in
groups.
9 View Gets or sets how items are displayed in the control. This
property has the following values:
 LargeIcon - displays large items with a large 32 x 32
pixels icon.
 SmallIcon - displays items with a small 16 x 16 pixels
icon
 List - displays small icons always in one column
 Details - displays items in multiple columns with column

GUI PROGRAMMING: Govt. Zirtiri Residential Science College Page 16


headers and fields
 Tile - displays items as full-size icons with item labels
and sub-item information.
Events of the ListView Control
The following are some of the commonly used events of the ListView control:
S.N Event Description
1 Click Occurs when the control is clicked.
2 DoubleClick Occurs when the control is double-clicked.
3 GotFocus Occurs when the control receives focus.
4 KeyDown Occurs when a key is pressed while the control has focus.
5 KeyPress Occurs when a key is pressed while the control has focus.
6 KeyUp Occurs when a key is released while the control has focus.

TREEVIEW
The TreeView control is used to display hierarchical representations of items similar to
the ways the files and folders are displayed in the left pane of the Windows Explorer. Each
node may contain one or more child nodes.

Properties of the TreeView Control


The following are some of the commonly used properties of the TreeView control:
S.N Property Description
1 BackColor Gets or sets the background color for the control.
2 BackgroundImage Gets or set the background image for the TreeView
control.
3 Font Gets or sets the font of the text displayed by the control.
4 ForeColor The current foreground color for this control, which is
the color the control uses to draw its text.

GUI PROGRAMMING: Govt. Zirtiri Residential Science College Page 17


6 Nodes Gets the collection of tree nodes that are assigned to the
tree view control.
7 Text Gets or sets the text of the TreeView.
8 Name This is the actual name of the control.

9 Visible If true, the control is visible at runtime, if false the


control hides or invisible.

Events of the TreeView Control


The following are some of the commonly used events of the TreeView control:
S.N Event Description
1 AfterCollapse Occurs after the tree node is collapsed.
2 AfterExpand Occurs after the tree node is expanded.
3 AfterSelect Occurs after the tree node is selected.
4 BeforeCollapse Occurs before the tree node is collapsed.
5 BeforeExpand Occurs before the tree node is expanded.
6 BeforeLabelEdit Occurs before the tree node label text is edited.
7 BeforeSelect Occurs before the tree node is selected.
8 ItemDrag Occurs when the user begins dragging a node.
9 NodeMouseClick Occurs when the user clicks a TreeNode with the mouse.
10 NodeMouseDoub Occurs when the user double-clicks a TreeNode with the mouse.
leClick

DATE TIME PICKER


The DateTimePicker control allows selecting a date and time by editing the displayed
values in the control. If you click the arrow in the DateTimePicker control, it displays a
month calendar, like a combo box control. The user can make selection by clicking the
required date. The new selected value appears in the text box part of the control.

GUI PROGRAMMING: Govt. Zirtiri Residential Science College Page 18


Properties
Sl.No. Property Description
1 BackColor Gets or sets a value indicating the background color of the
DateTimePicker control.
2 ForeColor Gets or sets the foreground color of the DateTimePicker
control.
3 Format Gets or sets the format of the date and time displayed in the
control.
4 MaxDate Gets or sets the maximum date and time that can be selected in
the control.
5 MinDate Gets or sets the minimum date and time that can be selected in
the control.
6 Text Gets or sets the text associated with this control
.
7 Value Gets or sets the date/time value assigned to the control.

Events
Sr.No. Event Description
1 Click Occurs when the control is clicked.

2 DoubleClick Occurs when the control is double-clicked.

3 DragDrop Occurs when a drag-and-drop operation is completed.

4 FormatChanged Occurs when the Format property value has changed.

5 MouseClick Occurs when the control is clicked with the mouse.

6 MouseDoubleClick Occurs when the control is double-clicked with the mouse.

7 TextChanged Occurs when the value of the Text property changes.

8 ValueChanged Occurs when the Value property changes.

GUI PROGRAMMING: Govt. Zirtiri Residential Science College Page 19


LINK LABEL

The LinkLabel control displays a label that is associated with a hyperlink, and by default it's
blue and underlined. In addition when you mouse over this control it displays a pointing
hand cursor, which makes it easy to be recognized as a link on a web page. If you click the
LinkLabel, it raises its LinkClicked event, so you can take whatever action is appropriate.
Properties:
Sl. No Properties Description
1 TextAlign Text alignment is set using this property either to right or left
by giving values 0 or 1
2 Text Property used for the label text.
3 Name This is the actual name of the control.
4 Visible If true, the control is visible at runtime, if false the control
hides or invisible.
5 ActiveLinkColor Property used to specify the color of the active link.
6 DisabledLinkColor Property used to specify the color of the disabled link.
7 LinkColor Property is used to specify the color for a normal link.
8 VisitedLinkColor Property is used to specify the color for a visited link.

Events:
Sl No Events Description
1 LinkClicked Triggered when the link is clicked.
2 DoubleClick Occurs when the control is double clicked
3 GotFocus Occurs when the control receives focus.

4 KeyDown Occurs when a key is pressed while the control has focus.

5 KeyPress Occurs when a key is pressed while the control has focus.

6 KeyUp Occurs when a key is released while the control has focus.

7 LostFocus Occurs when the control loses focus.

GUI PROGRAMMING: Govt. Zirtiri Residential Science College Page 20


DIALOG BOXES
There are many built-in dialog boxes to be used in Windows forms for various tasks like
opening and saving files, printing a page, providing choices for colors, fonts, page setup,
etc., to the user of an application. The following diagram shows the common dialog class
inheritance:

OPENFILE DIALOG
The OpenFileDialog control prompts the user to open a file and allows the user to select a
file to open. The user can check if the file exists and then open it. The OpenFileDialog
control class inherits from the abstract class FileDialog.
Following is the Open File dialog box:

Properties of the OpenFileDialog Control


S.N Property Description

1 Name Indicates the name of the control

2 Title Indicates the title of the dialog box

3 FileName Gets or sets a string containing the file name selected in the file

GUI PROGRAMMING: Govt. Zirtiri Residential Science College Page 21


dialog box.

4 InitialDirectory Gets or sets the initial directory displayed by the file dialog box.

5 Multiselect Gets or sets a value indicating whether the dialog box allows
multiple files to be selected.

6 ShowHelp Gets or sets a value indicating whether the Help button is


displayed in the file dialog box.

Methods of the OpenFileDialog Control


S.N Method Name Description

1 OpenFile Opens the file selected by the user, with read-only permission. The
file is specified by the FileName property.

2 Reset Resets all options to their default value.

SAVEFILE DIALOG
The SaveFileDialog control prompts the user to select a location for saving a file and
allows the user to specify the name of the file to save data. The SaveFileDialog control
class inherits from the abstract class FileDialog. Following is the Save File dialog box:

Properties of the SaveFileDialog Control


S.N Property Description
1 Name Indicates the name of the control
2 DefaultExt Gets or sets the default file name extension.
3 FileName Gets or sets a string containing the file name selected in the file
dialog box.

GUI PROGRAMMING: Govt. Zirtiri Residential Science College Page 22


4 FileNames Gets the file names of all selected files in the dialog box.
5 InitialDirectory Gets or sets the initial directory displayed by the file dialog
box.
6 ShowHelp Gets or sets a value indicating whether the Help button is
displayed in the file dialog box.
7 Title Gets or sets the file dialog box title.

Methods of the SaveFileDialog Control


S.N Method Name Description
1 OpenFile Opens the file with read/write permission.
2 Reset Resets all dialog box options to their default values.

FONTDIALOG
It prompts the user to choose a font from among those installed on the local computer and
lets the user select the font, font size, and color. It returns the Font and Color objects.
Following is the Font dialog box:

Properties of the FontDialog Control


S.N Property Description
1 Name Indicates the name of the control
2 Color Gets or sets the selected font color.
3 Font Gets or sets the selected font.
4 MaxSize Gets or sets the maximum point size a user can select.
5 MinSize Gets or sets the minimum point size a user can select.
6 ShowApply Gets or sets a value indicating whether the dialog box contains
an Apply button.

GUI PROGRAMMING: Govt. Zirtiri Residential Science College Page 23


7 ShowColor Gets or sets a value indicating whether the dialog box displays
the color choice.

Methods of the FontDialog Control


S.N Method Name Description
1 Reset Resets all options to their default values.
2 RunDialog When overridden in a derived class, specifies a common
dialog box.
3 ShowDialog Runs a common dialog box with a default owner.

COLORDIALOG
The ColorDialog control class represents a common dialog box that displays available
colors along with controls that enable the user to define custom colors. It lets the user
select a color. The main property of the ColorDialog control is Color, which returns
a Colorobject. Following is the Color dialog box:

Properties of the ColorDialog Control


S.N Property Description
1 Name Indicates the name of the control
2 Color Gets or sets the color selected by the user.
3 CustomColors Gets or sets the set of custom colors shown in the dialog box.
4 ShowHelp Gets or sets a value indicating whether a Help button appears in
the color dialog box.
5 SolidColorOnly Gets or sets a value indicating whether the dialog box will restrict
users to selecting solid colors only.
Methods of the ColorDialog Control

GUI PROGRAMMING: Govt. Zirtiri Residential Science College Page 24


S.N Method Name Description
1 Reset Resets all options to their default values, the last selected color to
black, and the custom colors to their default values.
2 RunDialog When overridden in a derived class, specifies a common dialog box.
3 ShowDialog Runs a common dialog box with a default owner.

PRINTDIALOG
The PrintDialog control lets the user to print documents by selecting a printer and
choosing which sections of the document to print from a Windows Forms application.
Following is the Print dialog box:

Properties of the PrintDialog Control


S.N Property Description
1 AllowCurrentPage Gets or sets a value indicating whether the Current
Page option button is displayed.
2 AllowPrintToFile Gets or sets a value indicating whether the Print to file check
box is enabled.
3 AllowSelection Gets or sets a value indicating whether the Selectionoption
button is enabled.
4 PrinterSettings Gets or sets the printer settings the dialog box modifies.
5 PrintToFile Gets or sets a value indicating whether the Print to file check
box is selected.
6 Name Indicates the name of the control

Methods of the PrintDialog Control


S.N Method Name Description

GUI PROGRAMMING: Govt. Zirtiri Residential Science College Page 25


1 Reset Resets all options to their default values.
2 RunDialog When overridden in a derived class, specifies a common dialog box.
3 ShowDialog Runs a common dialog box with a default owner.

DESIGNING MENUS :
MENU STRIP
The MenuStrip control represents the container for the menu structure.
The MenuStrip control works as the top-level container for the menu structure. The
ToolStripMenuItem class and the ToolStripDropDownMenu class provide the
functionalities to create menu items, sub menus and drop-down menus.

Properties of MenuStrip:
S.N Properties Description
1 Name This is the actual name of the menu.
2 Text The text, which will be display in the menu.
3 Items Collection of items or menu can be added, delete and modify
4 Font This property specify font type, style, size
5 Image Display a small icon for menu
6 Shortcutkey Used to create shortcut for the menu.
7 Size Size of the menu in pixel.
Visible Determin whether the item is visible or not

Events of MenuStrip:
S.N Event Description
1 Click Occurs when the menu is clicked.
2 DoubleClick Occurs when the menu control is double-clicked.

GUI PROGRAMMING: Govt. Zirtiri Residential Science College Page 26


3 GotFocus Occurs when the menu control receives focus.
4 LostFocus Occurs when the menu loses focus.
5 MouseDown Occurs when the mouse pointer is over the menu and a mouse
button is pressed.
6 MouseEnter Occurs when the mouse pointer enters the menu.
7 MouseMove Occurs when the mouse pointer is moved over the menu.
8 MouseUp Occurs when the mouse pointer is over the menu and a mouse
button is released.

CREATING MENUS
Typically, the menu bar contains five menus: File, Edit, View, Tools, and Help, and a few
menu items and submenu items. This enables you to fully exercise the features of the menu
controls.
You will be implementing code behind the menu items to demonstrate the menu and how
to add code to your menu items, so let’s get started.

In this example, you will learn how to create menus and submenus.
1. Start Visual Studio 2010 and click File ➪ New Project. In the New Project dialog, select
Windows Forms Application in the Templates pane and enter the project name Windows
Forms Menus in the Name field. Click the OK button to have the project created.
2. Click the form in the Forms Designer and set the properties of the form as desire.
3. Drag a MenuStrip control from the Toolbox and drop it on your form. It is automatically
positioned at the top of your form.

GUI PROGRAMMING: Govt. Zirtiri Residential Science College Page 27


4. Click the MenuStrip control on top of the form, a textbox will appear, type the menu
name, another two textbox (right and bottom) will appear automatically. Now you can
create menu and sub menu according to your requirements.
5. In the Properties window, you can manipulate each menu property such as name,
alignment, font, enable, image, shortcut key etc.
6. On double clicking each menu, you can create its corresponding events and write the
code correspondingly.

CONTEXTMENU
Context menus are menus that pop up when a user clicks the right mouse button on a
control or window. They provide users with quick access to the most commonly used
commands for the control that they are working with. Context menus are customized for
the control that you are working with; and in more complex applications, such as Visual
Studio 2010 or Microsoft Word, they provide quick access to the commands for the task
that is being performed.
Visual Studio 2010 provides a ContextMenuStrip control that you can place on your form
and customize, just as you did the MenuStrip control. However, the main difference
between the MenuStrip control and the ContextMenuStrip control is that you can create
only one top-level menu with the ContextMenuStrip control. You can still create submenu
items with the ContextMenuStrip if needed.

Properties of ContextMenuStrip:
S.N Properties Description
1 Name This is the actual name of the menu.
2 Text The text, which will be display in the menu.
3 Items Collection of items or menu can be added, delete and modify
4 Font This property specify font type, style, size
5 Image Display a small icon for menu
6 Shortcutkey Used to create shortcut for the menu.
7 Size Size of the menu in pixel.
Visible Determin whether the item is visible or not

Events of ContextMenuStrip:
S.N Event Description

GUI PROGRAMMING: Govt. Zirtiri Residential Science College Page 28


1 Click Occurs when the menu is clicked.
2 DoubleClick Occurs when the menu control is double-clicked.
3 GotFocus Occurs when the menu control receives focus.
4 LostFocus Occurs when the menu loses focus.
5 MouseDown Occurs when the mouse pointer is over the menu and a mouse
button is pressed.
6 MouseEnter Occurs when the mouse pointer enters the menu.
7 MouseMove Occurs when the mouse pointer is moved over the menu.
8 MouseUp Occurs when the mouse pointer is over the menu and a mouse
button is released.

Creating Context Menus


1. Go to the Forms Designer in your Windows Forms Menus project and then click the
Toolbox to locate the ContextMenuStrip control. Drag and drop it onto your form,
contextmenustrip appear at the top of the form and an object is added at the bottom of the
development environment just as the MenuStrip control was.
2. In the Properties window, click the button next to the Items property. You’ll be adding
five menu items in your context menu in the next several steps.
3. Click the ContextMenuStrip control on top of the form, only one top-level menu can be
created with the ContextMenuStrip control. But you can create submenu items with the
ContextMenuStrip if needed.
4. Type the first menu name, another two textbox (right and bottom) will appear
automatically. Now you can create menu and sub menu according to your requirements.
5. In the Properties window, you can manipulate each menu property such as name,
alignment, font, enable, image, shortcut key etc.
6. On double clicking each menu, you can create its corresponding events and write the
code correspondingly. A context menu can be called using the method “show”.
7. Now you can add any control on the form and set the contextmenustrip property to the
created contextmenustrip. So, when right clicking on that control, the context menu
appears.

ACCESS SHORCUT KEYS


Shortcut keys enable you to invoke the menu item without displaying the menus at all.
Shortcut keys usually consist of a control key and a letter, such as Ctrl+X to cut text.
Visual Basic .NET not only helps you program like a professionals even if you have no
programming experience at all, it offers many handy tools to help. One of those tools is a
whole range of shortcut keys you can use to cut, copy, and generally direct a program to do
what you want it to. The command names, functions, and the shortcut keys are in the
following table.

GUI PROGRAMMING: Govt. Zirtiri Residential Science College Page 29


General
Alt + F11 This toggles switch displays the VBE from the application window and vice
versa.
Ctrl + G Displays the Immediate window.
Ctrl + F Opens the Find dialog box.
F3 Finds Next.
Shift + F3 Finds Previous.
Ctrl + H Opens the Replace dialog box.
Ctrl + Z Cancels the last keyboard stroke or the last mouse operation.
F5 Runs the current procedure or continues execution after pausing.
Ctrl + Break Halts a procedure.
Shift + F5 Terminates a procedure and resets all variables to their default values.
Shift + F10 Displays the active window's shortcut menu.
Ctrl + R Opens the Project Explorer.
F4 Opens the Properties window.
F2 Opens the Object Browser.
F1 Opens VBA Help.
F7 Gives focus to the open module window.
Code module
Ctrl + Down Arrow Selects the next procedure.
Ctrl + Up Arrow Selects the previous procedure.
Ctrl + Page Down Shifts one screen down.
Ctrl + Page Up Shifts one screen up.
Ctrl + Shift + F2 Goes to the last position.
Ctrl + Home Goes to the beginning of module. (Also in the Immediate window.)
Ctrl + End Goes to the end of module. (Also in the Immediate window.)
Ctrl + Right Arrow Moves one word to the right.
Ctrl + Left Arrow Moves one word to the left.
End Moves to the end of the line.
Home Moves to the beginning of the line.
Ctrl + Y Deletes the current line.
Ctrl + Delete Deletes to the end of a word.
Ctrl + Shift + F9 Clears all breakpoints.

GUI PROGRAMMING: Govt. Zirtiri Residential Science College Page 30


Project Explorer
F7 Shift + Enter Opens the selected file's module.
Home Selects the first file in the list.
End Selects the last file in the list.
Right arrow Expands a sublist.
Left arrow Collapses a sublist.
Up arrow Moves up the list one item at a time.
Down arrow Moves down the list one item at a time.

GUI PROGRAMMING: Govt. Zirtiri Residential Science College Page 31

You might also like