原文: http://zetcode.com/gui/csharpwinforms/dialogs/

在 Mono Winforms 教程的这一部分中,我们将讨论对话框。

对话框窗口或对话框是大多数现代 GUI 应用必不可少的部分。 对话被定义为两个或更多人之间的对话。 在计算机应用中,对话框是一个窗口,用于与应用“对话”。 对话框用于输入数据,修改数据,更改应用设置等。对话框是用户与计算机程序之间进行通信的重要手段。

基本上有两种类型的对话框。 预定义对话框和自定义对话框。

FolderBrowserDialog

此对话框提示用户选择一个文件夹。

folderbrowserdialog.cs

  1. using System;
  2. using System.IO;
  3. using System.Drawing;
  4. using System.Windows.Forms;
  5. class MForm : Form {
  6. private ToolBar toolbar;
  7. private ToolBarButton open;
  8. private StatusBar statusbar;
  9. public MForm() {
  10. Text = "FolderBrowserDialog";
  11. toolbar = new ToolBar();
  12. open = new ToolBarButton();
  13. statusbar = new StatusBar();
  14. statusbar.Parent = this;
  15. toolbar.Buttons.Add(open);
  16. toolbar.ButtonClick += new ToolBarButtonClickEventHandler(OnClicked);
  17. Controls.Add(toolbar);
  18. CenterToScreen();
  19. }
  20. void OnClicked(object sender, ToolBarButtonClickEventArgs e) {
  21. FolderBrowserDialog dialog = new FolderBrowserDialog();
  22. if (dialog.ShowDialog(this) == DialogResult.OK) {
  23. statusbar.Text = dialog.SelectedPath;
  24. }
  25. }
  26. }
  27. class MApplication {
  28. public static void Main() {
  29. Application.Run(new MForm());
  30. }
  31. }

我们有一个工具栏和一个工具栏按钮。 点击按钮,FolderBrowserDialog出现在屏幕上。 所选文件夹的名称显示在状态栏中。

  1. FolderBrowserDialog dialog = new FolderBrowserDialog();

FolderBrowserDialog已创建。

  1. if (dialog.ShowDialog(this) == DialogResult.OK) {
  2. statusbar.Text = dialog.SelectedPath;
  3. }

ShowDialog()方法在屏幕上显示对话框。 如果单击对话框的“确定”按钮,则所选的目录路径将显示在状态栏上。

对话框 - 图1

图:FolderBrowserDialog

ColorDialog

此对话框显示可用的颜色以及使用户能够定义自定义颜色的控件。

colordialog.cs

  1. using System;
  2. using System.Drawing;
  3. using System.Windows.Forms;
  4. class MForm : Form {
  5. private ToolBar toolbar;
  6. private ToolBarButton open;
  7. private Color color;
  8. private int rectWidth = 100;
  9. private int rectHeight = 100;
  10. private Rectangle r;
  11. public MForm() {
  12. Text = "ColorDialog";
  13. color = Color.Blue;
  14. toolbar = new ToolBar();
  15. open = new ToolBarButton();
  16. toolbar.Buttons.Add(open);
  17. toolbar.ButtonClick += new ToolBarButtonClickEventHandler(OnClicked);
  18. LocateRect();
  19. SetStyle (ControlStyles.ResizeRedraw, true);
  20. Controls.Add(toolbar);
  21. Paint += new PaintEventHandler(OnPaint);
  22. CenterToScreen();
  23. }
  24. void OnPaint(object sender, PaintEventArgs e)
  25. {
  26. Graphics g = e.Graphics;
  27. LocateRect();
  28. SolidBrush brush = new SolidBrush(color);
  29. g.FillRectangle(brush, r);
  30. }
  31. void OnClicked(object sender, ToolBarButtonClickEventArgs e) {
  32. ColorDialog dialog = new ColorDialog();
  33. if (dialog.ShowDialog(this) == DialogResult.OK) {
  34. color = dialog.Color;
  35. Invalidate();
  36. }
  37. }
  38. void LocateRect() {
  39. int x = (ClientSize.Width - rectWidth) / 2;
  40. int y = (ClientSize.Height - rectHeight) / 2;
  41. r = new Rectangle(x, y, rectWidth, rectHeight);
  42. }
  43. }
  44. class MApplication {
  45. public static void Main() {
  46. Application.Run(new MForm());
  47. }
  48. }

在此代码示例中,我们使用ColorDialog为位于窗体控件中间的矩形选择颜色。

  1. color = Color.Blue;

开始时,矩形的颜色是蓝色。 我们使用color变量来确定矩形的颜色。

  1. ColorDialog dialog = new ColorDialog();

ColorDialog已创建。

  1. if (dialog.ShowDialog(this) == DialogResult.OK) {
  2. color = dialog.Color;
  3. Invalidate();
  4. }

该代码显示颜色对话框。 如果单击“确定”按钮,则将获得选定的颜色并调用Invalidate()方法。 该方法会使控件的整个表面无效,并使控件重画。 结果是用新的颜色值绘制了矩形。

对话框 - 图2

图:ColorDialog

FontDialog

FontDialog用于选择字体。

fontdialog.cs

  1. using System;
  2. using System.Drawing;
  3. using System.Windows.Forms;
  4. class MForm : Form {
  5. private Label text;
  6. private ToolBar toolbar;
  7. private ToolBarButton open;
  8. public MForm() {
  9. Text = "FontDialog";
  10. text = new Label();
  11. text.Parent = this;
  12. text.Text = "Winforms tutorial";
  13. LocateText();
  14. toolbar = new ToolBar();
  15. toolbar.Parent = this;
  16. open = new ToolBarButton();
  17. toolbar.Buttons.Add(open);
  18. toolbar.ButtonClick += new ToolBarButtonClickEventHandler(OnClicked);
  19. text.AutoSize = true;
  20. Resize += new EventHandler(OnResize);
  21. CenterToScreen();
  22. }
  23. void OnResize(object sender, EventArgs e){
  24. LocateText();
  25. }
  26. void OnClicked(object sender, ToolBarButtonClickEventArgs e) {
  27. FontDialog dialog = new FontDialog();
  28. if (dialog.ShowDialog(this) == DialogResult.OK) {
  29. text.Font = dialog.Font;
  30. LocateText();
  31. }
  32. }
  33. void LocateText() {
  34. text.Top = (this.ClientSize.Height - text.Height) / 2;
  35. text.Left = (this.ClientSize.Width - text.Width) / 2;
  36. }
  37. }
  38. class MApplication {
  39. public static void Main() {
  40. Application.Run(new MForm());
  41. }
  42. }

我们在表单控件的中间绘制一些文本。 我们使用字体对话框更改此文本的字体。

  1. FontDialog dialog = new FontDialog();

创建了FontDialog

  1. if (dialog.ShowDialog(this) == DialogResult.OK) {
  2. text.Font = dialog.Font;
  3. LocateText();
  4. }

单击“确定”按钮时,将为Label控件设置新选择的字体。 由于文本的大小会随着字体的变化而变化,因此我们必须调用LocateText()方法,该方法将文本定位在表单控件的中间。

对话框 - 图3

图:FontDialog

OpenDialog

此对话框用于打开文件。

opendialog.cs

  1. using System;
  2. using System.IO;
  3. using System.Drawing;
  4. using System.Windows.Forms;
  5. class MForm : Form {
  6. private ToolBar toolbar;
  7. private ToolBarButton open;
  8. private TextBox textbox;
  9. public MForm() {
  10. Text = "OpenFileDialog";
  11. toolbar = new ToolBar();
  12. open = new ToolBarButton();
  13. textbox = new TextBox();
  14. textbox.Multiline = true;
  15. textbox.ScrollBars = ScrollBars.Both;
  16. textbox.WordWrap = false;
  17. textbox.Parent = this;
  18. textbox.Dock = DockStyle.Fill;
  19. toolbar.Buttons.Add(open);
  20. toolbar.ButtonClick += new ToolBarButtonClickEventHandler(OnClicked);
  21. Controls.Add(toolbar);
  22. Controls.Add(textbox);
  23. CenterToScreen();
  24. }
  25. void OnClicked(object sender, ToolBarButtonClickEventArgs e) {
  26. OpenFileDialog dialog = new OpenFileDialog();
  27. dialog.Filter = "C# files (*.cs)|*.cs";
  28. if (dialog.ShowDialog(this) == DialogResult.OK) {
  29. StreamReader reader = new StreamReader(dialog.FileName);
  30. string data = reader.ReadToEnd();
  31. reader.Close();
  32. textbox.Text = data;
  33. }
  34. }
  35. }
  36. class MApplication {
  37. public static void Main() {
  38. Application.Run(new MForm());
  39. }
  40. }

我们使用OpenDialog控件打开 C# 源文件。 我们有一个TextBox控件,用于显示文件。

  1. OpenFileDialog dialog = new OpenFileDialog();

OpenDialog已创建。

  1. dialog.Filter = "C# files (*.cs)|*.cs";

我们将Filter属性设置为 C# 源文件。 此对话框实例只能选择 C# 文件。

  1. if (dialog.ShowDialog(this) == DialogResult.OK) {
  2. StreamReader reader = new StreamReader(dialog.FileName);
  3. string data = reader.ReadToEnd();
  4. reader.Close();
  5. textbox.Text = data;
  6. }

单击确定后,我们读取所选文件的内容并将其放入TextBox控件。

对话框 - 图4

图:OpenDialog

在 Mono Winforms 教程的这一部分中,我们显示了各种对话框。