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

在 Visual Basic GTK# 编程教程的这一部分中,我们将介绍对话框。

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

MessageDialog

消息对话框是方便的对话框,可向应用的用户提供消息。 该消息包含文本和图像数据。

  1. ' ZetCode Mono Visual Basic GTK# tutorial
  2. '
  3. ' This program shows message dialogs.
  4. '
  5. ' author jan bodnar
  6. ' last modified May 2009
  7. ' website www.zetcode.com
  8. Imports Gtk
  9. Public Class GtkVBApp
  10. Inherits Window
  11. Public Sub New
  12. MyBase.New("Message dialogs")
  13. Me.InitUI
  14. Me.SetDefaultSize(250, 100)
  15. Me.SetPosition(WindowPosition.Center)
  16. AddHandler Me.DeleteEvent, AddressOf Me.OnDelete
  17. Me.ShowAll
  18. End Sub
  19. Private Sub InitUI
  20. Dim table As New Table(2, 2, True)
  21. Dim info As New Button("Information")
  22. Dim warn As New Button("Warning")
  23. Dim ques As New Button("Question")
  24. Dim erro As New Button("Error")
  25. AddHandler info.Clicked, AddressOf Me.OnInfo
  26. AddHandler warn.Clicked, AddressOf Me.OnWarning
  27. AddHandler ques.Clicked, AddressOf Me.OnQuestion
  28. AddHandler erro.Clicked, AddressOf Me.OnError
  29. table.Attach(info, 0, 1, 0, 1)
  30. table.Attach(warn, 1, 2, 0, 1)
  31. table.Attach(ques, 0, 1, 1, 2)
  32. table.Attach(erro, 1, 2, 1, 2)
  33. Me.Add(table)
  34. End Sub
  35. Private Sub OnInfo(ByVal sender As Object, ByVal args As EventArgs)
  36. Dim md As MessageDialog = New MessageDialog(Me, _
  37. DialogFlags.DestroyWithParent, MessageType.Info, _
  38. ButtonsType.Close, "Download completed")
  39. md.Run
  40. md.Destroy
  41. End Sub
  42. Private Sub OnWarning(ByVal sender As Object, ByVal args As EventArgs)
  43. Dim md As MessageDialog = New MessageDialog(Me, _
  44. DialogFlags.DestroyWithParent, MessageType.Warning, _
  45. ButtonsType.Close, "Unallowed operation")
  46. md.Run
  47. md.Destroy
  48. End Sub
  49. Private Sub OnQuestion(ByVal sender As Object, ByVal args As EventArgs)
  50. Dim md As MessageDialog = New MessageDialog(Me, _
  51. DialogFlags.DestroyWithParent, MessageType.Question, _
  52. ButtonsType.Close, "Are you sure to quit?")
  53. md.Run
  54. md.Destroy
  55. End Sub
  56. Private Sub OnError(ByVal sender As Object, ByVal args As EventArgs)
  57. Dim md As MessageDialog = New MessageDialog(Me, _
  58. DialogFlags.DestroyWithParent, MessageType.Error, _
  59. ButtonsType.Close, "Error loading file")
  60. md.Run
  61. md.Destroy
  62. End Sub
  63. Private Sub OnDelete(ByVal sender As Object, _
  64. ByVal args As DeleteEventArgs)
  65. Application.Quit
  66. End Sub
  67. Public Shared Sub Main
  68. Application.Init
  69. Dim app As New GtkVBApp
  70. Application.Run
  71. End Sub
  72. End Class

在我们的示例中,我们将显示四种消息对话框。 信息,警告,问题和错误消息对话框。

  1. Dim info As New Button("Information")
  2. Dim warn As New Button("Warning")
  3. Dim ques As New Button("Question")
  4. Dim erro As New Button("Error")

我们有四个按钮。 这些按钮中的每个按钮都会显示不同类型的消息对话框。

  1. Private Sub OnInfo(ByVal sender As Object, ByVal args As EventArgs)
  2. Dim md As MessageDialog = New MessageDialog(Me, _
  3. DialogFlags.DestroyWithParent, MessageType.Info, _
  4. ButtonsType.Close, "Download completed")
  5. md.Run
  6. md.Destroy
  7. End Sub

如果单击信息按钮,将显示“信息”对话框。 MessageType.Info指定对话框的类型。 ButtonsType.Close指定要在对话框中显示的按钮。 最后一个参数是已分发的消息。 该对话框使用Run方法显示。 程序员还必须调用DestroyHide方法。

对话框 - 图1

对话框 - 图2

对话框 - 图3

对话框 - 图4

AboutDialog

AboutDialog显示有关应用的信息。 AboutDialog可以显示徽标,应用名称,版本,版权,网站或许可证信息。 也有可能对作者,文档撰写者,翻译者和艺术家予以赞扬。

  1. ' ZetCode Mono Visual Basic GTK# tutorial
  2. '
  3. ' This program shows the about
  4. ' dialog
  5. '
  6. ' author jan bodnar
  7. ' last modified May 2009
  8. ' website www.zetcode.com
  9. Imports Gtk
  10. Public Class GtkVBApp
  11. Inherits Window
  12. Public Sub New
  13. MyBase.New("About dialog")
  14. Me.InitUI
  15. Me.SetDefaultSize(350, 300)
  16. Me.SetPosition(WindowPosition.Center)
  17. AddHandler Me.DeleteEvent, AddressOf Me.OnDelete
  18. Me.ShowAll
  19. End Sub
  20. Private Sub InitUI
  21. Dim button As New Button("About")
  22. AddHandler button.Clicked, AddressOf Me.ShowDialog
  23. Dim fixed As New Fixed
  24. fixed.Put(button, 20, 20)
  25. Me.Add(fixed)
  26. End Sub
  27. Private Sub ShowDialog(ByVal sender As Object, _
  28. ByVal args As EventArgs)
  29. Dim about As New AboutDialog
  30. about.ProgramName = "Battery"
  31. about.Version = "0.1"
  32. about.Copyright = "(c) Jan Bodnar"
  33. about.Comments = "Battery is a simple tool for battery checking"
  34. about.Website = "http://www.zetcode.com"
  35. about.Logo = New Gdk.Pixbuf("battery.png")
  36. about.Run
  37. about.Destroy
  38. End Sub
  39. Sub OnDelete(ByVal sender As Object, _
  40. ByVal args As DeleteEventArgs)
  41. Application.Quit
  42. End Sub
  43. Public Shared Sub Main
  44. Application.Init
  45. Dim app As New GtkVBApp
  46. Application.Run
  47. End Sub
  48. End Class

该代码示例使用具有某些功能的AboutDialog

  1. Dim about As New AboutDialog

我们创建一个AboutDialog

  1. Dim about As New AboutDialog
  2. about.ProgramName = "Battery"
  3. about.Version = "0.1"
  4. about.Copyright = "(c) Jan Bodnar"

通过设置对话框的属性,我们指定名称,版本和版权。

  1. about.Logo = New Gdk.Pixbuf("battery.png")

此行创建徽标。

对话框 - 图5

图:AboutDialog

FontSelectionDialog

FontSelectionDialog是用于选择字体的对话框。 它通常用于进行一些文本编辑或格式化的应用中。

  1. ' ZetCode Mono Visual Basic GTK# tutorial
  2. '
  3. ' This program shows the FontSelectionDialog
  4. '
  5. ' author jan bodnar
  6. ' last modified May 2009
  7. ' website www.zetcode.com
  8. Imports Gtk
  9. Public Class GtkVBApp
  10. Inherits Window
  11. Dim label As Label
  12. Public Sub New
  13. MyBase.New("Font dialog")
  14. Me.InitUI
  15. Me.SetDefaultSize(350, 300)
  16. Me.SetPosition(WindowPosition.Center)
  17. AddHandler Me.DeleteEvent, AddressOf Me.OnDelete
  18. Me.ShowAll
  19. End Sub
  20. Private Sub InitUI
  21. label = New Label("The only victory over love is flight.")
  22. Dim button As New Button("Select font")
  23. AddHandler button.Clicked, AddressOf Me.ShowDialog
  24. Dim fixed As New Fixed
  25. fixed.Put(button, 100, 30)
  26. fixed.Put(label, 30, 90)
  27. Me.Add(fixed)
  28. End Sub
  29. Private Sub ShowDialog(ByVal sender As Object, _
  30. ByVal args As EventArgs)
  31. Dim fdia As New FontSelectionDialog("Select font name")
  32. AddHandler fdia.Response, AddressOf Me.SelectFont
  33. fdia.Run
  34. fdia.Destroy
  35. End Sub
  36. Private Sub SelectFont(ByVal sender As Object, _
  37. ByVal args As ResponseArgs)
  38. If args.ResponseId = ResponseType.Ok
  39. Dim fontdesc As Pango.FontDescription = _
  40. Pango.FontDescription.FromString(sender.FontName)
  41. label.ModifyFont(fontdesc)
  42. End If
  43. End Sub
  44. Sub OnDelete(ByVal sender As Object, _
  45. ByVal args As DeleteEventArgs)
  46. Application.Quit
  47. End Sub
  48. Public Shared Sub Main
  49. Application.Init
  50. Dim app As New GtkVBApp
  51. Application.Run
  52. End Sub
  53. End Class

在代码示例中,我们有一个按钮和一个标签。 单击按钮显示FontSelectionDialog

  1. Dim fdia As New FontSelectionDialog("Select font name")

我们创建FontSelectionDialog

  1. If args.ResponseId = ResponseType.Ok
  2. Dim fontdesc As Pango.FontDescription = _
  3. Pango.FontDescription.FromString(sender.FontName)
  4. label.ModifyFont(fontdesc)
  5. End If

如果单击“确定”按钮,则标签小部件的字体将更改为我们在对话框中选择的字体。

对话框 - 图6

图:FontSelectionDialog

ColorSelectionDialog

ColorSelectionDialog是用于选择颜色的对话框。

  1. ' ZetCode Mono Visual Basic GTK# tutorial
  2. '
  3. ' This program shows the ColorSelectionDialog
  4. '
  5. ' author jan bodnar
  6. ' last modified May 2009
  7. ' website www.zetcode.com
  8. Imports Gtk
  9. Public Class GtkVBApp
  10. Inherits Window
  11. Dim label As Label
  12. Public Sub New
  13. MyBase.New("Color dialog")
  14. Me.InitUI
  15. Me.SetDefaultSize(350, 300)
  16. Me.SetPosition(WindowPosition.Center)
  17. AddHandler Me.DeleteEvent, AddressOf Me.OnDelete
  18. Me.ShowAll
  19. End Sub
  20. Private Sub InitUI
  21. label = New Label("The only victory over love is flight.")
  22. Dim button As New Button("Select color")
  23. AddHandler button.Clicked, AddressOf Me.ShowDialog
  24. Dim fixed As New Fixed
  25. fixed.Put(button, 100, 30)
  26. fixed.Put(label, 30, 90)
  27. Me.Add(fixed)
  28. End Sub
  29. Private Sub ShowDialog(ByVal sender As Object, _
  30. ByVal args As EventArgs)
  31. Dim cdia As New ColorSelectionDialog("Select color")
  32. AddHandler cdia.Response, AddressOf Me.SelectColor
  33. cdia.Run
  34. cdia.Destroy
  35. End Sub
  36. Private Sub SelectColor(ByVal sender As Object, _
  37. ByVal args As ResponseArgs)
  38. If args.ResponseId = ResponseType.Ok
  39. label.ModifyFg(StateType.Normal, _
  40. sender.ColorSelection.CurrentColor)
  41. End If
  42. End Sub
  43. Sub OnDelete(ByVal sender As Object, _
  44. ByVal args As DeleteEventArgs)
  45. Application.Quit
  46. End Sub
  47. Public Shared Sub Main
  48. Application.Init
  49. Dim app As New GtkVBApp
  50. Application.Run
  51. End Sub
  52. End Class

该示例与上一个示例非常相似。 这次我们更改标签的颜色。

  1. Dim cdia As New ColorSelectionDialog("Select color")

我们创建ColorSelectionDialog

  1. If args.ResponseId = ResponseType.Ok
  2. label.ModifyFg(StateType.Normal, _
  3. sender.ColorSelection.CurrentColor)
  4. End If

如果用户按下 OK,我们将获得颜色值并修改标签的颜色。

对话框 - 图7

图:颜色 electionDialog

在 Visual Basic GTK# 教程的这一部分中,我们介绍了对话框。