A MessageBox is a predefined dialog box that displays application-related information to the user. Message boxes are also used to request information from the user.
To display information to the user in a message box
- Navigate to where you would like to add the code for the message box.
- Add code using the MessageBox.Show method.The following code demonstrates how to call the Show method of the MessageBox class to display information to the user. The call to the Show method uses the optional styleparameter to specify the type of icon to display in the message box that best fits the type of message box being displayed:
' Visual Basic Public Sub PerformCalculations() ' Code is entered here that performs a calculation. ' Display a message box informing the user that the calculations ' are complete. MessageBox.Show("The calculations are complete", "My Application", _ MessageBoxButtons.OKCancel, MessageBoxIcon.Asterisk) End Sub // C# public void PerformCalculations() { // Code is entered here that performs a calculation // Display a message box informing the user that the calculations // are complete MessageBox.Show ("The calculations are complete", "My Application", MessageBoxButtons.OKCancel, MessageBoxIcon.Asterisk); } // C++ public: void PerformCalculations() { // Code is entered here that performs a calculation // Display a message box informing the user that the calculations // are complete MessageBox::Show(S"The calculations are complete", S"My Application", MessageBoxButtons::OKCancel, MessageBoxIcon::Asterisk); }
Message boxes can also receive input. The Show method of the MessageBox class returns a value that can be used to determine a choice made by the user. You can store this value in an integer or compare the value returned when you display the message box using an
if
statement. The Style parameter of the Show method can be set to display the proper buttons to ask a user for information.To display a message box to request information
- Open the Code Editor for your class and navigate to where you would like to add the code for the message box.
- Add code that uses the Show method of the MessageBox class to display a message box.The following code demonstrates how to call the MessageBox method to retrieve information from the user and then determine the value that was selected:
' Visual Basic Public Sub ExitApplication() ' Display a message box asking users if they ' want to exit the application. If MessageBox.Show ("Do you want to exit?", "My Application", _ MessageBoxButtons.YesNo, MessageBoxIcon.Question) _ = DialogResult.Yes Then Application.Exit End If End Sub // C# public void ExitApplication() { // Display a message box asking users if they // want to exit the application. if (MessageBox.Show ("Do you want to exit?", "My Application", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { Application.Exit(); } } // C++ public: void ExitApplication() { // Display a message box asking users if they // want to exit the application. if (MessageBox::Show(S"Do you want to exit?", S"My Application", MessageBoxButtons::YesNo, MessageBoxIcon::Question) == DialogResult::Yes) { Application::Exit(); } }
Visual Basic Note In Visual Basic, using
MsgBox()to create a message box to display to users is still supported, though the new syntax,MessageBox.Show(), as seen above, is preferred. Thus, in reference to the previous code example, the following is acceptable in Visual Basic.For more information about' Visual Basic Public Sub ExitApplication() If MsgBox("Do you want to exit?", MsgBoxStyle.Exclamation, _ "My Application") = MsgBoxResult.Yes Then Application.Exit() End If End Sub
MsgBox()
, see MsgBox Function.
ref : http://msdn.microsoft.com/en-us/library/aa984357(v=vs.71).aspx
No comments:
Post a Comment