C# WinForm 中 MessageBox的使用詳解

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

  
  
         private void button2_Click(object sender, EventArgs e)
         {
             MessageBox.Show(" 2 個參數。。 ",
                                  "亮仔提示"
                                  );
         }
                                      
 
  
  
 
  
         private void button3_Click(object sender, EventArgs e)
         {
             MessageBox.Show(" 3 個參數。。。 ",
                                 " 亮仔提示",
                                 MessageBoxButtons.YesNoCancel
                                 );
         } 
                                      
  
  
  
 
         private void button4_Click(object sender, EventArgs e)
         {
             MessageBox.Show(" 4 個參數。。。  ",
                                 " 亮仔提示",
                                 MessageBoxButtons.OKCancel,
                                 MessageBoxIcon.Warning
                                 );
         }
                                      
  
  
  
  
  
         private void button5_Click(object sender, EventArgs e)
         {
             MessageBox.Show(" 5 個參數。。 。  ",
                                 " 亮仔提示",
                                 MessageBoxButtons.OKCancel,
                                 MessageBoxIcon.Warning,
                                 MessageBoxDefaultButton.Button2
                                 );
         }
                                      
  
  
  
  
  
         private void button6_Click(object sender, EventArgs e)
         {
             MessageBox.Show(" 6 個參數。。。  ",
                                 " 亮仔提示",
                                 MessageBoxButtons.OKCancel,
                                 MessageBoxIcon.Warning,
                                 MessageBoxDefaultButton.Button2,
                                 MessageBoxOptions.RtlReading      //ServiceNotification//.RightAlign   // 標題向右對齊。
                                 );
         }
           
                           
  
  
  
  
 
         private void button7_Click(object sender, EventArgs e)
         {
             MessageBox.Show(" 7 個參數。。幫助菜單不可用。。。。。  ",
                                 " 亮仔提示",
                                 MessageBoxButtons.OKCancel,
                                 MessageBoxIcon.Warning,
                                 MessageBoxDefaultButton.Button2,
                                 MessageBoxOptions.RightAlign,
                                 true   // 標題向右對齊。。。。。                                );
         }
                    
                    

  
 
  
  
  
         private void button8_Click(object sender, EventArgs e)
         {
             MessageBox.Show(" 7 個參數。幫助菜單    可用。   ",
                                 " 亮仔提示",
                                 MessageBoxButtons.OKCancel,
                                 MessageBoxIcon.Warning,
                                 MessageBoxDefaultButton.Button2,
                                MessageBoxOptions.RightAlign  ,   // 要使用默認風格,此處參數可設爲 0    
                                 @"C:\Documents and Settings\Administrator\桌面\新建文本文檔.txt"
                                 );
         }
            
                          
 

 1.     1個參數。 
  MessageBox.Show(string text); 
 //     顯示具有指定文本的消息框。 
 // 參數:text:     要在消息框中顯示的文本。 
 // 返回結果:     System.Windows.Forms.DialogResult 值之一。
   
 2.     2個參數。 
 MessageBox.Show(string text, string caption); 
 //     顯示具有指定文本和標題的消息框。 
 // 參數: 
 //   text:      要在消息框中顯示的文本。 
 //   caption:     要在消息框的標題欄中顯示的文本。 
 // 返回結果:      System.Windows.Forms.DialogResult 值之一。
   
 3.     3個參數。 
  MessageBox.Show(string text, string caption, MessageBoxButtons buttons); 
 //     顯示具有指定文本、標題和按鈕的消息框。 
 // 參數: 
 //   text:      要在消息框中顯示的文本。 
 //   caption:     要在消息框的標題欄中顯示的文本。 
 //   buttons:     System.Windows.Forms.MessageBoxButtons 值之一,可指定在消息框中顯示哪些按鈕。 
 // 返回結果:     System.Windows.Forms.DialogResult 值之一。 
 // 異常:     
 //System.ComponentModel.InvalidEnumArgumentException:     指定的 buttons 參數不是 System.Windows.Forms.MessageBoxButtons 的成員。 
 //   System.InvalidOperationException:     試圖在運行模式不是用戶交互模式的進程中顯示 System.Windows.Forms.MessageBox。這是由 System.Windows.Forms.SystemInformation.UserInteractive 屬性指定的。 
   
 4.     4個參數。 
 MessageBox.Show(string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon); 
 //     顯示具有指定文本、標題、按鈕和圖標的消息框。 
 // 參數: 
 //   text:     要在消息框中顯示的文本。 
 //   caption:     要在消息框的標題欄中顯示的文本。 
 //   buttons:     System.Windows.Forms.MessageBoxButtons 值之一,可指定在消息框中顯示哪些按鈕。 
 //   icon:     System.Windows.Forms.MessageBoxIcon 值之一,它指定在消息框中顯示哪個圖標。 
 // 返回結果:     System.Windows.Forms.DialogResult 值之一。 
 // 異常: 
 //   System.ComponentModel.InvalidEnumArgumentException:     指定的 buttons 參數不是 System.Windows.Forms.MessageBoxButtons 的成員。- 或 - 指定的 icon24參數不是 System.Windows.Forms.MessageBoxIcon 的成員。 
 //   System.InvalidOperationException:     試圖在運行模式不是用戶交互模式的進程中顯示 System.Windows.Forms.MessageBox。這是由 System.Windows.Forms.SystemInformation.UserInteractive屬性指定的。
          
 5.     5個參數。  
 MessageBox.Show(string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon, MessageBoxDefaultButton defaultButton); 
 //     顯示具有指定文本、標題、按鈕、圖標和默認按鈕的消息框。 
 // 參數: 
 //   text:      要在消息框中顯示的文本。 
 //   caption:     要在消息框的標題欄中顯示的文本。 
 //   buttons:     System.Windows.Forms.MessageBoxButtons 值之一,可指定在消息框中顯示哪些按鈕。 
 //   icon:     System.Windows.Forms.MessageBoxIcon 值之一,它指定在消息框中顯示哪個圖標。 
 //   default Button:     System.Windows.Forms.MessageBoxDefaultButton 值之一,可指定消息框中的默認按鈕。 
 // 返回結果:     System.Windows.Forms.DialogResult 值之一。 
 // 異常:  
 //   System.ComponentModel.InvalidEnumArgumentException:     buttons 不是 System.Windows.Forms.MessageBoxButtons 的成員。- 或 - icon 不是 System.Windows.Forms.MessageBoxIcon的成員。- 或 - defaultButton 不是 System.Windows.Forms.MessageBoxDefaultButton 的成員。 
 //   System.InvalidOperationException:     試圖在運行模式不是用戶交互模式的進程中顯示 System.Windows.Forms.MessageBox。這是由 System.Windows.Forms.SystemInformation.UserInteractive屬性指定的。 
   
 6.     6個參數。 
 MessageBox.Show(string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon,MessageBoxDefaultButton defaultButton, MessageBoxOptions options); 
 //     顯示具有指定文本、標題、按鈕、圖標、默認按鈕和選項的消息框。 
 // 參數: 
 //   text:      要在消息框中顯示的文本。 
 //   caption:     要在消息框的標題欄中顯示的文本 
 //   buttons:    System.Windows.Forms.MessageBoxButtons 值之一,可指定在消息框中顯示哪些按鈕。 
 //   icon:     System.Windows.Forms.MessageBoxIcon 值之一,它指定在消息框中顯示哪個圖標。 
 //   defaultButton:     System.Windows.Forms.MessageBoxDefaultButton 值之一,可指定消息框中的默認按鈕。 
 //   options:     System.Windows.Forms.MessageBoxOptions 值之一,可指定將對消息框使用哪些顯示和關聯選項。若要使用默認值,請傳入0。 
 // 返回結果:     System.Windows.Forms.DialogResult 值之一。 
 // 異常: 
 //   System.ComponentModel.InvalidEnumArgumentException:     buttons 不是 System.Windows.Forms.MessageBoxButtons 的成員。- 或 - icon 不是 System.Windows.Forms.MessageBoxIcon的成員。- 或 - 指定的 defaultButton 不是 System.Windows.Forms.MessageBoxDefaultButton的成員。 
 //   System.InvalidOperationException:     試圖在運行模式不是用戶交互模式的進程中顯示 System.Windows.Forms.MessageBox。這是由System.Windows.Forms.SystemInformation.UserInteractive屬性指定的。 
 //   System.ArgumentException:     options 同時指定了System.Windows.Forms.MessageBoxOptions.DefaultDesktopOnly 和System.Windows.Forms.MessageBoxOptions.ServiceNotification。- 或 - buttons42指定了無效的System.Windows.Forms.MessageBoxButtons 組合。
   
 7.     7個參數一。 
 MessageBox.Show(string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon,MessageBoxDefaultButton defaultButton, MessageBoxOptions options, bool displayHelpButton); 
 //     顯示一個具有指定文本、標題、按鈕、圖標、默認按鈕、選項和“幫助”按鈕的消息框。 
 // 參數: 
 //   text:      要在消息框中顯示的文本。 
 //   caption:     要在消息框的標題欄中顯示的文本。 
 //   buttons:     System.Windows.Forms.MessageBoxButtons 值之一,可指定在消息框中顯示哪些按鈕。 
 //   icon:     System.Windows.Forms.MessageBoxIcon 值之一,它指定在消息框中顯示哪個圖標。 
 //   defaultButton:     System.Windows.Forms.MessageBoxDefaultButton 值之一,可指定消息框中的默認按鈕。 
 //   options:     System.Windows.Forms.MessageBoxOptions 值之一,可指定將對消息框使用哪些顯示和關聯選項。若要使用默認值,請傳入0。 
 //   helpButton:     如果顯示“幫助”按鈕,則爲 true;否則爲 false。默認爲 false。 
 // 返回結果:     System.Windows.Forms.DialogResult 值之一。 
 // 異常:34         
 //   System.ComponentModel.InvalidEnumArgumentException:     buttons 不是 System.Windows.Forms.MessageBoxButtons 的成員。- 或 - icon 不是 System.Windows.Forms.MessageBoxIcon的成員。- 或 - 指定的 defaultButton 不是 System.Windows.Forms.MessageBoxDefaultButton的成員。 
 //   System.InvalidOperationException:     試圖在運行模式不是用戶交互模式的進程中顯示 System.Windows.Forms.MessageBox。這是由System.Windows.Forms.SystemInformation.UserInteractive屬性指定的。 
 //   System.ArgumentException:     options 同時指定了System.Windows.Forms.MessageBoxOptions.DefaultDesktopOnly 和System.Windows.Forms.MessageBoxOptions.ServiceNotification。- 或 - buttons指定了無效的 System.Windows.Forms.MessageBoxButtons 組合。 
       
 8.  7個參數二 
 MessageBox.Show(string text, string caption, MessageBoxButtons buttons,MessageBoxIcon icon, MessageBoxDefaultButton defaultButton,MessageBoxOptions options, string helpFilePath); 
 //     使用指定的幫助文件顯示一個具有指定文本、標題、按鈕、圖標、默認按鈕、選項和“幫助”按鈕的消息框。 
 // 參數: 
 //   text:     要在消息框中顯示的文本。 
 //   caption:     要在消息框的標題欄中顯示的文本。 
 //   buttons:     System.Windows.Forms.MessageBoxButtons 值之一,可指定在消息框中顯示哪些按鈕。 
 //   icon:     System.Windows.Forms.MessageBoxIcon 值之一,它指定在消息框中顯示哪個圖標。 
 //   defaultButton:     System.Windows.Forms.MessageBoxDefaultButton 值之一,可指定消息框中的默認按鈕。 
 //   options:     System.Windows.Forms.MessageBoxOptions 值之一,可指定將對消息框使用哪些顯示和關聯選項。若要使用默認值,請傳入0。 
 //   helpFilePath:     用戶單擊“幫助”按鈕時顯示的“幫助”文件的路徑和名稱。 
 // 返回結果:     System.Windows.Forms.DialogResult 值之一。 
 // 異常: 
 //   System.ComponentModel.InvalidEnumArgumentException:     buttons 不是 System.Windows.Forms.MessageBoxButtons 的成員。- 或 - icon 不是System.Windows.Forms.MessageBoxIcon的成員。- 或 - 指定的 defaultButton 不是System.Windows.Forms.MessageBoxDefaultButton的成員。 
 //   System.InvalidOperationException:     試圖在運行模式不是用戶交互模式的進程中顯示System.Windows.Forms.MessageBox。這是由System.Windows.Forms.SystemInformation.UserInteractive屬性指定的。 
 //   System.ArgumentException:     options 同時指定了System.Windows.Forms.MessageBoxOptions.DefaultDesktopOnly 和System.Windows.Forms.MessageBoxOptions.ServiceNotification。- 或 - buttons指定了無效的 System.Windows.Forms.MessageBoxButtons 組合。
 
   
 
 
 
 
 
 下面對C#中的預編譯指令進行介紹:
 1.#define和#undef
 用法:
       #define DEBUG 
       #undef DEBUG
       #define告訴編譯器,我定義了一個DEBUG的一個符號,他類似一個變量,但是它沒有具體的值,可以將它看爲一個符號而已。#undef就是刪除這個符號的定義。如果符號DEBUG沒定義過,則#undef不起作用,否則#define不起作用。二者都必須放在源代碼之前。二者的順序看代碼的順序:
       #define DEBUG 
      #undef  DEBUG
       這樣的話,DEBUG是沒有定義的,如果二者換個順序,編譯器就認爲DEBUG被定義了
 2.#if、#elif、#else、#endif
       這個告訴編譯器進行編譯代碼的流程控制。考慮下面代碼:
             #if DEBUG
                   Console.Write("debug");
             #elif RELEASE
                   Console.Write("release");
             #else
                   Console.Write("other");
             #endif
       以上代碼就是說如果定義了DEBUG則輸出debug,定義了RELEASE,則輸出realse,否則輸出other。如果定義了DEBUG和REALSE會怎麼樣呢?各位可以自己試一下。
 3.#warning、#error
       通過這兩個指定可以告訴編譯器,出一個警告還是錯誤信息。除了錯誤信息以後,編譯將停止。
       參考下面的代碼(C#在Debug狀態下自動定義DEBUG標誌,但Release狀態不會自動定義RELEASE標誌):
             #if DEBUG                         
                   #warning 現在是Debug狀態
             #elif RELEASE
                   #warning 現在是Release狀態
             #else
                   #error 並清楚什麼狀態
             #endif
 4.#region 和#endregion
       這個兩個用來組成代碼塊
 5.#line
       這個指令可以改變編譯器在警告和錯誤信息中顯示的文件名和行號信息,用#line default把行號恢復爲默認的行號。
       #line [ number ["file_name"] | default ]
       number
             要爲源代碼文件中後面的行指定的編號。
       "file_name"(可選)
             希望出現在編譯器輸出中的文件名。默認情況下,使用源代碼文件的實際名稱。文件名必須括在雙引號 ("") 中。
       default
             重置文件中的行編號。
       備註
             #line 可能由生成過程中的自動中間步驟使用。例如,如果中間步驟從原始的源代碼文件中移除行,但是您仍希望編譯器基於文件中的原始行號生成輸出,則可以移除行,然後用 #line 模擬原始行號。
       下面的示例說明如何報告與行號關聯的兩個警告。#line 200 指令迫使行號爲 200(儘管默認值爲 #7)。另一行 (#9) 作爲默認 #line 指令 的結果跟在通常序列後。
 示例1:
       // preprocessor_line.cs
       public class MyClass2
       {
             public static void Main()
             {
                   #line 200
                   int i;       // line 200
                   #line default
                   char c;  // line 9
             }
       }
 示例2:
       下面的示例說明調試器如何忽略代碼中的隱藏行。運行此示例時,它將顯示三行文本。但是,當設置如示例所示的斷點並按 F10 鍵逐句通過代碼時,您將看到調試器忽略了隱藏行。另請注意,即使在隱藏行上設置斷點,調試器仍會忽略它。
      // preprocessor_linehidden.cs
       using System;
       class MyClass
       {
             public static void Main()
             {
                   Console.WriteLine("Normal line #1.");  // Set a break point here.
                   #line hidden
                   Console.WriteLine("Hidden line.");
                   #line default
                   Console.WriteLine("Normal line #2.");
             }
       }
 6.#pragma
        可以抑制或恢復指定的編譯警告。與命令行選項不同,#pragma指令可以在類和方法上執行,對抑制什麼警告和抑制的時間進行更精細的控制。
        #pragma warning disable 169
        public class Aclass
        {
                int nFiled;
        }
        #pragma warning restore 169
 

 

 

 

轉自:http://www.cnblogs.com/bq-blog/archive/2012/07/27/2611810.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章