Upload
curlicue
View
215
Download
0
Embed Size (px)
Citation preview
8/14/2019 Lecture 3 - Displaying Dialog Boxes (VB 2008)
1/14
Lecture 3: Displaying Dialog Boxes
8/14/2019 Lecture 3 - Displaying Dialog Boxes (VB 2008)
2/14
Introduction VB .NET provides a number of built-in Dialog Boxes
Each of which provides Dialog-based advanced functionality: Displaying Messages to Users; Allowing users to Choose Fonts and Color; Allowing users to Open and Save files; Allowing users to Print from your Application.
These help you to provide a professional GUI for your Applications
Very quickly and quite impressively (RAD Development). This, and next lecture, we will discuss 4 standard Dialog Boxes:
The MessageBox (weve seen this!) But now, we examine it in more detail (more buttons; icons, etc)
The FontDialog Control
Which supports the easy, UI-based changing of Font and text-properties. The SaveFileDialog Control
Which supports the easy, Dialog-based saving of files.
The OpenFileDialog Control That allows users to conveniently open files.
8/14/2019 Lecture 3 - Displaying Dialog Boxes (VB 2008)
3/14
Dialog Boxes 1: The MessageBox
The Dialog Box used most often is the MessageBox As we know, MessageBoxes support:
Display information to users Warnings, Error Messages, Informative Messages, etc
Collect information, regarding a simple choice (A Button Click).
We have already used simple MessageBoxes, quite a bit
To display messages via MessageBox.Show().
As we will see, Show() takes various parameters, to display: A Message (1st parameter)
A Caption/Title (2nd parameter)
A Set of Buttons (3rd
parameter) A Display Icon (4th parameter)
Which Button is Default (5th parameter).
First, lets look at icons
8/14/2019 Lecture 3 - Displaying Dialog Boxes (VB 2008)
4/14
Selecting a Display Icon
One of severalDisplay Iconscan be selected for display Using the MessageBoxIcon Enumeration (a set ofSystem Constants)
An Error Icon Use the System Constant, MessageBoxIcon.Error Also called the Stop Icon:
MessageBoxIcon.Stop
A Question Icon
Use: MessageBoxIcon.Question A Warning Icon MessageBoxIcon.Warning
Also called the Exclamation Icon: MessageBoxIcon.Exclamation
An Information Icon (MessageBoxIcon.Information) Also called the Asterisk Icon:
MessageBoxIcon.Asterisk
No Icon MessageBoxIcon.None
Which is to be used is specified by using the System Constant in the Show() Method of MessageBox (4th parameter).
We discuss this, shortly
8/14/2019 Lecture 3 - Displaying Dialog Boxes (VB 2008)
5/14
MessageBox Buttons
Several button combinationscan be displayed Using the MessageBoxButtons Enumeration ()
AbortRetryIgnore i.e., MessageBoxButtons.AbortRetryIgnore in the 3rd parameter of Show() Displays Abort, Retry, and Ignore buttons.
OK Displays an OK button
OKCancel Displays OK and Cancel buttons
RetryCancel Displays OK and Cancel buttons
YesNo Displays Yes and No buttons
YesNoCancel Displays Yes, No, and Cancel buttons
Which is displayed is specified in MessageBox.Show() Note that operation is Modal:
Users must click one, for the main program to continue
Information about which is clicked will be returned to the call point
8/14/2019 Lecture 3 - Displaying Dialog Boxes (VB 2008)
6/14
Setting the Default Button
A Default button may be selected.
Using the MessageBoxDefaultButton Enumeration. This is the button activated when the user hits
The Default button is specified using the following options: Button1 (the Default value)
The 1st button from the left in the MessageBox is the Default. i.e., MessageBoxDefaultButton.Button1
Button2 The 2nd button in the MessageBox is the Default.
Button3 The 3rd button in the MessageBox is the Default.
If fewer buttons are on the MessageBox than the option indicates The Default button is selected by cycling clockwise. So forMessageBoxButtons.YesNo, the 3rd button is Yes.
This is specified in the 5th parameter of MessageBox.Show()
8/14/2019 Lecture 3 - Displaying Dialog Boxes (VB 2008)
7/14
The Show Method
A MessageBox is displayed using the Show() Method.
The full Syntax of the Show() method is:
MessageBox.Show( text, caption, Buttons, Icon, DefaultButton )
Any/All parameters may be omitted or used as desiredSo MessageBox.Show( text ) MessageBox.Show( text, caption )are also OK, for instance
Among these parameters: Text is a String orString Variable.
Used to display text in the main area.
Caption is also a String orString Variable. Used to display text in the title bar.
Buttons represents the MessageBoxButtons enumeration.
Icon represents the MessageBoxIcons enumeration.
DefaultButton represents the MessageBoxDefaultButton enumeration.
8/14/2019 Lecture 3 - Displaying Dialog Boxes (VB 2008)
8/14
The DialogResult Enumeration When a MessageBox Button is clicked by a user...
Information about the Event is returned to the point of call.
i.e., the point in the code where Show() was called. What is returned is a defined value of the DialogResult enumeration.
Which indicates which button was actually chosen. Note: what is done with that information is up to the programmer!
DialogResult enumeration members are quite intuitive:
Each type ofButton has a corresponding DialogResult value: Abort Cancel Ignore No None
OK Retry Yes
Each value is represented in the standard enumeration style e.g.: RetryDialogResult.Retry
Such a returned value can be used in a conditional
expression.
8/14/2019 Lecture 3 - Displaying Dialog Boxes (VB 2008)
9/14
MessageBox Example
8/14/2019 Lecture 3 - Displaying Dialog Boxes (VB 2008)
10/14
MessageBox Example (Cont.)
8/14/2019 Lecture 3 - Displaying Dialog Boxes (VB 2008)
11/14
The FontDialog
The FontDialog Class supports UI-based font selection To help users choose which font to use in displaying their data (text).
Available fonts: those installed on the users system.
The FontDialogcan be used as either a:1. VB.NET Class, by
Declaring an Instancein the code;
Setting its propertiesin code.
2. VB .NET Control, by Dragging the Control
from the ToolBox. Setting its properties in code,
or in the Properties Window.
Either way, it will have the same methods, properties, and events And will be displayed, as shown above By invoking the OpenFileDialogs ShowDialog() Method.
8/14/2019 Lecture 3 - Displaying Dialog Boxes (VB 2008)
12/14
FontDialog Properties and Methods
FontDialog has the following Properties : Name: name of the Control instance ( as usual ).
Color:Indicates the color selected in the Dialog.
Font: Indicates the font selected in the Dialog.
MaxSize / MinSize: Indicates the maximum / minimum selectable font size ( in points ).
ShowApply: Indicates whether the Dialog contains an Apply button.
ShowColor: Indicates whether the Dialog displays the Colorchoice.
ShowEffects: Indicates whether the Dialog contains options for special effects ( underline, text color, etc ).
ShowHelp: Indicates whether the Dialog displays a Help button.
Note that user choices do not take effect automatically We must ask the FontDialog what the user as chosen, and code the effect!
We will use only one Method ofFontDialog: ShowDialog(), which displays the Dialog, as usual.
8/14/2019 Lecture 3 - Displaying Dialog Boxes (VB 2008)
13/14
FontDialog: Example
Lets open and use the TextEditorfrom our previous lecture
8/14/2019 Lecture 3 - Displaying Dialog Boxes (VB 2008)
14/14
FontDialog: Example (cont)
To demonstrate proper functionality, here we change:
The text Font to Comic Sans MS Bold12pt
The text Color to light purple.