1. 显示可包含文本、按钮和符号(通知并指示用户)的消息框。
2. MessageBox 方法:
- Show(String):显示具有指定文本的消息框。
- Show(String, String):显示具有指定文本和标题的消息框。
- Show(String, String, MessageBoxButtons):
显示具有指定文本、标题和按钮的消息框。
privatevoid validateUserEntry() { // Checks the value of the text. if(serverName.Text.Length == 0) { // Initializes the variables to pass to the MessageBox.Show method. string message = "You did not enter a server name. Cancel this operation?"; string caption = "Error Detected in Input"; MessageBoxButtons buttons = MessageBoxButtons.YesNo; DialogResult result; // Displays the MessageBox. result = MessageBox.Show(message, caption, buttons); if (result == System.Windows.Forms.DialogResult.Yes) { // Closes the parent form. this.Close(); } } }
-
- MessageBoxButtons 枚举
- OK,OKCancel,AbortRetryIgnor,YesNoCancel,YesNo,RetryCancel
- Show(String, String, MessageBoxButtons, MessageBoxIcon):
在指定对象的前面显示具有指定文本、标题、按钮和图标的消息框。- MessageBoxIcon 枚举
- None,Hand,Question,Exclamation,Asterisk,Stop,Error,Warning,Information
- Show(String, String, MessageBoxButtons, MessageBoxIcon, MessageBoxDefaultButton):
显示具有指定文本、标题、按钮、图标和默认按钮的消息框。- MessageBoxDefaultButton 枚举
- Button1,Button2,Button3
- Show(String, String, MessageBoxButtons, MessageBoxIcon, MessageBoxDefaultButton, MessageBoxOptions):
显示具有指定文本、标题、按钮、图标、默认按钮和选项的消息框。 - 参考:http://zyq5510806.blog.163.com/blog/static/122738986200911185925316/
- Show方法的返回值为DialogResult枚举,因此可以通过根据返回的DialogResult枚举值,来获取用户的按键!!!上面的FormClosing事件有演示这个方法。