1 private void button1_Click(object sender, EventArgs e)
2 {
3 MessageBox.Show(" 1 個參數 "
4 );
5 }
6

7
8
9
10 private void button2_Click(object sender, EventArgs e)
11 {
12 MessageBox.Show(" 2 個參數。。 ",
13 "亮仔提示"
14 );
15 }
16
17

18
19
20
21
22 private void button3_Click(object sender, EventArgs e)
23 {
24 MessageBox.Show(" 3 個參數。。。 ",
25 " 亮仔提示",
26 MessageBoxButtons.YesNoCancel
27 );
28 }
29
30
31
32

33
34 private 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
48 private 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
63 private 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
79 private void button7_Click(object sender, EventArgs e)
80 {
81 MessageBox.Show(" 7 個參數。。幫助菜單不可用。。。。。 ",
82 " 亮仔提示",
83 MessageBoxButtons.OKCancel,
84 MessageBoxIcon.Warning,
85 MessageBoxDefaultButton.Button2,
86 MessageBoxOptions.RightAlign,
87 true // 標題向右對齊。。。。。 );
88 }
89
90

91
92
93
94
95
96 private 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

110 1. 1個參數。
111 MessageBox.Show(string text);
112 // 顯示具有指定文本的消息框。
113 // 參數:text: 要在消息框中顯示的文本。
114 // 返回結果: System.Windows.Forms.DialogResult 值之一。
115
116 2. 2個參數。
117 MessageBox.Show(string text, string caption);
118 // 顯示具有指定文本和標題的消息框。
119 // 參數:
120 // text: 要在消息框中顯示的文本。
121 // caption: 要在消息框的標題欄中顯示的文本。
122 // 返回結果: System.Windows.Forms.DialogResult 值之一。
123
124 3. 3個參數。
125 MessageBox.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 //System.ComponentModel.InvalidEnumArgumentException: 指定的 buttons 參數不是 System.Windows.Forms.MessageBoxButtons 的成員。
134 // System.InvalidOperationException: 試圖在運行模式不是用戶交互模式的進程中顯示 System.Windows.Forms.MessageBox。這是由 System.Windows.Forms.SystemInformation.UserInteractive 屬性指定的。
135
136 4. 4個參數。
137 MessageBox.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 // System.ComponentModel.InvalidEnumArgumentException: 指定的 buttons 參數不是 System.Windows.Forms.MessageBoxButtons 的成員。- 或 - 指定的 icon24參數不是 System.Windows.Forms.MessageBoxIcon 的成員。
147 // System.InvalidOperationException: 試圖在運行模式不是用戶交互模式的進程中顯示 System.Windows.Forms.MessageBox。這是由 System.Windows.Forms.SystemInformation.UserInteractive屬性指定的。
148
149 5. 5個參數。
150 MessageBox.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 // System.ComponentModel.InvalidEnumArgumentException: buttons 不是 System.Windows.Forms.MessageBoxButtons 的成員。- 或 - icon 不是 System.Windows.Forms.MessageBoxIcon的成員。- 或 - defaultButton 不是 System.Windows.Forms.MessageBoxDefaultButton 的成員。
161 // System.InvalidOperationException: 試圖在運行模式不是用戶交互模式的進程中顯示 System.Windows.Forms.MessageBox。這是由 System.Windows.Forms.SystemInformation.UserInteractive屬性指定的。
162
163 6. 6個參數。
164 MessageBox.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 // System.ComponentModel.InvalidEnumArgumentException: buttons 不是 System.Windows.Forms.MessageBoxButtons 的成員。- 或 - icon 不是 System.Windows.Forms.MessageBoxIcon的成員。- 或 - 指定的 defaultButton 不是 System.Windows.Forms.MessageBoxDefaultButton的成員。
176 // System.InvalidOperationException: 試圖在運行模式不是用戶交互模式的進程中顯示 System.Windows.Forms.MessageBox。這是由System.Windows.Forms.SystemInformation.UserInteractive屬性指定的。
177 // System.ArgumentException: options 同時指定了System.Windows.Forms.MessageBoxOptions.DefaultDesktopOnly 和System.Windows.Forms.MessageBoxOptions.ServiceNotification。- 或 - buttons42指定了無效的System.Windows.Forms.MessageBoxButtons 組合。
178
179 7. 7個參數一。
180 MessageBox.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 // System.ComponentModel.InvalidEnumArgumentException: buttons 不是 System.Windows.Forms.MessageBoxButtons 的成員。- 或 - icon 不是 System.Windows.Forms.MessageBoxIcon的成員。- 或 - 指定的 defaultButton 不是 System.Windows.Forms.MessageBoxDefaultButton的成員。
193 // System.InvalidOperationException: 試圖在運行模式不是用戶交互模式的進程中顯示 System.Windows.Forms.MessageBox。這是由System.Windows.Forms.SystemInformation.UserInteractive屬性指定的。
194 // System.ArgumentException: options 同時指定了System.Windows.Forms.MessageBoxOptions.DefaultDesktopOnly 和System.Windows.Forms.MessageBoxOptions.ServiceNotification。- 或 - buttons指定了無效的 System.Windows.Forms.MessageBoxButtons 組合。
195
196 8. 7個參數二
197 MessageBox.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 // System.ComponentModel.InvalidEnumArgumentException: buttons 不是 System.Windows.Forms.MessageBoxButtons 的成員。- 或 - icon 不是System.Windows.Forms.MessageBoxIcon的成員。- 或 - 指定的 defaultButton 不是System.Windows.Forms.MessageBoxDefaultButton的成員。
210 // System.InvalidOperationException: 試圖在運行模式不是用戶交互模式的進程中顯示System.Windows.Forms.MessageBox。這是由System.Windows.Forms.SystemInformation.UserInteractive屬性指定的。
211 // System.ArgumentException: options 同時指定了System.Windows.Forms.MessageBoxOptions.DefaultDesktopOnly 和System.Windows.Forms.MessageBoxOptions.ServiceNotification。- 或 - buttons指定了無效的 System.Windows.Forms.MessageBoxButtons 組合。
212
213
214
215
216
217 下面對C#中的預編譯指令進行介紹:
218 1.#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被定義了
226 2.#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會怎么樣呢?各位可以自己試一下。
236 3.#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
246 4.#region 和#endregion
247 這個兩個用來組成代碼塊
248 5.#line
249 這個指令可以改變編譯器在警告和錯誤信息中顯示的文件名和行號信息,用#line default把行號恢復為默認的行號。
250 #line [ number ["file_name"] | default ]
251 number
252 要為源代碼文件中后面的行指定的編號。
253 "file_name"(可選)
254 希望出現在編譯器輸出中的文件名。默認情況下,使用源代碼文件的實際名稱。文件名必須括在雙引號 ("") 中。
255 default
256 重置文件中的行編號。
257 備注
258 #line 可能由生成過程中的自動中間步驟使用。例如,如果中間步驟從原始的源代碼文件中移除行,但是您仍希望編譯器基於文件中的原始行號生成輸出,則可以移除行,然后用 #line 模擬原始行號。
259 下面的示例說明如何報告與行號關聯的兩個警告。#line 200 指令迫使行號為 200(盡管默認值為 #7)。另一行 (#9) 作為默認 #line 指令 的結果跟在通常序列后。
260 示例1:
261 // preprocessor_line.cs
262 public class MyClass2
263 {
264 public static void Main()
265 {
266 #line 200
267 int i; // line 200
268 #line default
269 char c; // line 9
270 }
271 }
272 示例2:
273 下面的示例說明調試器如何忽略代碼中的隱藏行。運行此示例時,它將顯示三行文本。但是,當設置如示例所示的斷點並按 F10 鍵逐句通過代碼時,您將看到調試器忽略了隱藏行。另請注意,即使在隱藏行上設置斷點,調試器仍會忽略它。
274 // preprocessor_linehidden.cs
275 using System;
276 class MyClass
277 {
278 public 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 }
287 6.#pragma
288 可以抑制或恢復指定的編譯警告。與命令行選項不同,#pragma指令可以在類和方法上執行,對抑制什么警告和抑制的時間進行更精細的控制。
289 #pragma warning disable 169
290 public class Aclass
291 {
292 int nFiled;
293 }
294 #pragma warning restore 169