Gui Unit 2
Gui Unit 2
Gui Unit 2
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.
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.
Mouse events
Keyboard events
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 −
Form Properties
Following table lists down a few important properties related to a form. These properties
can be set or read during application execution:
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.
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.
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.
LISTBOX
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.
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.
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.
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.
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.
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.
4 Visible If true, the control is visible at runtime, if false the control hides
or invisible.
Properties of Panel
SN Name Description
3 Font Gets or sets the font of the text displayed by 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
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
2 Enabled Gets or sets a value indicating whether the Timer should raise
the Elapsed event.
Events of Timer
SN Name Description
5 Visible If true, the control is visible at runtime, if false the control hides
or invisible.
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.
Events
Sr.No. Event Description
1 Click Occurs when the control is clicked.
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.
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:
3 FileName Gets or sets a string containing the file name selected in the file
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.
1 OpenFile Opens the file selected by the user, with read-only permission. The
file is specified by the FileName property.
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:
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:
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:
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:
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.
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.
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