Professional Documents
Culture Documents
OBJECTIVES
With this topic, the students should be able to:
COMPARING COMMAND-LINE
AND FORM-BASED INTERFACES
command-line interface
It display textual prompts and gets end user input one line at a time.
Button
Label
Command-line interface
Description
Control
The end user should always control the application, rather than the
other way around.
User-friendliness
The interface should help the end user accomplish tasks and not call
attention to itself.
Example
Too many different fonts or images can be distracting
Intuitiveness
Description
Consistency
Feedback
The interface should provide clear and immediate feedback to the end user.
Example
If the user adds a record to a file, the interface should inform the end
user that the record was added.
If the end user makes an error when entering data, the interface
should communicate the cause of the error and suggest possible
solutions to the problem.
Description
Graphics
Input
The interface should minimize situations in which the end user needs
to switch input modes from the mouse to the keyboard and back
again.
Label prompt
User Interface
File name:
Button to
display a
dialog box
Center
Normal
Button to
resize the
image
Stretch
Clear
Exit
Button to
clear the
image
Button to
exit the
application
Description
Alignment
Balance
Description
Appropriate color
Function grouping
Consistent sizing
Display small
icons enabled
Solution name
The only visual parts of a Console Application project are the textual
messages that appear in a Command Prompt window. Windows Forms
Application projects are graphical applications having one or more windows
with which the end user interacts.
Console Application projects are procedural. Windows Forms Application
projects are event-driven.
The startup object for a Console Application project is typically a procedure
named Main. The startup object for a Windows Forms Application project is
typically a form.
Toolbox window
It is a tool window containing the visual tools, called controls.
These are special category of classes that appear on a form.
Properties window
It provides a visual way to set the properties for an object at design time.
Example
A form has a property named Text containing a string that appears in the forms title
bar.
Solution Explorer
Toolbox
Properties window
System.Drawing namespace
It is used by Windows Forms Application projects to render (draw) the form and the visual
objects appearing on the form.
System.Windows.Forms namespace
It contains those classes that make up the visual forms and the objects created on those
forms.
Project support
files are the same
Assembly references
Form files
1. Select the form name in the Solution Explorer, and then select View, Designer on
the menu bar.
2. Right-click the form name in the Solution Explorer, and then select View Designer
from the context menu.
3. Double-click the form name in the Solution Explorer.
control box (optional and located along the right side of the title bar)
It has buttons to minimize, maximize, and restore a maximized form to its original size.
It can be made visible or hidden.
Restore / Maximize button (toggle and appears as the center button in the control box)
Maximize button
It causes the form to maximize, thus filling the desktop.
Restore button
It restores the form to its original size.
Close button
It closes the form or the application itself.
Close method
It closes the form, thereby unloading it from memory.
Hide method
It makes a form invisible, but the form is not unloaded from memory.
Show method
It displays a form.
AcceptButton property
It contains the name of a Button control instance.
Pressing the Enter key causes the Click event to fire for the button designated as the
Accept button.
There can be only one Accept button per form.
It is often called the Default button
BackColor property
It specifies the forms background color.
ControlBox property
If set to True, it appears on the forms title bar.
If set to False, it does not appear.
Rightmost button always appears and is enabled when the control box is visible.
MaximizeBox property
If set to True, the Restore/Maximize buttons are enabled.
If set to False, the buttons are disabled.
MinimizeBox property
It defines whether the Minimize button is enabled or disabled.
FormBorderStyle property
It is used to specify the appearance of the border surrounding a form and whether the for
is resizable.
None
No border surrounds the form
FixedSingle and Fixed3D
It causes the form to appear with a flat and three-dimensional border respectively.
CancelButton property
It contains the name of the Button control instance designated as the Cancel button.
Pressing the Escape key causes the Click event to fire for the associated button.
Only one button on a form can be designated as the Cancel button.
Icon property
It contains the image that appears in the upper-left corner of the forms title bar.
Text property
It contains the caption that appears in the forms title bar.
Width and Height properties
It define the size of the form.
Values are expressed in pixels.
StarPosition property
It is used to specify where on the desktop the form will appear when it is displayed.
CenterScreen
The form is displayed in the center of the desktop.
Manuao
The position of the form is defined by the X and Y properties.
X and Y properties
It define the position of the form relative to the desktop.
Only used when the StartPosition property is set to Manual.
Values are expressed in pixels.
Load event
It fires when a form is loaded into memory each time the application is run.
It is used to perform initialization tasks, such as displaying the current date on the
form.
FormClosing event
It fires just before a form is unloaded from memory.
Me keyword
It is a special identifier used to reference the current instance of a form.
Close method -> Me.Close()
It close the current form instance, thereby ending the application.
It contains all of the visual tools (controls) necessary to create a forms user
interface.
Creating a control instance on a form
Click the desired control in the Toolbox. The selected control appears highlighted. Then,
drag the outline of the control instance into the Windows Forms Designer to define its
position and size.
It is also possible to create a control instance by double-clicking the control in the
Toolbox. The control is created at the upper-left corner of the form, or adjacent to the
most recently created control instance.
Common Controls
tab expanded
Tabs collapsed
toolbar area
It contains buttons used to change the order in which the properties appear in the list
section and whether to display the properties or events for the selected object.
Categorized button
The properties are organized based on the purpose of the property.
Display properties
by category
Display properties
alphabetically
Display properties
Object drop-down box
Toolbar
Display events
List section
Name column
Value column
Description section
The property is edited by typing the desired value in the Value column
Example
Name property store a textual value
Hierarchical
The values are set using a drill-down interface.
Example
Location property has X and Y properties.
Size property has Width and Height properties.
Location property
expanded
Hierarchical property
collapsed
Web tab
Lists those color supported by the Web.
System tab
It causes the color of a control instance to
be the same as the color defined by a
common Windows system property.
Color palette
drop-down list
It display a dialog box, allowing the programmer to select the typeface, size,
and font attributes.
Property value is
an enumeration
Click the list
arrow to display
the visual editor
Visual Editor
Windows Forms
Designer active
frmMain selected in
Object drop-down box
ControlBox
property
Sizing handles
appear
FormBorderStyle
property
Setting properties for a form.
The Height and Width properties can also be set using the Properties window,
thereby resizing the control instance.
Horizontal
snap line
Contro instance
being aligned
Vertical
snap line
INTRODUCTION TO VISUAL
STUDIO CONTROLS
Controls:
PictureBox control
It displays graphical images.
Label control
It displays text.
Button control
It performs a specific task when clicked by the end user.
OpenFileDialog control
It displays a dialog box, allowing the end user to select a file to open.
ToolTip control
It displays an informational pop-up message when the mouse hovers over another
control instance for a few seconds.
INTRODUCTION TO VISUAL
STUDIO CONTROLS
The System.Windows.Forms.Control Class
It is the base class for all visual controls.
Control Class
It supplies the basic functionality to get input from the keyboard and mouse and
to display output.
System.Windows.Forms
System.Windows.Forms.Control
System.Windows.Forms.ButtonBase
System.Windows.Forms.Button
System.Windows.Forms.Label
System.Windows.Forms.PictureBox
System.Windows.Forms.CommonDialog
System.Windows.Forms.FileDialog
System.Windows.Forms.OpenFileDialog
Hierarchical organization of control classes
INTRODUCTION TO VISUAL
STUDIO CONTROLS
Understanding the Name Property
Rules for naming identifiers:
Must begin with a letter and contain only letters, digits, and the underscore
character.
It must be unique.
INTRODUCTION TO VISUAL
STUDIO CONTROLS
Understanding the Name Property
Control and form prefixes
Control
Prefix
Button
btn
Label
lbl
OpenFileDialog
ofd
PictureBox
pic
ToolTip
tip
Form
frm
INTRODUCTION TO VISUAL
STUDIO CONTROLS
Understanding the Name Property
Common Errors
Click to collapse
Details section
INTRODUCTION TO VISUAL
STUDIO CONTROLS
Members of the Control Class
Syntax
System.Windows.Forms.Control class
Methods
Hide method
It makes the control instance invisible.
Show method
It makes the control instance visible.
Properties
Forecolor and BackColor properties
It define the text color and color appearing behind the text, respectively.
INTRODUCTION TO VISUAL
STUDIO CONTROLS
Members of the Control Class
Properties
Enable property
It stores values True or False, indicating whether the control instance will respond to
end user interaction.
AutoSize property
If set to True, the control instance is resized based on its contents.
MinimumSize property
It defines the minimum size of the control instance.
MaximumSize property
It defines the maximum size of the control instance.
INTRODUCTION TO VISUAL
STUDIO CONTROLS
Members of the Control Class
Properties
Location property
It is used to set the position of the control instance on the form.
Name property
It is used to uniquely identify the control instance.
Text property
It contains the text that appears in the visible region of the control instance.
Visible property
It stores the values True or False and indicates whether the control instance is visible
or hidden from the end user.
INTRODUCTION TO VISUAL
STUDIO CONTROLS
Members of the Control Class
Events
Click event
It fires when the end user clicks the mouse on a control instance.
INTRODUCTION TO VISUAL
STUDIO CONTROLS
The Label Control
Properties
BoderStyle property
It can be set to one of three constant values.
None
No border surrounds the label.
FixedSingle
A flat border surround the label.
Fixed3D
The control instance appears recessed.
Font property
It defines the typeface and the size of the text appearing in the label.
TextAlign property
It determines how the textual characters are aligned inside the region of the label.
INTRODUCTION TO VISUAL
STUDIO CONTROLS
The Button Control
Syntax
System.Windows.Forms.Button class
Properties
X, Y, Height, and Width properties
It define its position and size on the form.
Enabled property
If set to True, Windows fires a Click event when the end user clicks the button.
If set to False, the button text appears shaded, and Click events are not fired.
FlatStyle property
Flat
The button appears flat on the form.
INTRODUCTION TO VISUAL
STUDIO CONTROLS
The Button Control
FlatStyle property
Popup
The button appears raised when the mouse pointer hovers over the button at run
time.
Standard
The button appears raised off the forms surface.
System
The appearance of the control instance is the same as that of the one used by the
operating system.
Image property
It contains a picture that appears in the button along with the button text.
ImageAlign property
It is used to indicate where the image will appear within the buttons visible region.
INTRODUCTION TO VISUAL
STUDIO CONTROLS
The Button Control
Text property
It contains the text that appears in the buttons visible region.
It can also create a hot key for a button by inserting an ampersand character (&) in the
Text propertys value just before the character that should act as the hot key.
It is a set of keystrokes, such as Alt+C, which, when typed by the end user,
produces the same result as clicking the button.
Example
The string E&xit would cause Alt+X to be the hot key.
Events
Click event
Windows fires when the end user clicks the button.
INTRODUCTION TO VISUAL
STUDIO CONTROLS
The PictureBox Control
It can display JPEG, bitmap, and other image formats.
Syntax
System.Windows.Forms.PictureBox class
Properties
BorderStyle property
It defines the appearance of the border surrounding the control instance.
Image property
It contains the image displayed in the control instance.
ImageLocation property
It contains the name of the disk file where the image is stored.
INTRODUCTION TO VISUAL
STUDIO CONTROLS
The PictureBox Control
Properties
SizeMode property
It defines how the image appears inside the region of the control instance.
Normal
The image appears at the upper-left corner of the control instance and is not resized.
StretchImage
It causes the image to be resized such that it fills the region of the control instance.
AutoSize
The control instance resizes to fit the image.
CenterImage
It causes the image to be centered in the control instance.
Events
Click event
A PictureBox control instance can be made to work as if it were a Button control instance.
Control instance is
resized to fit image
Image appears at
its actual size
Image is stretched to
fill the control instance
INTRODUCTION TO VISUAL
STUDIO CONTROLS
The OpenFileDialog Control
It displays a dialog box, allowing the end user to select a file to open.
INTRODUCTION TO VISUAL
STUDIO CONTROLS
The OpenFileDialog Control
Method and property needed to use the OpenFileDialog control:
ShowDialog method
It displays the dialog box to the end user.
FileName property
It stores the filename selected by the end user.
Automatically generated
code appears in the
frmMain.Designer.vb file
Procedures
frmMain.Designer.vb
file selected
Code segment showing a Click event handler for a Button control instance
named btnDemo:
Private Sub btnDemo_Click( ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnDemo.Click
End Sub
Handles clause
It marks a procedure as an event handler.
Application
starts
User clicks a
button firing an
event
Application waits
for an event to
fire
Windows calls
event-handling
procedure
Event-handling
procedure
finishes
btnDemo selected
Button control
instance selected
enumeration
It is a descriptive name used in place of a numeric value.
AutoSize
CenterImage
Normal
StretchImage
FromFile method
It reads an image from a disk file stores it as an Image object.
Statement that shows how to read an image by calling the FromFile method
of the System.Drawing,Image class, and store a reference to that Image
property of the PictureBox control instance named picDemo.
picDemo.Image =
System.Drawing.Image.FromFile(C:\House1.jpg)
Hide method
It makes the control instance invisible.
Example:
lblDemo.Hide()