Professional Documents
Culture Documents
Working With Dialogs
Working With Dialogs
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 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.
AbortRetryIgnore The message box contains Abort, Retry, and Ignore buttons
YesNoCancel The message box contains Yes, No, and Cancel buttons.
CancelTryContinue Specifies that the message box contains Cancel, Try Again, and Continue buttons.
5
Standard Dialog Boxes and Custom Dialog Boxes
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).
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.
9
Open File Dialog Box
Open File Dialog Box
OpenFileDialog allows you to choose a file to be opened in an Application.
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();
}
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