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

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

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

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

FolderBrowserDialog

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

  1. ' ZetCode Mono Visual Basic Winforms tutorial
  2. '
  3. ' In this program we select a directory with a
  4. ' FolderBrowser dialog. The selected directory's
  5. ' name is shown in the statusbar.
  6. '
  7. ' author jan bodnar
  8. ' last modified May 2009
  9. ' website www.zetcode.com
  10. Imports System.Windows.Forms
  11. Imports System.Drawing
  12. Public Class WinVBApp
  13. Inherits Form
  14. Dim statusbar As StatusBar
  15. Public Sub New
  16. Me.Text = "FolderBrowserDialog"
  17. Me.Size = New Size(300, 250)
  18. Me.InitUI
  19. Me.CenterToScreen
  20. End Sub
  21. Private Sub InitUI
  22. Dim toolbar As New ToolBar
  23. Dim open As New ToolBarButton
  24. statusbar = New StatusBar
  25. statusbar.Parent = Me
  26. toolbar.Buttons.Add(open)
  27. Me.Controls.Add(toolbar)
  28. AddHandler toolbar.ButtonClick, AddressOf Me.OnClicked
  29. End Sub
  30. Private Sub OnClicked(ByVal sender As Object, _
  31. ByVal e As ToolBarButtonClickEventArgs)
  32. Dim dialog As New FolderBrowserDialog
  33. If dialog.ShowDialog(Me) = DialogResult.OK
  34. statusbar.Text = dialog.SelectedPath
  35. End If
  36. End Sub
  37. Public Shared Sub Main
  38. Application.Run(New WinVBApp)
  39. End Sub
  40. End Class

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

  1. Dim dialog As New FolderBrowserDialog

FolderBrowserDialog已创建。

  1. If dialog.ShowDialog(Me) = DialogResult.OK
  2. statusbar.Text = dialog.SelectedPath
  3. End If

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

对话框 - 图1

图:FolderBrowserDialog

ColorDialog

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

  1. ' ZetCode Mono Visual Basic Winforms tutorial
  2. '
  3. ' In this program we use the ColorDialog
  4. ' to change a color of a rectangle
  5. '
  6. ' author jan bodnar
  7. ' last modified May 2009
  8. ' website www.zetcode.com
  9. Imports System.Windows.Forms
  10. Imports System.Drawing
  11. Public Class WinVBApp
  12. Inherits Form
  13. Private col As Color
  14. Private Const rectWidth As Integer = 100
  15. Private Const rectHeight As Integer = 100
  16. Private Dim r As Rectangle
  17. Public Sub New
  18. Me.Text = "ColorDialog"
  19. Me.Size = New Size(300, 250)
  20. Me.InitUI
  21. Me.CenterToScreen
  22. End Sub
  23. Private Sub InitUI
  24. Dim tbar As New ToolBar
  25. Dim open As New ToolBarButton
  26. col = Color.Blue
  27. tbar.Buttons.Add(open)
  28. Me.LocateRect
  29. Me.SetStyle(ControlStyles.ResizeRedraw, True)
  30. Controls.Add(tbar)
  31. AddHandler Me.Paint, AddressOf Me.OnPaint
  32. AddHandler tbar.ButtonClick, AddressOf Me.OnClicked
  33. End Sub
  34. Private Sub OnPaint(ByVal sender As Object, ByVal e As PaintEventArgs)
  35. Dim g As Graphics = e.Graphics
  36. Me.LocateRect
  37. Dim brsh As New SolidBrush(col)
  38. g.FillRectangle(brsh, r)
  39. End Sub
  40. Private Sub OnClicked(ByVal sender As Object, _
  41. ByVal e As ToolBarButtonClickEventArgs)
  42. Dim dialog As New ColorDialog
  43. If dialog.ShowDialog(Me) = DialogResult.OK
  44. col = dialog.Color
  45. Me.Invalidate
  46. End If
  47. End Sub
  48. Private Sub LocateRect
  49. Dim x As Integer = (Me.ClientSize.Width - rectWidth) / 2
  50. Dim y As Integer = (Me.ClientSize.Height - rectHeight) / 2
  51. r = New Rectangle(x, y, rectWidth, rectHeight)
  52. End Sub
  53. Public Shared Sub Main
  54. Application.Run(New WinVBApp)
  55. End Sub
  56. End Class

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

  1. col = Color.Blue

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

  1. Dim dialog As New ColorDialog

ColorDialog已创建。

  1. If dialog.ShowDialog(Me) = DialogResult.OK
  2. col = dialog.Color
  3. Me.Invalidate
  4. End If

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

对话框 - 图2

图:ColorDialog

FontDialog

FontDialog用于选择字体。

  1. ' ZetCode Mono Visual Basic Winforms tutorial
  2. '
  3. ' In this program we use the FontDialog
  4. ' to change a font of a label
  5. '
  6. ' author jan bodnar
  7. ' last modified May 2009
  8. ' website www.zetcode.com
  9. Imports System.Windows.Forms
  10. Imports System.Drawing
  11. Public Class WinVBApp
  12. Inherits Form
  13. Private Dim txt As Label
  14. Public Sub New
  15. Me.Text = "FontDialog"
  16. Me.Size = New Size(300, 250)
  17. Me.InitUI
  18. Me.CenterToScreen
  19. End Sub
  20. Private Sub InitUI
  21. Dim tbar As New ToolBar
  22. tbar.Parent = Me
  23. Dim open As New ToolBarButton
  24. tbar.Buttons.Add(open)
  25. txt = New Label
  26. txt.Parent = Me
  27. txt.Text = "Winforms tutorial"
  28. Me.LocateText
  29. txt.AutoSize = True
  30. AddHandler Me.Resize, AddressOf Me.OnResize
  31. AddHandler tbar.ButtonClick, AddressOf Me.OnClicked
  32. End Sub
  33. Private Sub OnClicked(ByVal sender As Object, _
  34. ByVal e As ToolBarButtonClickEventArgs)
  35. Dim dialog As New FontDialog
  36. If dialog.ShowDialog(Me) = DialogResult.OK
  37. txt.Font = dialog.Font
  38. Me.LocateText
  39. End If
  40. End Sub
  41. Private Sub LocateText
  42. txt.Top = (Me.ClientSize.Height - txt.Height) / 2
  43. txt.Left = (Me.ClientSize.Width - txt.Width) / 2
  44. End Sub
  45. Private Sub OnResize(ByVal sender As Object, ByVal e As EventArgs)
  46. Me.LocateText
  47. End Sub
  48. Public Shared Sub Main
  49. Application.Run(New WinVBApp)
  50. End Sub
  51. End Class

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

  1. Dim dialog As New FontDialog

FontDialog已创建。

  1. If dialog.ShowDialog(Me) = DialogResult.OK
  2. txt.Font = dialog.Font
  3. Me.LocateText
  4. End If

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

对话框 - 图3

图:FontDialog

OpenDialog

此对话框用于打开文件。

  1. ' ZetCode Mono Visual Basic Winforms tutorial
  2. '
  3. ' In this program we use the OpenDialog to
  4. ' open a file and show its contents in
  5. ' a TextBox control
  6. '
  7. ' author jan bodnar
  8. ' last modified May 2009
  9. ' website www.zetcode.com
  10. Imports System.Windows.Forms
  11. Imports System.Drawing
  12. Imports System.IO
  13. Public Class WinVBApp
  14. Inherits Form
  15. Private txtBox As TextBox
  16. Public Sub New
  17. Me.Text = "OpenDialog"
  18. Me.Size = New Size(300, 250)
  19. Me.InitUI
  20. Me.CenterToScreen
  21. End Sub
  22. Private Sub InitUI
  23. Dim tbar As New ToolBar
  24. tbar.Parent = Me
  25. Dim open As New ToolBarButton
  26. tbar.Buttons.Add(open)
  27. txtBox = New TextBox
  28. txtBox.Parent = Me
  29. txtBox.Multiline = True
  30. txtBox.ScrollBars = ScrollBars.Both
  31. txtBox.WordWrap = False
  32. txtBox.Parent = Me
  33. txtBox.Dock = DockStyle.Fill
  34. AddHandler tbar.ButtonClick, AddressOf Me.OnClicked
  35. End Sub
  36. Private Sub OnClicked(ByVal sender As Object, _
  37. ByVal e As ToolBarButtonClickEventArgs)
  38. Dim dia As New OpenFileDialog
  39. dia.Filter = "VB files (*.vb)|*.vb"
  40. If dia.ShowDialog(Me) = DialogResult.OK
  41. Dim reader As New StreamReader(dia.FileName)
  42. Dim data As String = reader.ReadToEnd
  43. reader.Close
  44. txtBox.Text = data
  45. End If
  46. End Sub
  47. Public Shared Sub Main
  48. Application.Run(New WinVBApp)
  49. End Sub
  50. End Class

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

  1. Dim dia As New OpenFileDialog

OpenDialog已创建。

  1. dia.Filter = "VB files (*.vb)|*.vb"

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

  1. If dia.ShowDialog(Me) = DialogResult.OK
  2. Dim reader As New StreamReader(dia.FileName)
  3. Dim data As String = reader.ReadToEnd
  4. reader.Close
  5. txtBox.Text = data
  6. End If

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

对话框 - 图4

图:OpenDialog

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