0% found this document useful (0 votes)
10 views16 pages

W08 Dialogs

Uploaded by

muproductions002
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views16 pages

W08 Dialogs

Uploaded by

muproductions002
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

Fall 2014

Visual
Instructor: Saima Jawad
Programming

Dialogs
MODAL AND MODELESS DIALOGS
COMMON DIALOGS
DIALOG CONFIGURATION
CUSTOMIZED DIALOGS

Week-08 Outline

Visual Programming - Fall 2014


Instructor: Saima jawad 1
Dialog Box
3

A dialog box is a form defined with particular


properties.

• A container, used to host child controls

Dialog Box
4

 Facilitates ‘communication’ between the


user and the application.
 Retrieves “data” that users enter for use in the
application.
 Enables User interaction

Visual Programming - Fall 2014


Instructor: Saima jawad 2
Modal and Modeless Dialogs
5

 There are two types of dialogs:

 A Modal dialog box is one that the user must


first close in order to have access to any other
window of the same application.

Modal Dialog Example


6

Visual Programming - Fall 2014


Instructor: Saima jawad 3
Modeless Dialog Box
7

 A dialog box is Modeless if the user does not


have to close it in order to continue using the
application that owns the dialog box.

 Characteristics of a modeless dialog box:

 It can be neither minimized nor maximized.


 It must provide a way for the user to close it.

Modeless Dialog Box Example


8

Visual Programming - Fall 2014


Instructor: Saima jawad 4
Common Dialogs
9

Built-in dialogs “common” in applications.

 ColorDialog
 FileDialog
 FontDialog
 PageSetupDialog
 PrintDialog

using System.Windows.Forms.CommonDialog;

Visual Programming - Fall 2014


Instructor: Saima jawad 5
Color Dialog
11

File Dialog
12

Visual Programming - Fall 2014


Instructor: Saima jawad 6
Font Dialog
13

Folder Browser Dialog


14

Visual Programming - Fall 2014


Instructor: Saima jawad 7
Print Dialog
15

Color Dialog Example


16

Visual Programming - Fall 2014


Instructor: Saima jawad 8
Color Dialog Example
17

private void myButton_Click (object sender, EventArgs e)


{
ColorDialog myDialog = new ColorDialog();
// Keeps the user from selecting a custom color
myDialog.AllowFullOpen = false ;
// Allows the user to get help. (The default is false)
myDialog.ShowHelp = true ;
// Sets the initial color select to the current text color
myDialog.Color = myLabel.ForeColor ;
if (myDialog.ShowDialog() == DialogResult.OK)
myLabel.ForeColor = myDialog.Color;
}

OpenFileDialog Example
18

Visual Programming - Fall 2014


Instructor: Saima jawad 9
OpenFileDialog Example
19

private void openButton_Click (object sender, EventArgs e)


{
openFileDialog.DefaultExt = "rtf";
DialogResult result = openFileDialog.ShowDialog();
if (result == DialogResult.OK)
{
string filename = openFileDialog.FileName;
richTextBox.LoadFile(filename);
}
}

Dialog Box Configuration


20

 To convert a form into a dialog box, change its


Properties as follows:

FormBorderStyle: FixedDialog
StartPosition: CenterParent
MinimizeBox: False
MaximizeBox: False
ShowInTaskbar: False

Visual Programming - Fall 2014


Instructor: Saima jawad 10
Closing a Dialog Box
21

 A dialog box should have at least one


button labeled OK.

 This button allows the user to acknowledge


the message of the dialog box and close it
by clicking the button.

Accepting an Action
22

 If the user press Enter, the dialog box should


also be closed as if the OK button was clicked.

 To fulfill this functionality of the OK button,


open the AcceptButton combo box in the
Properties window for the form and select the
name of the button.

Visual Programming - Fall 2014


Instructor: Saima jawad 11
Cancelling an Action
23

 The Cancel button allows the user to dismiss whatever


changes have been made on the controls of the dialog
box.

 The dialog box should also be configured so that if the


user presses Esc, the dialog box would be closed as if the
user had clicked Cancel.

 To fulfill this functionality of the Cancel button, open the


CancelButton combo box in the Properties window for
the form and select the name of the button.

Designing a Customized Dialog


24

 A common task in Windows Applications is


displaying a dialog box to collect information
from the user.

 Suppose we want to get user’s information


through a dialog and display this information
in the main form.

Visual Programming - Fall 2014


Instructor: Saima jawad 12
Customized Dialog Application
25

Creating the Main Form


26

1. Create a new Windows Form Application

2. Rename the form as “mainForm”

3. From the Toolbox:

• Drag a ListBox control to the form


• Add a Button control to the form.

Visual Programming - Fall 2014


Instructor: Saima jawad 13
Creating a Dialog Box
27

1. In the Project menu, click Add Windows Form, change the


name to “dataDialog”, and then click Add.

2. From the Toolbox, drag a Label control to the form, and


change the Text property to ‘Enter the String (one per
line)’.

3. Add a TextBox control to the form, and change the


following properties:
Property Value
Multiline True
Scrollbars Both
Size 255, 160

Add OK Button
28

1. Add a Button control to the form, and change the following


properties in the Properties window:

Property Value

Name okButton

Text OK

Visual Programming - Fall 2014


Instructor: Saima jawad 14
Connecting mainForm and Dialog
29

Passing mainForm to the constructor of dataDialog

Add the following code below the default constructor. This


code creates an overloaded constructor that requires
mainForm as a parameter.

mainForm mForm;

public dataDialog(mainForm mF) {


this.mForm = mF;
InitializeComponent();
}

Displaying the Dialog


30

private void addButton_Click(object sender, EventArgs e)


{
dataDialog dlg = new dataDialog(this);
dlg.Show(); //for Modeless Dialog
}

Visual Programming - Fall 2014


Instructor: Saima jawad 15
Retrieving Data from a Dialog Box
31

In the Click event handler of okButton, add the following


code.

if (textBox.Text != string.Empty)
{
mForm.listBox.Items.Clear();
string[] stringsEntered = textBox.Lines;
for (int count = 0; count < stringsEntered.Length; count++)
mForm.listBox.Items.Add(stringsEntered[count]);
}
this.Close();

Visual Programming - Fall 2014


Instructor: Saima jawad 16

You might also like