You are on page 1of 24

Dialog, Message Boxes & Layout Management

Week 9

Instructor: Jawad Shaukat


Outline
1. Dialog Boxes
1. Custom Dialog Box
2. Common Dialog Box

2. Message Boxes
3. Layout Management
1. Anchor
2. Dock
3. Tab Order
Dialog Box
 A dialog box is a form defined with particular properties. Like a form, a dialog box
is referred to as a container. Like a form, a dialog box is mostly used to host child
controls, insuring the role of dialog between the user and the machine. Here is an
example of a dialog box:

 A dialog box has the following characteristics:


 The only system button it is equipped with is Close. As the only system button, this button
allows the user to dismiss the dialog and ignore whatever the user would have done on
the dialog box
 It cannot be minimized, maximized, or restored. A dialog box does not have any other
system button but Close
Dialog Box

 A dialog box is most often used to provide the user with the means for
specifying how to implement a command or to respond to a question.
Windows.Form is a base class for a dialog box.
 Sometimes, in a graphical user interface, a window is used to communicate
with the user or establish a dialog between the user and the application. This
additional window is called a dialog box. It may communicate information to
the user; prompt the user for a response or both.
 Dialog boxes are special forms that are non-resizable. They are also used to
display the messages to the user. The messages can be error messages,
confirmation of the password, confirmation for the deletion of a particular
record, Find-Replace utility of the word etc. There are standard dialog boxes
to open and save a file, select a folder, print the documents, set the font or
color for the text, etc.
Dialog Box - Types

 Dialog boxes are of two types, which are given below.


 Modal dialog box
 A dialog box that temporarily halts the application and the user cannot continue until
the dialog has been closed is called modal dialog box. The application may require
some additional information before it can continue or may simply wish to confirm that
the user wants to proceed with a potentially dangerous course of action. The
application continues to execute only after the dialog box is closed; until then the
application halts.
 Modeless dialog box
 It is used when the requested information is not essential to continue, so the Window
can be left open, while work continues somewhere else. For example, when working in
a text editor, the user wants to find and replace a particular word. This can be done,
using a dialog box, which asks for the word to be found and replaced. The user can
continue to work, even if this box is open.
 Model dialog is displayed, using ShowDialog() method. Modeless dialog boxes are
displayed, using Show() method.
Common Dialog Boxes

 The dialog boxes that are used, which are common to all Windows
Application. It performs common tasks like saving a file, choosing a font etc.
This provides a standard way to the Application interface.
 The examples are given below.
 FontDialog
 ColorDialog
 OpenDialog
 SaveDialog
 These dialog boxes are implemented by an operating system, so they can be
shared across all the Application that runs on that operating system
(Windows).
Custom Dialog Box

 Even though common dialog boxes are useful, they do not support the
requirements of domain-specific dialog boxes. Developer need to create their
own dialog boxes.
 Following steps represent the process of creating Custom Dialog Box
 Add a form to your project by right clicking the project in Solution Explorer, point
to Add and then click Windows Form.
 In the properties Window, change the FormBorderStyle property to FixedDialog.
 Customize the appearance of the form, as required.
 Add controls into this form.
Custom Dialog Box - Creation
 Add another form in your project.
 There are a few actions you should perform on a form to transform it into a
dialog box; but normally, these are only suggestions, not rules. Based on the
Microsoft Windows design and standards, to create a dialog box, you should
set a FormBorderStyle property to FixedDialog.
 Set both the MinimizeBox and the MaximizeBox properties to False. This
causes the window to display only the system Close button.
private void InitializeComponent()
{
Text = "Domain Configuration";
Width = 320;
Height = 150;
Location = new Point(140, 100);
StartPosition = FormStartPosition.CenterScreen;
FormBorderStyle = FormBorderStyle.FixedDialog;
MinimizeBox = false;
MaximizeBox = false; 
}
Custom Dialog Box

 On your button of Custom Dialog box set the DialogResult property to


DialogResult.OK or any other as you like.

Main Form Button of DialogBox

Password_Dialog_Box p1 = new private void button1_Click(object


Password_Dialog_Box(); sender, EventArgs e)
{
if (p1.ShowDialog() == this.Close();
DialogResult.OK) }
{
textBox1.Text = "Your
Password is reset.";
}
Message Box
 A message box is a special dialog box used to display a piece of information to the
user. As opposed to a regular form, the user cannot type anything in the message
box. The .NET Framework inherently supports message boxes through its
own MessageBox class.
 A message box is may be meant to let the user make a decision by clicking a button
and, depending on the button the user would have clicked, the message box would
return a value. The value returned by a message box corresponds to the particular
button the user would have clicked (on the message box). The return values are
defined in the DialogResult enumeration.
Message Box

 The .NET Framework provides the MessageBox class function used to easily


create a message box. To display a simple message with just an OK button,
you can call the Show() static method of this class. 
 MessageBox.Show("Welcome to the Wonderful World of Visual C#");

 Caption of MessageBox
 MessageBox.Show("Welcome to the Wonderful World of Visual C#","Visual C#
Tutorials");
Message Box
 Message Box Button

MessageBox.Show("Welcome to the Wonderful World of Visual C#","Visual C#


Tutorials", MessageBoxButtons.OKCancel);
Message Box
 The possible icons are available through the MessageBoxIcon enumeration.
 MessageBox.Show("Your order appears to be correct" +"\nAre you ready to provide
your credit card information?", "Customer Order Processing",
MessageBoxButtons.YesNoCancel, MessageBoxIcon.Information);
Message Box

 Default Button:
 When a message box is configured to display more than one button, the operating system
is set to decide which button is the default. The default button has a thick border that
sets it apart from the other button(s). If the user presses Enter, the message box would
behave as if the user had clicked the default button. If the message box has more than
one button, you can decide what button would be the default. To specify the default
button
 MessageBox.Show("Your order appears to be correct" +"\nAre you ready to provide your
credit card information?", "Customer Order Processing",
MessageBoxButtons.YesNoCancel, MessageBoxIcon.Information,
MessageBoxDefaultButton.Button1);
Message Box - Example

 private void button1_Click(object sender, EventArgs e)


 {
 DialogResult dr;
 dr = MessageBox.Show("Welcome to C# Corner", "Learn C#",
MessageBoxButtons.YesNo, MessageBoxIcon.Information);
 if (dr == DialogResult.Yes)
 {
 Close();
 }
 }
Common Dialog Box – Color Dialog Box
 To provide the selection of colors on Microsoft Windows applications, the
operating system provides a common dialog box appropriate for such tasks.
You can use the Color dialog box for various reasons such as letting the user
set or change a color of an object or specifying the background color of a
control or the color used to paint an object. When it displays, by default, the
dialog box appears.

 Use Following Code to display and select Color from Dialog Box
ColorDialog cd = new ColorDialog();
DialogResult cr=cd.ShowDialog();
if (cr == DialogResult.OK)
{
this.BackColor=cd.Color;
}
Font Dialog Box
 To assist the user with selecting a font for an application, Microsoft Windows
provides the Font dialog box

 Use the following Code to use Font Dialog Box

FontDialog fd = new FontDialog();


DialogResult dr=fd.ShowDialog();
if (dr == DialogResult.OK)
{
label1.Font = fd.Font;
}
Open File Dialog Box

 C# OpenFileDialog control allows us to browse and select files on a computer


in an application. A typical Open File Dialog looks like Figure 1 where you can
see Windows Explorer like features to navigate through folders and select a
file.

 Creating Open File Dialog Box


 OpenFileDialog ofd = new OpenFileDialog();
Open File Dialog Box - Properties
Title
 Title property is used to set or get the title of the open file dialog.
 openFileDialog1.Title = "Browse Text Files";  
Default Extension
 DefaultExtn property represents the default file name extension.
 openFileDialog1.DefaultExt = "txt";  
Filter and Filter Index
 Filter property represents the filter on an open file dialog that is used to
filter the type of files to be loaded during the browse option in an open file
dialog. For example, if you need users to restrict to image files only, we can
set Filter property to load image files only.
 openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"; 
File Name and File Names
 FileName property represents the file name selected in the open file dialog.
 textBox1.Text = openFileDialog1.FileName;
Open File Dialog Box - Example
 label1.Text = "";
 OpenFileDialog ofd = new OpenFileDialog();
 ofd.Filter = "TXT files|*.txt";
 DialogResult dr= ofd.ShowDialog();
 if (dr == DialogResult.OK)
 {
 StreamReader mystream = new StreamReader(ofd.FileName);
 label1.Text = mystream.ReadToEnd();
 }

 \\Add System.Io library for using StreamReader.


Layout Management

 Control Layout Properties


Control.Anchor Property

 Gets or sets the edges of the container to which a control is bound


and determines how a control is resized with its parent
 A bitwise combination of the AnchorStyles values. The default is top
and left.
 The Effects of Anchoring

 Programmatically
 button1.Anchor = (AnchorStyles.Bottom | AnchorStyles.Right);
Control.Dock Property

 Gets or sets which control borders are docked to its parent control and
determines how a control is resized with its parent.

 Programmatically
 Button1.Dock = DockStyle.Top;
Tab Order

 Set the tab order for the entire form by choosing Tab
Order in the menu.
 Click the controls in the order you wish them to
receive focus
 Select Control -> Properties -> Tab Index
 As you press Tab Key while running application, focus
will switch to controller according to tab index.

You might also like