C#winform中MessageBox用法大全(附效果图)
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
C#winform中MessageBox用法大全(附效果图)
C# winform 中MessageBox用法大全(附效果图)2012-11-22 09:34 33648人阅读评论(0) 收藏举报
分类:
Winform(25)
我们在程序中经常会用到MessageBox。
MessageBox.Show()共有21中重载方法。
现将其常见用法总结如下:
1.MessageBox.Show(“Hello~~~~”);
最简单的,只显示提示信息。
2.MessageBox.Show(“There are something wrong!”,“ERROR”);
可以给消息框加上标题。
3.if(MessageBox.Show(“Delete this user?”, “Confirm Message”, MessageBoxButtons.OKCancel) == DialogResult.OK) {
//delete
}
询问是否删除时会用到这个。
4.if(MessageBox.Show(“Delete this user?”, “Confirm Message”,
MessageBoxButtons.OKCancel,MessageBoxIcon.Question) ==
DialogResult.OK)
{
//delete
}
可以给MessageBox加上一个Icon,.net提供常见的Icon共选择。
5.if(MessageBox.Show(“Delete this user?”, “Confirm Message”, MessageBoxButtons.OKCancel, MessageBoxIcon.Question,MessageBoxDefaultButton.Button2) == DialogResult.OK)
{
//delete
}
可以改变MessageBox的默认焦点,如下:
6.if(MessageBox.Show(“Delete this user?”, “Confirm Message”, MessageBoxButtons.OKCancel, MessageBoxIcon.Question,MessageBoxDefaultButton.Button2, MessageBoxOptions.RtlReading) == DialogResult.OK) {
//delete
}
反向显示:
7.if(MessageBox.Show(“Delete this user?”, “Confirm Message”, MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2, MessageBoxOptions.RightAlign,true) == DialogResult.OK) {
//delete
}
添加Help按钮:
8.if(MessageBox.Show(“Delete this user?”, “Confirm Message”, MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1, MessageBoxOptions.RtlReading, @”/folder/file.htm”) == DialogResult.OK)
{
//delete
}
指定帮助文件的路径,点击即可打开该路径下的帮助文件。
9.//HelpNavigator指定常数来指示要显示的帮助文件元素。
Find 帮助文件将打开到搜索页。
if(MessageBox.Show(“Delete this user?”, “Confirm Message”, MessageBoxButtons.OKCancel,
MessageBoxIcon.Question, MessageBoxDefaultButton.Button1, MessageBoxOptions.RtlReading, @”/folder/file.htm”, HelpNavigator.Find) == DialogResult.OK)
{
//delete
}
还有一些用法,不是太实用这里就不一一介绍了,有兴趣的朋友可以参考下这里:MSDN的MessageBox类。
转自:/xuenzhen123/article/details/4808005
private void button1_Click(object sender, EventArgs e)
2 {
3 MessageBox.Show(" 1 个参数 "
4 );
5 }
6
7
8
9
10private void button2_Click(object sender, EventArgs
e)
11 {
12 MessageBox.Show(" 2 个参数。
",
13"亮仔提示"
14 );
15 }
16
17
18
19
20
21
22private void button3_Click(object sender, EventArgs
e)
23 {
24 MessageBox.Show(" 3 个参数。
",
25" 亮仔提示",
26 MessageBoxButtons.YesNoCancel
27 );
28 }
29
30
31
32
33
34private void button4_Click(object sender, EventArgs
e)
35 {
36 MessageBox.Show(" 4 个参数。
",
37" 亮仔提示",
38 MessageBoxButtons.OKCancel,
39 MessageBoxIcon.Warning
40 );
41 }
42
43
44
45
46
47
48private void button5_Click(object sender, EventArgs
e)
49 {
50 MessageBox.Show(" 5 个参数。
",
51" 亮仔提示",
52 MessageBoxButtons.OKCancel,
53 MessageBoxIcon.Warning,
54 MessageBoxDefaultButton.Button2
55 );
56 }
57
58
59
60
61
62
63private void button6_Click(object sender, EventArgs
e)
64 {
65 MessageBox.Show(" 6 个参数。
",
66" 亮仔提示",
67 MessageBoxButtons.OKCancel,
68 MessageBoxIcon.Warning,
69 MessageBoxDefaultButton.Button2,
70 MessageBoxOptions.RtlReading //ServiceNotification//.RightAlign // 标题向右对齐。
71 );
72 }
73
74
75
76
77
78
79private void button7_Click(object sender, EventArgs e)
80 {
81MessageBox.Show(" 7 个参数。
帮助菜单不可用。
",
82" 亮仔提示",
83 MessageBoxButtons.OKCancel,
84 MessageBoxIcon.Warning,
85 MessageBoxDefaultButton.Button2,
86 MessageBoxOptions.RightAlign,
87true// 标题向右对齐。
);
88 }
89
90
91
92
93
94
95
96private void button8_Click(object sender, EventArgs
e)
97 {
98 MessageBox.Show(" 7 个参数。
帮助菜单可用。
",
99" 亮仔提示",
100 MessageBoxButtons.OKCancel,
101 MessageBoxIcon.Warning,
102 MessageBoxDefaultButton.Button2,
103 MessageBoxOptions.RightAlign , // 要使用默认风格,此处参数可设为 0
104@"C:\Documents and Settings\Administrator\桌面\新建文本文档.txt"
105 );
106 }
107
108
109
1101. 1个参数。
111 MessageBox.Show(string text);
112// 显示具有指定文本的消息框。
113// 参数:text: 要在消息框中显示的文本。
114// 返回结果: System.Windows.Forms.DialogResult 值之一。
115
1162. 2个参数。
117 MessageBox.Show(string text, string caption);
118// 显示具有指定文本和标题的消息框。
119// 参数:
120// text: 要在消息框中显示的文本。
121// caption: 要在消息框的标题栏中显示的文本。
122// 返回结果: System.Windows.Forms.DialogResult 值之一。
123
1243. 3个参数。
125MessageBox.Show(string text, string caption, MessageBoxButtons buttons);
126// 显示具有指定文本、标题和按钮的消息框。
127// 参数:
128// text: 要在消息框中显示的文本。
129// caption: 要在消息框的标题栏中显示的文本。
130// buttons: System.Windows.Forms.MessageBoxButtons 值之一,可指定在消息框中显示哪些按钮。
131// 返回结果: System.Windows.Forms.DialogResult 值之一。
132// 异常:
133
//ponentModel.InvalidEnumArgumentException:
指定的buttons 参数不是System.Windows.Forms.MessageBoxButtons 的成员。
134// System.InvalidOperationException: 试图在运行模式不是用户交互模式的进程中显示System.Windows.Forms.MessageBox。
这是由erInteractive 属性指定的。
135
1364. 4个参数。
137MessageBox.Show(string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon);
138// 显示具有指定文本、标题、按钮和图标的消息框。
139// 参数:
140// text: 要在消息框中显示的文本。
141// caption: 要在消息框的标题栏中显示的文本。
142// buttons:
System.Windows.Forms.MessageBoxButtons 值之一,可指定在消息框中显示哪些按钮。
143// icon: System.Windows.Forms.MessageBoxIcon 值之一,它指定在消息框中显示哪个图标。
144// 返回结果: System.Windows.Forms.DialogResult 值之一。
145// 异常:
146// ponentModel.InvalidEnumArgumentException: 指定的buttons 参数不是System.Windows.Forms.MessageBoxButtons 的成员。
- 或- 指定的 icon24参数不是 System.Windows.Forms.MessageBoxIcon 的成员。
147// System.InvalidOperationException: 试图在运行模式不是用户交互模式的进程中显示System.Windows.Forms.MessageBox。
这是由erInteractive属性指定的。
148
1495. 5个参数。
150MessageBox.Show(string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon, MessageBoxDefaultButton defaultButton);
151// 显示具有指定文本、标题、按钮、图标和默认按钮的消息框。
152// 参数:
153// text: 要在消息框中显示的文本。
154// caption: 要在消息框的标题栏中显示的文本。
155// buttons: System.Windows.Forms.MessageBoxButtons 值之一,可指定在消
息框中显示哪些按钮。
156// icon: System.Windows.Forms.MessageBoxIcon 值之一,它指定在消息框中显示哪个图标。
157// default Button: System.Windows.Forms.MessageBoxDefaultButton 值之一,可指定消息框中的默认按钮。
158// 返回结果: System.Windows.Forms.DialogResult 值之一。
159// 异常:
160// ponentModel.InvalidEnumArgumentException: buttons 不是 System.Windows.Forms.MessageBoxButtons 的成员。
- 或 - icon 不是 System.Windows.Forms.MessageBoxIcon的成员。
- 或- defaultButton 不是System.Windows.Forms.MessageBoxDefaultButton 的成员。
161// System.InvalidOperationException: 试图在运行模式不是用户交互模式的进程中显示System.Windows.Forms.MessageBox。
这是由erInteractive属性指定的。
162
1636. 6个参数。
164MessageBox.Show(string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon,MessageBoxDefaultButton defaultButton, MessageBoxOptions options);
165// 显示具有指定文本、标题、按钮、图标、默认按钮和选项的消息框。
166// 参数:
167// text: 要在消息框中显示的文本。
168// caption: 要在消息框的标题栏中显示的文本
169// buttons: System.Windows.Forms.MessageBoxButtons 值之一,可指定在消息框中显示哪些按钮。
170// icon: System.Windows.Forms.MessageBoxIcon 值之一,它指定在消息框中显示哪个图标。
171// defaultButton: System.Windows.Forms.MessageBoxDefaultButton 值之一,可指定消息框中的默认按钮。
172// options: System.Windows.Forms.MessageBoxOptions 值之一,可指定将对消息框使用哪些显示和关联选项。
若要使用默认值,请传入0。
173// 返回结果: System.Windows.Forms.DialogResult 值之一。
174// 异常:
175// ponentModel.InvalidEnumArgumentException: buttons 不是 System.Windows.Forms.MessageBoxButtons 的成员。
- 或 - icon 不是 System.Windows.Forms.MessageBoxIcon的成员。
- 或- 指定的defaultButton 不是System.Windows.Forms.MessageBoxDefaultButton的成员。
176// System.InvalidOperationException: 试图在运行模式不是用户交互模式的进程中显示System.Windows.Forms.MessageBox。
这是由erInteractive属性指定的。
177// System.ArgumentException: options 同时指定了System.Windows.Forms.MessageBoxOptions.DefaultDesktopOnl
y 和System.Windows.Forms.MessageBoxOptions.ServiceNotification。
- 或- buttons42指定了无效的System.Windows.Forms.MessageBoxButtons 组合。
178
1797. 7个参数一。
180MessageBox.Show(string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon,MessageBoxDefaultButton defaultButton, MessageBoxOptions options, bool displayHelpButton);
181// 显示一个具有指定文本、标题、按钮、图标、默认按钮、选项和“帮助”按钮的消息框。
182// 参数:
183// text: 要在消息框中显示的文本。
184// caption: 要在消息框的标题栏中显示的文本。
185// buttons: System.Windows.Forms.MessageBoxButtons 值之一,可指定在消息框中显示哪些按钮。
186// icon: System.Windows.Forms.MessageBoxIcon 值之一,它指定在消息框中显示哪个图标。
187// defaultButton: System.Windows.Forms.MessageBoxDefaultButton 值之一,可指定消息框中的默认按钮。
188// options: System.Windows.Forms.MessageBoxOptions 值之一,可指定将对消息框使用哪些显示和关联选项。
若要使用默认值,请传入0。
189// helpButton: 如果显示“帮助”按钮,则为 true;否则为 false。
默认为 false。
190// 返回结果: System.Windows.Forms.DialogResult 值之一。
191// 异常:34
192//
ponentModel.InvalidEnumArgumentException: buttons 不是 System.Windows.Forms.MessageBoxButtons 的成员。
- 或 - icon 不是 System.Windows.Forms.MessageBoxIcon的成员。
- 或- 指定的defaultButton 不是System.Windows.Forms.MessageBoxDefaultButton的成员。
193// System.InvalidOperationException: 试图在运行模式不是用户交互模式的进程中显示System.Windows.Forms.MessageBox。
这是由erInteractive属性指定的。
194// System.ArgumentException: options 同时指定了System.Windows.Forms.MessageBoxOptions.DefaultDesktopOnl
y 和System.Windows.Forms.MessageBoxOptions.ServiceNotification。
- 或- buttons指定了无效的System.Windows.Forms.MessageBoxButtons 组合。
195
1968. 7个参数二
197MessageBox.Show(string text, string caption, MessageBoxButtons buttons,MessageBoxIcon icon, MessageBoxDefaultButton defaultButton,MessageBoxOptions options, string helpFilePath);
198// 使用指定的帮助文件显示一个具有指定文本、标题、按钮、图标、默认按钮、选项和“帮助”按钮的消息框。
199// 参数:
200// text: 要在消息框中显示的文本。
201// caption: 要在消息框的标题栏中显示的文本。
202// buttons: System.Windows.Forms.MessageBoxButtons 值之一,可指定在消息框中显示哪些按钮。
203// icon: System.Windows.Forms.MessageBoxIcon 值之一,它指定在消息框中显示哪个图标。
204// defaultButton: System.Windows.Forms.MessageBoxDefaultButton 值之一,可指定消息框中的默认按钮。
205// options: System.Windows.Forms.MessageBoxOptions 值之一,可指定将对消息框使用哪些显示和关联选项。
若要使用默认值,请传入0。
206// helpFilePath: 用户单击“帮助”按钮时显示的“帮助”文件的路径和名称。
207// 返回结果: System.Windows.Forms.DialogResult 值之一。
208// 异常:
209// ponentModel.InvalidEnumArgumentException: buttons 不是 System.Windows.Forms.MessageBoxButtons 的成员。
- 或 - icon 不是System.Windows.Forms.MessageBoxIcon的成员。
- 或- 指定的defaultButton 不是System.Windows.Forms.MessageBoxDefaultButton的成员。
210// System.InvalidOperationException: 试图在运行模式不是用户交互模式的进程中显示System.Windows.Forms.MessageBox。
这是由erInteractive属性指定的。
211// System.ArgumentException: options 同时指定了System.Windows.Forms.MessageBoxOptions.DefaultDesktopOnl
y 和System.Windows.Forms.MessageBoxOptions.ServiceNotification。
- 或- buttons指定了无效的System.Windows.Forms.MessageBoxButtons 组合。
212
213
214
215
216
217下面对C#中的预编译指令进行介绍:
2181.#define和#undef
219用法:
220#define DEBUG
221#undef DEBUG
222 #define告诉编译器,我定义了一个DEBUG的一个符号,他类似一个变量,但是它没有具体的值,可以将它看为一个符号而已。
#undef就是删除这个符号的定义。
如果符号DEBUG没定义过,则#undef不起作用,否则#define不起作用。
二者都必须放在源代码之前。
二者的顺序看代码的顺序:
223#define DEBUG
224#undef DEBUG
225这样的话,DEBUG是没有定义的,如果二者换个顺序,编译器就认为DEBUG被定义了
2262.#if、#elif、#else、#endif
227这个告诉编译器进行编译代码的流程控制。
考虑下面代码:
228#if DEBUG
229 Console.Write("debug");
230#elif RELEASE
231 Console.Write("release");
232#else
233 Console.Write("other");
234#endif
235以上代码就是说如果定义了DEBUG则输出debug,定
义了RELEASE,则输出realse,否则输出other。
如果定义了DEBUG 和REALSE会怎么样呢?各位可以自己试一下。
2363.#warning、#error
237通过这两个指定可以告诉编译器,出一个警告还是错误信息。
除了错误信息以后,编译将停止。
238参考下面的代码(C#在Debug状态下自动定义DEBUG 标志,但Release状态不会自动定义RELEASE标志):
239#if DEBUG
240#warning现在是Debug状态
241#elif RELEASE
242#warning现在是Release状态
243#else
244#error并清楚什么状态
245#endif
2464.#region和#endregion
247这个两个用来组成代码块
2485.#line
249这个指令可以改变编译器在警告和错误信息中显示的文件名和行号信息,用#line default把行号恢复为默认的行号。
250#line [ number ["file_name"] | default ]
251 number
252要为源代码文件中后面的行指定的编号。
253"file_name"(可选)
254希望出现在编译器输出中的文件名。
默认情况下,使用源代码文件的实际名称。
文件名必须括在双引号 ("") 中。
255default
256重置文件中的行编号。
257备注
258#line可能由生成过程中的自动中间步骤使用。
例如,如果中间步骤从原始的源代码文件中移除行,但是您仍希望编译器基
于文件中的原始行号生成输出,则可以移除行,然后用#line 模拟原始行号。
259下面的示例说明如何报告与行号关联的两个警告。
#line 200 指令迫使行号为200(尽管默认值为#7)。
另一行 (#9) 作为默认 #line 指令的结果跟在通常序列后。
260示例1:
261// preprocessor_line.cs
262public class MyClass2
263 {
264public static void Main()
265 {
266#line 200
267int i; // line 200
268#line default
269char c; // line 9
270 }
271 }
272示例2:
273下面的示例说明调试器如何忽略代码中的隐藏行。
运行此示例时,它将显示三行文本。
但是,当设置如示例所示的断点并按F10 键逐句通过代码时,您将看到调试器忽略了隐藏行。
另请注意,即使在隐藏行上设置断点,调试器仍会忽略它。
274// preprocessor_linehidden.cs
275using System;
276class MyClass
277 {
278public static void Main()
279 {
280 Console.WriteLine("Normal line #1."); // Set a break point here.
281#line hidden
282 Console.WriteLine("Hidden line.");
283#line default
284 Console.WriteLine("Normal line #2.");
285 }
286 }
2876.#pragma
288可以抑制或恢复指定的编译警告。
与命令行选项不同,#pragma指令可以在类和方法上执行,对抑制什么警告和抑制的时间进行更精细的控制。
289#pragma warning disable 169
290public class Aclass
291 {
292int nFiled;
293 }
294#pragma warning restore 169。