A message box is similar to an input box. The message box can be used as either a function (returns an answer) or a statement (no answer returned) The programmer can select the type of message box to display. The type indicates which command buttons to display: Yes, No, OK, Cancel, Abort, Retry, etc.
There are two formats for opening a message box: as a statement or as a function that returns an answer.
The format for the statement is:
msgBox prompt, type, Title
This method is usually used when the only button on the message box is OK. We do not need to find out which button they pressed, they’re going to press OK!
In the illustration below, the prompt is "Press to change to red". The title is "RED".
Modify the previous code as shown below:
'Programmer: Janet Joy 'Format for message box: answer = msgBox( prompt,type,title) Public Class Form1 Private Sub BtnPress_Click(sender As Object, e As EventArgs) Handles BtnPress.Click MsgBox("Press to change to red", MsgBoxStyle.OkOnly, "RED") Me.BackColor = Color.Red End Sub End Class
As soon as the user presses OK, the message box disappears and the form turns red.
In the next example the user is given a choice. Since we do not know which button they will press, we will use a function so we can look at the answer.
The format for the function is:
answer= msgBox(prompt, type , title)
We will turn the form red only if the press Yes.Notice that as you type the code most of the choices are selected from pop-up boxes.
'Programmer: Janet Joy 'Format for message box: answer = msgBox( prompt,type,title) Public Class Form1 Private Sub BtnPress_Click(sender As Object, e As EventArgs) Handles BtnPress.Click Dim Answer As MsgBoxResult Answer = MsgBox("Do you like red?", MsgBoxStyle.YesNo, "Color") If Answer = MsgBoxResult.Yes Then Me.BackColor = Color.Red End If End Sub End ClassExperiment: Once the user press yes, the button does not have any affect. If they click No generate a color with red = 0 and random values for green and blue.