Home

Introduction

 

Fundamentals

 

Introduction

 

 

 
   
 
 

 

 
 
   
 

Previous Copyright © 2015 FunctionX Next

Home

Dialog Boxes

   

Introduction to Dialog Boxes

 

Description

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:

The Run dialog box

A dialog box has the following characteristics:

Dialog Box Creation

To create a dialog box, you start with a form. Here is an example:

open System
open System.Drawing
open System.Windows.Forms

let exercise : Form = new Form()

exercise.Text <- "Domain Configuration"
exercise.Size <- new Size(320, 150)
exercise.Location <- new Point(140, 100)
exercise.StartPosition <- FormStartPosition.CenterScreen

do Application.Run exercise

This would produce:

Starting a dialog box

Characteristics of Dialog Boxes

 

The Border Style of a Dialog Box

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 form's FormBorderStyle property to FixedDialog. Setting this property changes the borders of the form to the standard borders of a dialog box (the border of a dialog box is thinner than that of a regular form). Here is an example:

open System
open System.Drawing
open System.Windows.Forms

let exercise : Form = new Form()

exercise.Text <- "Domain Configuration"
exercise.Size <- new Size(320, 150)
exercise.Location <- new Point(140, 100)
exercise.StartPosition <- FormStartPosition.CenterScreen

exercise.FormBorderStyle <- FormBorderStyle.FixedDialog

do Application.Run exercise

This would produce:

Starting a dialog box

The Minimize and Maximize Boxes

Besides taking care of the borders, you should also set both the MinimizeBox and the MaximizeBox properties to false. This causes the window to display only the system Close button. Here is an example:

open System
open System.Drawing
open System.Windows.Forms

let exercise : Form = new Form()

exercise.Text <- "Domain Configuration"
exercise.Size <- new Size(320, 150)
exercise.Location <- new Point(140, 100)
exercise.StartPosition <- FormStartPosition.CenterScreen

exercise.FormBorderStyle <- FormBorderStyle.FixedDialog
exercise.MinimizeBox <- false
exercise.MaximizeBox <- false

do Application.Run exercise

This would produce:

Form

Closing a Dialog Box

You should provide a way for the user to close the dialog box. 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. If the user presses Enter, the dialog box should also be closed as if the OK button was clicked.

Accepting an Action

Often the user will be presented with various options on a dialog box and may be asked to make a decision on the available controls. Most of the time, if you are creating such a dialog box, besides the OK button, it should also have a Cancel button. The OK button should be the default so that if the user presses Enter, the dialog box would be closed as if the user had clicked OK. Clicking OK or pressing Enter would indicate that, if the user had made changes on the controls of the dialog box, those changes would be acknowledged and kept when the dialog box is closed. Usually, the changed values of the control would be transferred to another dialog box or form. Keep in mind that you are responsible for implementing this functionality.

To fulfill this functionality of the OK button, set the AcceptButton property to the name of the button.

Cancelling an Action

The Cancel button is used to allow the user to dismiss whatever changes would 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, after adding it to a dialog box (or form), set the CancelButton property to the name of the button.

Besides the OK and the Cancel buttons, a dialog box can be created with additional buttons such as Finish or Help, etc. It depends on its role and the decision is made by the application developer.

The Help Button

Besides the system Close button, if you are planning to provide help on a dialog box, you can equip it with a Help button. To support this, the Form class is equipped with a Boolean property named HelpButton. The default value of this property is false. Here is an example:

open System
open System.Drawing
open System.Windows.Forms

let exercise : Form = new Form()

exercise.Text <- "Domain Configuration"
exercise.Size <- new Size(320, 150)
exercise.Location <- new Point(140, 100)
exercise.StartPosition <- FormStartPosition.CenterScreen

exercise.FormBorderStyle <- FormBorderStyle.FixedDialog
exercise.MinimizeBox <- false
exercise.MaximizeBox <- false

exercise.HelpButton <- true

Application.Run Exercise

This would produce:

Form

When the user clicks the help button, the mouse cursor becomes equipped with a question mark. Here is an example:

Form

You can then write code so that, when the user clicks a control on the dialog box, some guiding help is provided as a tool tip.

Modal and Modeless Dialog Boxes

 

Modal Dialog Boxes

There are two types of dialog boxes: modal and modeless.

A modal dialog box is one that the user must first close in order to have access to any other framed window or dialog box of the same application. One of the scenarios in which you use a dialog box is to create an application that is centered around one. In this case, if either there is no other form or dialog box in your application or all the other forms or dialog boxes depend on this central dialog box, it must be created as modal. Such an application is referred to as dialog-based.

Some applications require various dialog boxes to complete their functionality. When in case, you may need to call one dialog box from another and display it as modal. Here is an example:

Modal

After creating a dialog box used as an addition to an existing form or an existing dialog box, to call it as modal, use the ShowDialog() method. Such a dialog box typically has an OK and a Cancel button. The Button class is equipped with a property named DialogResult:

abstract DialogResult : DialogResult with get, set 
override DialogResult : DialogResult with get, set

Here is how the dialog box and its buttons work:

Here is an example:

open System
open System.Drawing
open System.Windows.Forms 

let loginDialogBox = new Form(MinimizeBox = false,
                              MaximizeBox = false,
                              Text = "Conditional Statements",
                              FormBorderStyle = FormBorderStyle.FixedDialog,
                              ClientSize = new System.Drawing.Size(210, 100),
                              StartPosition = FormStartPosition.CenterScreen)

let lblUsername : Label = new Label(AutoSize = true, 
                                    Text = "Username:",
                                    Location = new Point(21, 24))
loginDialogBox.Controls.Add lblUsername

let txtUsername : TextBox = new TextBox(Location = new Point(85, 21),
                                        Size = new System.Drawing.Size(100, 20))
loginDialogBox.Controls.Add txtUsername

let btnOK : Button = new Button(Text = "OK", Location = new Point(24, 60))
loginDialogBox.Controls.Add btnOK

let btnCancel : Button = new Button()
btnCancel.Location <- new Point(110, 60)
btnCancel.Text <- "Cancel"
loginDialogBox.Controls.Add btnCancel

loginDialogBox.AcceptButton <- btnCancel
loginDialogBox.AcceptButton <- btnOK

[<EntryPoint>]
let main argv = 
    Application.Run loginDialogBox
    0

To close the dialog box, the user can click one of the buttons (OK or Cancel). Meanwhile, the signature of the form's ShowDialog() method is:

member ShowDialog : unit -> DialogResult

As a result, when you call this method, that is, when the user closes the dialog box, you can find out what button the user clicked by enquiring about the returned value of the method. The method returns DialogResult.OK if the user clicks OK and it returns DialogResult.Cancel if the user clicks Cancel.

Modeless Dialog Boxes

A dialog box is referred to as modeless if the user does not have to close it in order to continue using the application that owns the dialog box. A modeless dialog box has the following characteristics

Here is an example:

Modeless

Since the modeless dialog box does not display its button on the task bar, the user should know that the dialog box is opened. To make the presence of a modeless dialog box obvious to the user, it typically displays on top of its host application until the user closes it.

A modeless dialog box is created from a form but it should look like a regular dialog box or a tool window. Therefore, to create a modeless dialog box, set the FormBorderStyle property to an appropriate value such as FixedSingle, FixedToolWindow, Sizable or SizableToolWindow. Also, set its ShowInTaskbar property to False.

After creating the dialog box, to display it as modeless, call the Show() method. The fundamental difference between the ShowDialog() and the Show() methods is that the former displays a modal dialog box, which makes sure that the called dialog box cannot go in the background of the main application. By contrast, the Show() method only calls the dialog box every time it is requested. For this reason, it is up to you to make sure that the modeless dialog box always remains on top of the application. This is easily taken care of by setting the Boolean TopMost property of the form to True.

There are two main ways a normal modeless dialog box can be dismissed:


Home Copyright © 2015 FunctionX Home