The msg box function and the messagebox class


Published on

Published in: Technology, Art & Photos
1 Like
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

The msg box function and the messagebox class

  1. 1. The MsgBox Function and the MessageBox Class• MsgBox function, is a part of the Microsoft.VisualBasic namespace .• MessageBox is the class. Part of System.Windows.MessageBox
  2. 2. The MsgBox Function• The MsgBox function displays a message in a dialog box, waits for the user to click a button, and returns an Integer indicating which button the user clicked.• Syntax: – MsgBox(prompt[, buttons] [, title]) – - or – – IntegerVariable = MsgBox(prompt[, buttons] [, title])• The MsgBox function syntax has these parts:• Part• prompt – Required. String expression displayed as the message in the dialog box. The maximum length of prompt is approximately 1024 characters, depending on the width of the characters used.
  3. 3. • buttons – Optional. Numeric expression that is the sum of values specifying the number and type of buttons to display, the icon style to use, the identity of the default button, and the modality of the message box. If omitted, the default value for buttons is 0 (which causes only an OK button to be displayed with no icon). The buttons argument is explained in more detail below.• title – Optional. String expression displayed in the title bar of the dialog box. If you omit title, the application name is placed in the title bar.
  4. 4. The buttons argument• The buttons argument is formed by five groups of values.• The first group of values (0–5) describes the number and type of buttons displayed in the dialog box;• The second group (16, 32, 48, 64) describes the icon style;• The third group (0, 256, 512, 768) determines which button is the default;• The fourth group (0, 4096) determines the modality of the message box;• And the fifth group contains values that would only be used under special circumstances.• When adding numbers to create a final value for the buttons argument, use only one number from each group.• For each value, a corresponding built-in constant i.e. either the classic "vb" constants or the .NET "MsgBoxStyle" enumeration constants may also be used.• Use of the constants is preferred for readability.• The "vb" constants were introduced in earlier versions of Visual Basic and may also be used in VB.NET.• VB.NET also provides the "MsgBoxStyle" enumeration constants which can be used as an alternative to the "vb" constants.
  5. 5. First Group: Determines which buttons to display:Constant Value DescriptionvbOKOnly 0 Display OK button only.- or -MsgBoxStyle.OKOnlyvbOKCancel 1 Display OK and Cancel- or - buttons.MsgBoxStyle.OKCancelvbAbortRetryIgnore 2 Display Abort, Retry,- or - and Ignore buttons.MsgBoxStyle.AbortRetryIgnorevbYesNoCancel 3 Display Yes, No, and- or - Cancel buttons.MsgBoxStyle.YesNoCancelvbYesNo 4 Display Yes and No- or - buttons.MsgBoxStyle.YesNovbRetryCancel 5 Display Retry and- or - Cancel buttons.MsgBoxStyle.RetryCancel
  6. 6. Second Group:Determines which icon to display:Constant Value Description IconvbCritical 16 Display Critical- or - Message icon.MsgBoxStyle.CriticalvbQuestion 32 Display- or - Warning QueryMsgBoxStyle.Qu (question mark)estion icon.vbExclamation 48 Display- or - WarningMsgBoxStyle.Ex Message icon.clamationvbInformation 64 Display- or - InformationMsgBoxStyle.Inf Message icon.ormation
  7. 7. Third Group:Determines which button is the default:Constant Value DescriptionvbDefaultButton1 0 First button is default.- or -MsgBoxStyle.DefaultButton1vbDefaultButton2 256 Second button is default.- or -MsgBoxStyle.DefaultButton2vbDefaultButton3 512 Third button is default.- or -MsgBoxStyle.DefaultButton3vbDefaultButton4 768 Fourth button is default- or - (applicable only if a HelpMsgBoxStyle.DefaultButt button has been added).on4
  8. 8. Fourth Group:Determines the modality of the message box.Generally, you would not need to use a constant from this group, as you would wantto use the default (application modal). If you specified "system modal", you would be"hogging" Windows – i.e., if a user had open another app , like Word or Excel, theywould not be able to get back to it until they responded to your apps message box.Constant Value DescriptionvbApplicationModal 0 Application modal; the- or - user must respond toMsgBoxStyle.Applicati the message boxonModal before continuing work in the current application.vbSystemModal 4096 System modal; all- or - applications areMsgBoxStyle.System suspended until theModal user responds to the message box.
  9. 9. The fifth group of constants that can be used for the buttons argument would only be used under special circumstances:Constant Value DescriptionvbMsgBoxHelpButton 16384 Adds Help button to the- or - message boxMsgBoxStyle.MsgBoxHelpButtonvbMsgBoxSetForeground 65536 Specifies the message- or - box window as theMsgBoxStyle.MsgBoxSet foreground windowForegroundvbMsgBoxRight 524288 Text is right aligned- or -MsgBoxStyle.MsgBoxRightvbMsgBoxRtlReading 1048576 Specifies text should- or - appear as right-to-leftMsgBoxStyle.MsgBoxRtl reading on Hebrew andReading Arabic systems
  10. 10. • When you use MsgBox with the option to display more than one button (i.e., from the first group, anything other than "vbOKOnly"), you can test which button the user clicked by comparing the return value of the Msgbox function with one of these values.• For each return value, a corresponding built-in constant (either the classic "vb" constants or the .NET "MsgBoxResult" enumeration constants) may also be used.• VB.NET also provides the "MsgBoxResult" enumeration constants which can be used as an alternative to the "vb" constants.•
  11. 11. Msgbox constantsConstant Value DescriptionvbOK 1 The OK button was-or- pressedMsgBoxResult.OKvbCancel 2 The Cancel button was-or- pressedMsgBoxResult.CancelvbAbort 3 The Abort button was-or- pressedMsgBoxResult.AbortvbRetry 4 The Retry button was-or- pressedMsgBoxResult.Retry
  12. 12. vbIgnore 5 The Ignore button was-or- pressedMsgBoxResult.IgnorevbYes 6 The Yes button was-or- pressedMsgBoxResult.YesvbNo 7 The No button was-or- pressedMsgBoxResult.No
  13. 13. MessageBox Class• As an alternative, .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!")
  14. 14. • The Show method has various overloaded forms.• syntax – [DialogResult = ] MessageBox.Show([window ,] - prompt –                                   [, caption] – [, MessageBoxButtons] – [, MessageBoxIcon] – [, MessageBoxDefaultButton] – [, MessageBoxOptions])
  15. 15. 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 argumentcaption The text to display in the title bar of the message box. If omitted, the project name will be displayed.
  16. 16. MessageBoxButtons Specifies which buttons to display on the message box. Possible values are: •MessageBoxButtons.AbortRetryIgnore (displays the Abort, Retry, and Ignore buttons) •MessageBoxButtons.OK (displays the OK button) •MessageBoxButtons.OKCancel (displays the OK and Cancel buttons) •MessageBoxButtons.RetryCancel (displays the Retry and Cancel buttons) •MessageBoxButtons.YesNo (displays the Yes and No buttons) •MessageBoxButtons.YesNoCancel (displays the Yes, No, and Cancel buttons)
  17. 17. MessageBoxIcon Specifies which icon to display on the message box. Possible values are:Value IconMessageBoxIcon.Error- or -MessageBoxIcon.Hand- or -MessageBoxIcon.StopMessageBoxIcon.Question MessageBoxIcon.Exclamation- or -MessageBoxIcon.Warning MessageBoxIcon.Asterisk- or -MessageBoxIcon.Information (none)MessageBoxIcon.None
  18. 18. MessageBoxDefaultButton Specifies the default button for the message box. Possible values are: • MessageBoxDefaultButton.Button1 (the first message box button is the default button) • MessageBoxDefaultButton.Button2 (the second message box button is the default button) • MessageBoxDefaultButton.Button3 (the third message box button is the default button)MessageBoxOptions Allows specialized options to be specified. Possible values are: •          MessageBoxOptions.DefaultDesktopOnly (displays the message box on the active desktop) •         MessageBoxOptions.RightAlign (displays the message box text right-aligned) •         MessageBoxOptions.RtlReading (displays the text in right-to-left reading order) •         MessageBoxOptions.ServiceNotification (displays the message box on the active desktop, even if there is no user logged on to the computer
  19. 19. Value DescriptionDialogResult.OK The OK button was pressedDialogResult.Cancel The Cancel button was pressedDialogResult.Abort The Abort button was pressedDialogResult.Retry The Retry button was pressedDialogResult.Ignore The Ignore button was pressedDialogResult.Yes The Yes button was pressedDialogResult.No The No button was pressedDialogResult.None Nothing is returned from the dialog box. This means that the modal dialog continues running.
  20. 20. 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.
  21. 21. • Private Sub Button7_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