<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                # 對話框 > 原文: [http://zetcode.com/gui/csharpwinforms/dialogs/](http://zetcode.com/gui/csharpwinforms/dialogs/) 在 Mono Winforms 教程的這一部分中,我們將討論對話框。 對話框窗口或對話框是大多數現代 GUI 應用必不可少的部分。 對話被定義為兩個或更多人之間的對話。 在計算機應用中,對話框是一個窗口,用于與應用“對話”。 對話框用于輸入數據,修改數據,更改應用設置等。對話框是用戶與計算機程序之間進行通信的重要手段。 基本上有兩種類型的對話框。 預定義對話框和自定義對話框。 ## `FolderBrowserDialog` 此對話框提示用戶選擇一個文件夾。 `folderbrowserdialog.cs` ```cs using System; using System.IO; using System.Drawing; using System.Windows.Forms; class MForm : Form { private ToolBar toolbar; private ToolBarButton open; private StatusBar statusbar; public MForm() { Text = "FolderBrowserDialog"; toolbar = new ToolBar(); open = new ToolBarButton(); statusbar = new StatusBar(); statusbar.Parent = this; toolbar.Buttons.Add(open); toolbar.ButtonClick += new ToolBarButtonClickEventHandler(OnClicked); Controls.Add(toolbar); CenterToScreen(); } void OnClicked(object sender, ToolBarButtonClickEventArgs e) { FolderBrowserDialog dialog = new FolderBrowserDialog(); if (dialog.ShowDialog(this) == DialogResult.OK) { statusbar.Text = dialog.SelectedPath; } } } class MApplication { public static void Main() { Application.Run(new MForm()); } } ``` 我們有一個工具欄和一個工具欄按鈕。 點擊按鈕,`FolderBrowserDialog`出現在屏幕上。 所選文件夾的名稱顯示在狀態欄中。 ```cs FolderBrowserDialog dialog = new FolderBrowserDialog(); ``` `FolderBrowserDialog`已創建。 ```cs if (dialog.ShowDialog(this) == DialogResult.OK) { statusbar.Text = dialog.SelectedPath; } ``` `ShowDialog()`方法在屏幕上顯示對話框。 如果單擊對話框的“確定”按鈕,則所選的目錄路徑將顯示在狀態欄上。 ![FolderBrowserDialog](https://img.kancloud.cn/c8/2b/c82ba68cff4c1aa5c9a248774625db9a_330x352.jpg) 圖:`FolderBrowserDialog` ## `ColorDialog` 此對話框顯示可用的顏色以及使用戶能夠定義自定義顏色的控件。 `colordialog.cs` ```cs using System; using System.Drawing; using System.Windows.Forms; class MForm : Form { private ToolBar toolbar; private ToolBarButton open; private Color color; private int rectWidth = 100; private int rectHeight = 100; private Rectangle r; public MForm() { Text = "ColorDialog"; color = Color.Blue; toolbar = new ToolBar(); open = new ToolBarButton(); toolbar.Buttons.Add(open); toolbar.ButtonClick += new ToolBarButtonClickEventHandler(OnClicked); LocateRect(); SetStyle (ControlStyles.ResizeRedraw, true); Controls.Add(toolbar); Paint += new PaintEventHandler(OnPaint); CenterToScreen(); } void OnPaint(object sender, PaintEventArgs e) { Graphics g = e.Graphics; LocateRect(); SolidBrush brush = new SolidBrush(color); g.FillRectangle(brush, r); } void OnClicked(object sender, ToolBarButtonClickEventArgs e) { ColorDialog dialog = new ColorDialog(); if (dialog.ShowDialog(this) == DialogResult.OK) { color = dialog.Color; Invalidate(); } } void LocateRect() { int x = (ClientSize.Width - rectWidth) / 2; int y = (ClientSize.Height - rectHeight) / 2; r = new Rectangle(x, y, rectWidth, rectHeight); } } class MApplication { public static void Main() { Application.Run(new MForm()); } } ``` 在此代碼示例中,我們使用`ColorDialog`為位于窗體控件中間的矩形選擇顏色。 ```cs color = Color.Blue; ``` 開始時,矩形的顏色是藍色。 我們使用`color`變量來確定矩形的顏色。 ```cs ColorDialog dialog = new ColorDialog(); ``` `ColorDialog`已創建。 ```cs if (dialog.ShowDialog(this) == DialogResult.OK) { color = dialog.Color; Invalidate(); } ``` 該代碼顯示顏色對話框。 如果單擊“確定”按鈕,則將獲得選定的顏色并調用`Invalidate()`方法。 該方法會使控件的整個表面無效,并使控件重畫。 結果是用新的顏色值繪制了矩形。 ![ColorDialog](https://img.kancloud.cn/00/c1/00c1e3f4c5701e287054e22160ae1d3c_450x335.jpg) 圖:`ColorDialog` ## `FontDialog` `FontDialog`用于選擇字體。 `fontdialog.cs` ```cs using System; using System.Drawing; using System.Windows.Forms; class MForm : Form { private Label text; private ToolBar toolbar; private ToolBarButton open; public MForm() { Text = "FontDialog"; text = new Label(); text.Parent = this; text.Text = "Winforms tutorial"; LocateText(); toolbar = new ToolBar(); toolbar.Parent = this; open = new ToolBarButton(); toolbar.Buttons.Add(open); toolbar.ButtonClick += new ToolBarButtonClickEventHandler(OnClicked); text.AutoSize = true; Resize += new EventHandler(OnResize); CenterToScreen(); } void OnResize(object sender, EventArgs e){ LocateText(); } void OnClicked(object sender, ToolBarButtonClickEventArgs e) { FontDialog dialog = new FontDialog(); if (dialog.ShowDialog(this) == DialogResult.OK) { text.Font = dialog.Font; LocateText(); } } void LocateText() { text.Top = (this.ClientSize.Height - text.Height) / 2; text.Left = (this.ClientSize.Width - text.Width) / 2; } } class MApplication { public static void Main() { Application.Run(new MForm()); } } ``` 我們在表單控件的中間繪制一些文本。 我們使用字體對話框更改此文本的字體。 ```cs FontDialog dialog = new FontDialog(); ``` 創建了`FontDialog`。 ```cs if (dialog.ShowDialog(this) == DialogResult.OK) { text.Font = dialog.Font; LocateText(); } ``` 單擊“確定”按鈕時,將為`Label`控件設置新選擇的字體。 由于文本的大小會隨著字體的變化而變化,因此我們必須調用`LocateText()`方法,該方法將文本定位在表單控件的中間。 ![FontDialog](https://img.kancloud.cn/b4/15/b415909c51a68917dbb47908ef1c6cdb_432x321.jpg) 圖:`FontDialog` ## `OpenDialog` 此對話框用于打開文件。 `opendialog.cs` ```cs using System; using System.IO; using System.Drawing; using System.Windows.Forms; class MForm : Form { private ToolBar toolbar; private ToolBarButton open; private TextBox textbox; public MForm() { Text = "OpenFileDialog"; toolbar = new ToolBar(); open = new ToolBarButton(); textbox = new TextBox(); textbox.Multiline = true; textbox.ScrollBars = ScrollBars.Both; textbox.WordWrap = false; textbox.Parent = this; textbox.Dock = DockStyle.Fill; toolbar.Buttons.Add(open); toolbar.ButtonClick += new ToolBarButtonClickEventHandler(OnClicked); Controls.Add(toolbar); Controls.Add(textbox); CenterToScreen(); } void OnClicked(object sender, ToolBarButtonClickEventArgs e) { OpenFileDialog dialog = new OpenFileDialog(); dialog.Filter = "C# files (*.cs)|*.cs"; if (dialog.ShowDialog(this) == DialogResult.OK) { StreamReader reader = new StreamReader(dialog.FileName); string data = reader.ReadToEnd(); reader.Close(); textbox.Text = data; } } } class MApplication { public static void Main() { Application.Run(new MForm()); } } ``` 我們使用`OpenDialog`控件打開 C# 源文件。 我們有一個`TextBox`控件,用于顯示文件。 ```cs OpenFileDialog dialog = new OpenFileDialog(); ``` `OpenDialog`已創建。 ```cs dialog.Filter = "C# files (*.cs)|*.cs"; ``` 我們將`Filter`屬性設置為 C# 源文件。 此對話框實例只能選擇 C# 文件。 ```cs if (dialog.ShowDialog(this) == DialogResult.OK) { StreamReader reader = new StreamReader(dialog.FileName); string data = reader.ReadToEnd(); reader.Close(); textbox.Text = data; } ``` 單擊確定后,我們讀取所選文件的內容并將其放入`TextBox`控件。 ![OpenDialog](https://img.kancloud.cn/cc/a4/cca418369d7c63d9a1176369c8f703bb_562x433.jpg) 圖:`OpenDialog` 在 Mono Winforms 教程的這一部分中,我們顯示了各種對話框。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看