Home

Introduction

 

Fundamentals

 

Introduction

 

 

     
 
 

 

   
   
 

Previous Copyright © 2015 FunctionX Next

Home

Message Boxes

     

Introduction

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 dialog box. The .NET Framework inherently supports message boxes through its own MessageBox class. Besides this, you can also use functions from either the Visual Basic or the Win32 libraries.

The Return Value of a Message Box

Besides displaying a message, a message box may let the user make a decision by clicking a button. 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. The buttons and the returned values are as follows:

If the User Clicks The method Returns
Abort DialogResult.Abort
Cancel DialogResult.Cancel
Ignore DialogResult.Ignore
No DialogResult.No
OK DialogResult.OK
Retry DialogResult.Retry
Yes DialogResult.Yes
 

The Message of a Message Box

The .NET Framework provides the MessageBox class 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. Its syntax is as follows:

static member Show : 
        text:string -> DialogResult

In this case, the message to display must be passed as a string to the Show() method. 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

let btnOKClick(e : EventArgs) =
    if txtUsername.Text = "402944" then
        MessageBox.Show "Login Successful" |> ignore

    |> ignore

btnOK.Click.Add btnOKClick

LoginDialogBox.AcceptButton <- btnCancel
LoginDialogBox.AcceptButton <- btnOK

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

This would produce:

Nesting a Conditional Statement

The message to display can be made of up to 1024 characters. To display the message on multiple lines, you can use the new line escape sequence anywhere inside the string.

The Caption of a Message Box

In reality, the MessagBox.Show() method is overloaded with various versions. Another version is:

static member Show : 
        text:string * 
        caption:string -> DialogResult

This version allows you to specify a custom caption for the message box. With this version, the first argument is the string that the user will see displaying on the message box. You can pass it as a string. You can also create it from other pieces of strings.

The second argument, caption, will be the sentence to display in the title bar of the message box. Here is an example:

open System
open System.Windows.Forms

MessageBox.Show("Welcome to the Wonderful World of F# Programming", "F# Programming") |> ignore

This would produce:

Message Box

The Buttons of a Message Box

Another version of the MessageBox.Show() method is as follows:

static member Show : 
        text:string * 
        caption:string * 
        buttons:MessageBoxButtons -> DialogResult

This version allows you to display one or more buttons on the message box. The available buttons are defined through the MessageBoxButtons enumeration. Its members are:

MessageBoxButtons  Display
OK OK
OKCancel OK Cancel
YesNo Yes No
YesNoCancel Yes No Cancel
RetryCancel Retry Cancel
AbortRetryIgnore Abort Retry Ignore

To use any of these combinations of buttons, call the MessageBoxButtons enumeration and access the desired combination. Here is an example:

open System.Windows.Forms

MessageBox.Show("Welcome to the Wonderful World of F# Programming",
                "F# Programming",
                MessageBoxButtons.OKCancel) |> ignore

This would produce:

Message Box With Buttons

The Icon of a Message Box

Another version of the message box allows you to display an icon. This is done using the following version of the MessageBox.Show() method:

static member Show : 
        text:string * 
        caption:string * 
        buttons:MessageBoxButtons * 
        icon:MessageBoxIcon -> DialogResult

The last argument represents an icon. The possible options are available through the MessageBoxIcon enumeration. The members of this enumerator and their corresponding icons are:

MessageBoxIcon Description
None    
Asterisk Information
Error Critical
Exclamation Exclamation
Hand Critical
Information  Information
Question Question
Stop Critical
Warning Exclamation

Here is an example:

open System.Windows.Forms

MessageBox.Show("Welcome to the Wonderful World of F# Programming",
                "F# Programming",
                MessageBoxButtons.YesNoCancel,
                MessageBoxIcon.Information) |> ignore

This would produce:

Message Box With an Icon

The Default Button of a Message Box

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, the MessageBox.Show() method provides the following version:

static member Show : 
        text:string * 
        caption:string * 
        buttons:MessageBoxButtons * 
        icon:MessageBoxIcon * 
        defaultButton:MessageBoxDefaultButton -> DialogResult

Based on this, you can specify the default button using the last argument that provides values through the MessageBoxDefaultButton enumerator whose values are:

Button1: The left button will be the default. Here is an example:

open System.Windows.Forms

MessageBox.Show("Welcome to the Wonderful World of F# Programming",
                "F# Programming",
                MessageBoxButtons.YesNoCancel,
                MessageBoxIcon.Information,
                MessageBoxDefaultButton.Button1) |> ignore

Message Box With an Icon

Button2: If the message box displays two buttons, the right button will be the default. If the message box displays three buttons, the middle button will be the default. Here is an example:

open System.Windows.Forms

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.Button2) |> ignore

Message Box With an Icon

Button3: The right button will be the default. Here is an example:

open System.Windows.Forms

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.Button3) |> ignore

Message Box With an Icon

     

Home Copyright © 2015 FunctionX Home