欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C#中的Dialog對(duì)話框

 更新時(shí)間:2022年05月13日 15:04:08   作者:springsnow  
這篇文章介紹了C#中的Dialog對(duì)話框,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

一、MessageBox彈出框

MessageBox.Show(<字符串> Text, <字符串> Title, <整型> nType,MessageBoxIcon);

  • 第一個(gè)參數(shù)是 String 類型,表示提示框里面的 內(nèi)容;
  • 第二個(gè)參數(shù)是String 類型,表示提示框的 標(biāo)題;
  • 第三個(gè)參數(shù)是整數(shù)類型,表示消息框的類型  ,一般的都使用系統(tǒng)提供的幾種類型;
  • 第四個(gè)參數(shù)是提示框的 圖標(biāo),比如說警告、提示、問題等等。

MessageBoxButtons類型:

  • AbortRetryIgnore: 消息框包含“中止”、“重試”和“忽略”按鈕。
  • OK :消息框包含“確定”按鈕。(默認(rèn))
  • OKCancel : 消息框包含“確定”和“取消”按鈕。(上例所示)
  • RetryCancel  :消息框包含“重試”和“取消”按鈕。
  • YesNo :消息框包含“是”和“否”按鈕。
  • YesNoCancel :消息框包含“是”、“否”和“取消”按鈕

MessageBoxIcon圖標(biāo)樣式:

  • MessageBoxIcon.Question
    MessageBoxIcon.Asterisk
    MessageBoxIcon.Information
    MessageBoxIcon.Error
    MessageBoxIcon.Stop
    MessageBoxIcon.Hand
    MessageBoxIcon.Exclamation
    MessageBoxIcon.Warning

舉例

    MessageBox.Show("用戶名或者密碼不能為空");
    MessageBox.Show("用戶名或者密碼不能為空","登錄提示");
    MessageBox.Show("用戶名或者密碼不能為空","登錄提示",MessageBoxButtons.OKCancel);
    MessageBox.Show("用戶名或者密碼不能為空","登錄提示",MessageBoxButtons.OKCancel,MessageBoxIcon.Exclamation);

      二、WinForm自帶對(duì)話框

      除PrintPreviewDialog外,所有的對(duì)話框都繼承于抽象類CommonDialog。

      CommonDialog的繼承結(jié)構(gòu)

      • 1、文件對(duì)話框(FileDialog) 它又常用到兩個(gè):
        • 打開文件對(duì)話框(OpenFileDialog)
        • 保存文件對(duì)話(SaveFileDialog)
      • 2、字體對(duì)話框(FontDialog)
      • 3、顏色對(duì)話框(ColorDialog)
      • 4、打印預(yù)瀏對(duì)話框(PrintPreviewDialog)
      • 5、頁面設(shè)置(PrintDialog)
      • 6、打印對(duì)話框(PrintDialog)

      CommonDialog的方法

      • OnHelpRequest(EventArgs):    引發(fā) HelpRequest 事件。
      • Reset():在派生類中被重寫時(shí),將通用對(duì)話框的屬性重置為默認(rèn)值。
      • ShowDialog():    用默認(rèn)的所有者運(yùn)行通用對(duì)話框。
      • ShowDialog(IWin32Window) :   運(yùn)行具有指定所有者的通用對(duì)話框。

      1、打開文件對(duì)話框(OpenFileDialog)

      基本屬性

      • InitialDirectory 對(duì)話框的初始目錄
      • Filter 要在對(duì)話框中顯示的文件篩選器,例如,"文本文件(*.txt)|*.txt|所有文件(*.*)||*.*"
      • FilterIndex 在對(duì)話框中選擇的文件篩選器的索引,如果選第一項(xiàng)就設(shè)為1
      • RestoreDirectory 控制對(duì)話框在關(guān)閉之前是否恢復(fù)當(dāng)前目錄
      • FileName 獲取或設(shè)置一個(gè)包含在文件對(duì)話框中選定的文件名的字符串。
      • Title 將顯示在對(duì)話框標(biāo)題欄中的字符
      • AddExtension 是否自動(dòng)添加默認(rèn)擴(kuò)展名
      • CheckPathExists 在對(duì)話框返回之前,檢查指定路徑是否存在
      • DefaultExt 默認(rèn)擴(kuò)展名
      • DereferenceLinks 在從對(duì)話框返回前是否取消引用快捷方式
      • ShowHelp 啟用"幫助"按鈕
      • ValiDateNames 控制對(duì)話框檢查文件名中是否不含有無效的字符或序列

      示例

      System.Windows.Forms.OpenFileDialog dlg = new System.Windows.Forms.OpenFileDialog();
      dlg.Title = "打開文件";
      dlg.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Templates);
      dlg.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
      dlg.FilterIndex = 2;
      dlg.RestoreDirectory = true;
      if (dlg.ShowDialog() == DialogResult.OK)
      {
          if (dlg.FileName != "") //如果dlg.Multiselect=true;可以是dlg.FileNames
          {
              MessageBox.Show("你選擇了" + dlg.FileName);
          }
      }

      2、保存文件對(duì)話框(SaveFileDialog)

      屬性

      • Filter 要在對(duì)話框中顯示的文件篩選器,例如,"文本文件(*.txt)|*.txt|所有文件(*.*)|*.*"
      • FilterIndex 在對(duì)話框中選擇的文件篩選器的索引,如果選第一項(xiàng)就設(shè)為1
      • RestoreDirectory 控制對(duì)話框在關(guān)閉之前是否恢復(fù)當(dāng)前目錄
      • AddExtension 是否自動(dòng)添加默認(rèn)擴(kuò)展名
      • CheckFileExists 獲取或設(shè)置一個(gè)值,該值指示如果用戶指定不存在的文件名,對(duì)話框是否顯示警告。
      • CheckPathExists 在對(duì)話框返回之前,檢查指定路徑是否存在
      • Container 控制在將要?jiǎng)?chuàng)建文件時(shí),是否提示用戶。只有在ValidateNames為真值時(shí),才適用。
      • DefaultExt 缺省擴(kuò)展名
      • DereferenceLinks 在從對(duì)話框返回前是否取消引用快捷方式
      • FileName 獲取或設(shè)置一個(gè)包含在文件對(duì)話框中選定的文件名的字符串。
      • InitialDirector 對(duì)話框的初始目錄
      • OverwritePrompt 控制在將要在改寫現(xiàn)在文件時(shí)是否提示用戶,只有在ValidateNames為真值時(shí),才適用
      • ShowHelp 啟用"幫助"按鈕
      • Title 將顯示在對(duì)話框標(biāo)題欄中的字符
      • ValidateNames 控制對(duì)話框檢查文件名中是否不含有無效的字符或序列

      示例

      System.IO.Stream stream;
      System.Windows.Forms.SaveFileDialog saveFileDialog1 = new System.Windows.Forms.SaveFileDialog();
      saveFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
      saveFileDialog1.FilterIndex = 2;
      saveFileDialog1.RestoreDirectory = true;
      if (saveFileDialog1.ShowDialog() == DialogResult.OK)
      {
          if ((stream = saveFileDialog1.OpenFile()) != null)
          {
              // Code to write the stream goes here.
              stream.Close();
           }
      }

      3、打印預(yù)覽對(duì)話框和打印對(duì)話框

      1、打印預(yù)覽對(duì)話框(PrintPreviewDialog)屬性:

      • AutoScrollMargin 獲取或設(shè)置自動(dòng)滾動(dòng)邊距的大小。

      • AutoScrollMinSize 獲取或設(shè)置自動(dòng)滾動(dòng)的最小尺寸。

      • DialogResult 獲取或設(shè)置窗體的對(duì)話框結(jié)果。

      • Document 獲取或設(shè)置要預(yù)覽的文檔。

      • HelpButton 獲取或設(shè)置一個(gè)值,該值指示是否應(yīng)在窗體的標(biāo)題框中顯示“幫助”按鈕。

      2、打印對(duì)話框(PrintDialog)屬性:

      • AllowPrintToFile 禁止或使用"打印到文件"復(fù)選框

      • AllowSelection 禁止或使用"選定內(nèi)容"單選框

      • AllowSomePages 禁止或使用"頁"單選按鈕

      • Document 從中獲取打印機(jī)設(shè)置的PrintDocument

      • PrintToFile 打印到文件"復(fù)選框是否選中
      • ShowHelp 控制是否顯示"幫助"按鈕
      • ShowNetWork 控制是否顯示"網(wǎng)絡(luò)"按鈕

      3、示例:

      private void printPreviewButton_Click(object sender, EventArgs e)
      {
          StreamReader streamToPrint = new StreamReader("PrintMe.Txt");
          try
          {
              PrintDocument pd = new PrintDocument(streamToPrint); //假定為默認(rèn)打印機(jī)
              if (storedPageSettings != null)
              {
                  pd.DefaultPageSettings = storedPageSettings;
              }
              PrintPreviewDialog dlg = new PrintPreviewDialog();
              dlg.Document = pd;
              dlg.ShowDialog();
          }
          finally
          {
              streamToPrint.Close();
          }
      
      }
      
      private void printButton_Click(object sender, EventArgs e)
      {
      
          StreamReader streamToPrint = new StreamReader("PrintMe.Txt");
          try
          {
              PrintDocument pd = new PrintDocument(streamToPrint);
              PrintDialog dlg = new PrintDialog();
              dlg.Document = pd;
              DialogResult result = dlg.ShowDialog();
              if (result == DialogResult.OK)
                  pd.Print();
      
          }
          finally
          {
              streamToPrint.Close();
          }
      
      }

      三、自定義對(duì)話框

      1 模態(tài)窗口: ShowDialog():

      打開模態(tài)窗口后,只要不關(guān)閉該窗口,鼠標(biāo)焦點(diǎn)或者光標(biāo)就會(huì)一直停留在該窗口上。只有關(guān)閉該窗口后,調(diào)用窗口才能繼續(xù)。

      模態(tài)窗口關(guān)閉后,仍可以讀取模態(tài)窗口中的信息,如窗口的返回狀態(tài)等,以后還可以使用ShowDialog()使其可見。

      2 非模態(tài)窗口: Show():

      打開非模態(tài)窗口后,仍可以操作調(diào)用窗口。后面的代碼立即執(zhí)行。

      關(guān)閉非模態(tài)窗口,該窗口將不復(fù)存在,會(huì)釋放窗口的所有資源,所以無法得到該窗口的任何信息。常用Hide()方法(等效于Visible=false)然后調(diào)用Show()方法使其可見。

      3、對(duì)話框窗體:Form2

      public Form1(string para)//獲取參數(shù)
      {
          InitializeComponent();
      
          this.StartPosition = FormStartPosition.CenterParent;//啟動(dòng)位置,父窗口中央
          this.MaximizeBox = false;
          this.MinimizeBox = false;
          this.ShowIcon = false;//不顯示圖標(biāo)
          this.ControlBox = false;
          this.ShowInTaskbar = false;
          this.FormBorderStyle = FormBorderStyle.FixedDialog;//邊框樣式為固定對(duì)話框
          this.btnOK.DialogResult = DialogResult.OK;//"Enter"為OK按鈕
          this.btnCancel.DialogResult = DialogResult.Cancel;//“ESC”為Cancel按鈕
          this.textBox1.Text = para;
      }
      
      public string ReturnText //定義一個(gè)公共屬性,供調(diào)用窗口Form1使用
      {
          get { return this.textBox1.Text + "b"; }
      }
      
      
      private void Form1_Load(object sender, EventArgs e)
      {
          if (this.Owner.Name != "Form1")//Owner為調(diào)用窗體,即調(diào)用改對(duì)話框的窗體
              MessageBox.Show("非法調(diào)用");
      }
      
      
      private void BtnOK_Click(object sender, EventArgs e)
      {
          if (this.textBox1.Text.Trim().Length == 0)
              MessageBox.Show("無輸入");
          this.textBox1.Focus();
          this.DialogResult = DialogResult.None;//阻止隱藏對(duì)話框,對(duì)話框不消失
      }

      4、主窗體Form1:

      Form f2 = new Form2("a");
      
      if (f2.ShowDialog(this) == DialogResult.OK)//對(duì)應(yīng)Form2中的Owner,this為給對(duì)話框窗體傳值
          this.textBox1.Text = f2.ReturnText;
      f2.Close();
      f2.Dispose();

      到此這篇關(guān)于C#對(duì)話框Dialog的文章就介紹到這了。希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

      相關(guān)文章

      最新評(píng)論