Working With Dialogs

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 23

Working with DIALOGS

Dialog Box
A dialog box in C# is a type of window, which is used to enable common
communication or dialog between a computer and its user. 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.
2
Two types of Dialog Boxes

Modal Dialog Box Modeless Dialog Box


is the dialog box that temporarily halts the when the requested information is not essential to
application and the user cannot continue until the continue, so the Window can be left open, while
dialog has been closed. work continues somewhere else.

Modal dialog box is displayed, using ShowDialog() method. Modeless dialog boxes are
displayed, using Show() method.

3
MessageBox and Show Method

The MessageBox class is used to display messages to the user. The show() method is used to display a message box
with the specified text, caption, buttons, and icon. There are other overloads also available.
Example:

4
MessageBoxButtons
Specifies constants defining which buttons to display on a MessageBox.

OK The message box contains an OK button.

OKCancel The message box contains OK and Cancel buttons.

AbortRetryIgnore The message box contains Abort, Retry, and Ignore buttons

YesNoCancel The message box contains Yes, No, and Cancel buttons.

YesNo The message box contains Yes and No buttons.

RetryCancel The message box contains Retry and Cancel buttons.

CancelTryContinue Specifies that the message box contains Cancel, Try Again, and Continue buttons.
5
Standard Dialog Boxes and Custom Dialog Boxes

STANDARD DIALOG BOXES CUSTOM DIALOG BOX


Some dialog boxes are standard like the A dialog box can also be customized.
warning message or error message. Save the file Such a dialog box is called a Custom Dialog Box.
and enter the password. These are called standard Dialog boxes are special forms that are non-
dialog boxes. There are standard dialog boxes to resizable. They are also used to display the
open and save a file, select a folder, print the messages to the user. The messages can be error
documents, set the font or color for the text, etc. messages, confirmation of the password,
confirmation for the deletion of a particular record

6
Custom Dialog Boxes
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.

Take note, use ShowDialog() method to open the form as modal dialog box. This method can be used for the custom
dialog boxes too. Show() method is used to open the model's dialog box.

7
Common Dialog Properties
There are different dialog boxes which are used though mostly 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 FontDialog, ColorDialog, OpenDialog, and
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).

Steps to use common dialog boxes


■ Instantiate the required common dialog box.
■ Set the properties of common dialog box, if required.
■ Call its ShowDialog() method to invoke the dialog box.

ShowDialog() returns an enumerated type called DialogResult. It defines the identifiers, which
indicates which button was clicked. For example, DialogResult.OK and DialogResult.Cancel are some
values that indicate OK or Cancel button were clicked respectively.
8
Font and Color Dialog Boxes
Font Dialog Box Color Dialog Box
FontDialogBox is used to allow the user to ColorDialogBox is used to allow the user
select font settings. to select a color.

FontDialog fdlg = new FontDialog(); ColorDialog cdlg = new ColorDialog();


fdlg.ShowDialog(); cdlg.ShowDialog();
txtEx.Font = fdlg.Font; txtEx.ForeColor = cdlg.color;

9
Open File Dialog Box
Open File Dialog Box
OpenFileDialog allows you to choose a file to be opened in an Application.

OpenFileDialog ofd = new OpenFileDialog();


ofd.Title = "Open a Text File";
ofd.Filter = "Text Files (*.txt) | *.txt | All Files(*.*) | *.*";
//Here you can filter which all files you wanted allow to open
DialogResult dr = ofd.ShowDialog();
if (dr == DialogResult.OK) {
StreamReader sr = new StreamReader(ofd.FileName);
txtEx.Text = sr.ReadToEnd();
sr.Close();
}
10
11
Save File Dialog Box
Save File Dialog Box
SaveFileDialog box is used to allow the user to select the destination and name of the file to be
saved.

SaveFileDialog sfdlg = new SaveFileDialog();


sfdlg.Filter = "Text Files (*.txt) | *.txt"; /* Here you can filter
which all files you wanted allow to open */
if (sfdlg.ShowDialog() == DialogResult.OK) {
// Code to write the stream goes here.
}

12
13
Error Provider Control

The ErrorProvider presents a simple mechanism for indicating to the end user that a control on a
form has an error associated with it. If an error description string is specified for the control, an icon
appears next to the control. To use this control, we must add the namespace using
System.Text.RegularExpressions; at the top of the code.

14
15
Parent and Modeless Dialog

A ‘modeless’ dialog is a window that is always on top of its parent window but activity can still
take place within the parent window.
The boolean TopMost property of a form should be set to true simply puts the form on top of
everything, like Task Manager. Simply a matter of setting the Owner property of the form, to the form
that programmer wants to always be on top of it.

16
// Launch Normal Window
private void _btnNormal_Click(object sender, EventArgs e)
Example {
Form form = new Form();
form.Text = "Normal Window";
form.Show();
}

// Launch Modeless Window


private void _btnModeless_Click(object sender, EventArgs e)
{
Form form = new Form();
form.Text = "Modeless Window";
form.Owner = this; // new form to float on top of this one
form.Show();
}

// Launch Modal Window


private void _btnModal_Click(object sender, EventArgs e)
{
Form form = new Form();
form.Text = "Modal Window";
form.ShowDialog();
}
17
Example

The main form has focus and that the normal


window has dropped to the back. However, the
Modeless form is still on top but we can still access
the other forms.

Now the Modal dialog comes straight to the top and


we can't access any of the other forms until this form
is dismissed.

18
Close Buttons
Different exit methods in C# that can be used.

1. this.Close()
When we need to exit or close opened form then we should use "this.Close()" method to
close the form on some button click event.

19
Close Buttons

2. System.Windows.Forms.Application.ExitThread()
When we are running a winform application & need to exit or close SUB
APPLICATION or CURRENT THREAD then we should use
“System.Windows.Forms.Application.ExitThread()”. Before you exit your application, you really
need to have all your other thread exited, unless they are background threads or the threads
obtained via the ThreadPool. This method exits the message loop on the current thread and closes
all windows on the thread.

20
Close Buttons

3. System.Windows.Forms.Application.Exit()
When we are running a winform application & need to exit or close ENTIRE APPLICATION
then we should use “System.Windows.Forms.Application.Exit( )”. This method internally informs
all message loops in application that you must terminate. Then this method wait to close all
application windows till the message loops have been processed. This method does not force the
application to exit whereas this method force RUN method (we are calling run method inside
program file) to return.

21
Close Buttons

4. System.Environment.Exit(a_ExitCode)
When we are running a console application & need to exit or close whole application
then we should use “System.Environment.Exit(a_ExitCode)” where this exit code is an int type
argument, which show the status of process. This method terminates this process and gives the
underlying operating system the specified exit code. We should not use this method in winform
application because winform application has some running message loops.

22
Close Buttons

4. System.Environment.Exit(a_ExitCode)
a_ExitCode: If in your application main method return type is void, then you should use this
property to assign the exit code value. This exit code value will be returned to the calling process.
If your main method returns something, then you should ignore this. The default value of this
property is zero, which shows that the process completed successfully. You can use other number
to indicate an error like 1, 2 , 3, 4, 5, ........ till 499. Also you can create some error codes yourself
& use in your application that will return the suitable error code.

23

You might also like