Message Box and Input Box

Download as pdf or txt
Download as pdf or txt
You are on page 1of 20

Message Box and Input Box

MessageBox Class
• .NET has introduced a class called MessageBox which
encapsulates all the features of MsgBox.
• The difference between MsgBox and MessageBox is that
Msgbox is a function while MessageBox is a class.
• The MessageBox class has various overloaded Show
methods for different parameters.
• From a practical standpoint, both the MsgBox function and
the MessageBox class will accomplish the same thing.
• The arguments for MessageBox are specified in a slightly
different order from MsgBox.
• MessageBox.Show Method
• To show the message box we need to call the Show
method of the MessageBox class, for example:
-MessageBox.Show("Hello World!")
Displaying Message Boxes
• Use Message box to
– Display message
– Get response
• Message Box class
– Member of System.Windows.Forms namespace
– Single method named Show
• Creates instance of MessageBox
• Makes it visible
Displaying Message Boxes (Contd..)
• Show() method arguments
– Message to display
• Can string literal or variable

– Caption to display

– Buttons to display

– Type of icon to show


Example
1. ' display a message
2. MessageBox.Show(“Hello Again”)
3. 'display a message and a caption
4. MessageBox.Show(“Hello Again”, “MessageBox
Demo”)
5. 'display message, caption, Yes/No/Cancel buttons
6. MessageBox.Show(“Hello Again”, “MessageBox
Demo”, MessageBoxButtons.YesNoCancel)
7. 'display message, caption, Yes/No/Cancel buttons,
and Icon
8. MessageBox.Show(“Hello Again”, “MessageBox
Demo”, MessageBoxButtons.YesNoCancel,
MessageBoxIcon.Information)

Message Box with Message and Caption


• Button combinations
– Automatically displayed by IntelliSense
• After you type class name MessageBoxButtons
– Select specific buttons to be displayed from list
• Icons
– Displayed by IntelliSense when typing
MessageBoxIcon
Button Combination
Message Box icon Choices
Show()
• show() method return value

– Data type DialogResult

– Compare to specific values using If statement


Dialog Result Values
• The Show method has various overloaded
forms.
• syntax
– [DialogResult = ] MessageBox.Show([window ,]
- prompt
– [, caption]
– [, MessageBoxButtons]
– [, MessageBoxIcon]
– [, MessageBoxDefaultButton]
Syntax

• MessageBox.Show("Cancel Button Pressed",


"MessageBox Title",MessageBoxButtons.OK ,
MessageBoxIcon.Exclamation)
• Window : The window that the message
box will display in front of (for example,
you could specify "Me" to refer to the
current form). This argument is typically
omitted.
• Prompt : The text to display in the
message box. This is the only required
argument
• Caption : The text to display in the title bar
of the message box. If omitted, the project
name will be displayed.
Message Box Icon Specifies which icon to display on
the message box. Possible values
are:
Value Icon
MessageBoxIcon.Error
- or -
MessageBoxIcon.Hand
- or -
MessageBoxIcon.Stop

MessageBoxIcon.Question

MessageBoxIcon.Exclamatio
n
- or -
MessageBoxIcon.Warning

MessageBoxIcon.Asterisk
- or -
MessageBoxIcon.Informatio
n
(none)
MessageBoxIcon.None
Value Description
DialogResult.OK The OK button was pressed
DialogResult.Cancel The Cancel button was pressed
DialogResult.Abort The Abort button was pressed
DialogResult.Retry The Retry button was pressed
DialogResult.Ignore The Ignore button was pressed
DialogResult.Yes The Yes button was pressed
DialogResult.No The No button was pressed
DialogResult.None Nothing is returned from the dialog
box. This means that the modal dialog
continues running.
Input Box
• Usually, a text box is used to obtain input
described by a label. Sometimes we want just
one piece of input and would rather not have
a text box and label stay on the screen forever.
The problem can be solved with an input box,
which is an input form automatically created
by VB for you.
InputBox( ) function
• An InputBox( ) function will display a message box where the user
can enter a value or a message in the form of text.
• Syntax
– myMessage=InputBox(Prompt, Title, default_text, x-position, y-
position)
• myMessage is a string type but which accept the message input by
the users. The arguments are explained as follows:
• Prompt - The message displayed normally as a question asked.
• Title - The title of the Input Box.
• default-text - The default text that appears in the input field where
users can use it as his intended input or he may change to the
message he wish to enter.
• x-position and y-position - the position or the coordinates of the
input box.
Input Box Example
• Private Sub Button1_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs)
Handles Button7.Click
• Dim r As String
• r = InputBox("hi", "demo", "fun", 50, 70)
• End Sub

You might also like