在 Visual Basic GTK# 编程教程的这一部分中,我们将介绍对话框。
对话框窗口或对话框是大多数现代 GUI 应用必不可少的部分。 对话被定义为两个或更多人之间的对话。 在计算机应用中,对话框是一个窗口,用于与应用“对话”。 对话框用于输入数据,修改数据,更改应用设置等。对话框是用户与计算机程序之间进行通信的重要手段。
MessageDialog
消息对话框是方便的对话框,可向应用的用户提供消息。 该消息包含文本和图像数据。
' ZetCode Mono Visual Basic GTK# tutorial'' This program shows message dialogs.'' author jan bodnar' last modified May 2009' website www.zetcode.comImports GtkPublic Class GtkVBAppInherits WindowPublic Sub NewMyBase.New("Message dialogs")Me.InitUIMe.SetDefaultSize(250, 100)Me.SetPosition(WindowPosition.Center)AddHandler Me.DeleteEvent, AddressOf Me.OnDeleteMe.ShowAllEnd SubPrivate Sub InitUIDim table As New Table(2, 2, True)Dim info As New Button("Information")Dim warn As New Button("Warning")Dim ques As New Button("Question")Dim erro As New Button("Error")AddHandler info.Clicked, AddressOf Me.OnInfoAddHandler warn.Clicked, AddressOf Me.OnWarningAddHandler ques.Clicked, AddressOf Me.OnQuestionAddHandler erro.Clicked, AddressOf Me.OnErrortable.Attach(info, 0, 1, 0, 1)table.Attach(warn, 1, 2, 0, 1)table.Attach(ques, 0, 1, 1, 2)table.Attach(erro, 1, 2, 1, 2)Me.Add(table)End SubPrivate Sub OnInfo(ByVal sender As Object, ByVal args As EventArgs)Dim md As MessageDialog = New MessageDialog(Me, _DialogFlags.DestroyWithParent, MessageType.Info, _ButtonsType.Close, "Download completed")md.Runmd.DestroyEnd SubPrivate Sub OnWarning(ByVal sender As Object, ByVal args As EventArgs)Dim md As MessageDialog = New MessageDialog(Me, _DialogFlags.DestroyWithParent, MessageType.Warning, _ButtonsType.Close, "Unallowed operation")md.Runmd.DestroyEnd SubPrivate Sub OnQuestion(ByVal sender As Object, ByVal args As EventArgs)Dim md As MessageDialog = New MessageDialog(Me, _DialogFlags.DestroyWithParent, MessageType.Question, _ButtonsType.Close, "Are you sure to quit?")md.Runmd.DestroyEnd SubPrivate Sub OnError(ByVal sender As Object, ByVal args As EventArgs)Dim md As MessageDialog = New MessageDialog(Me, _DialogFlags.DestroyWithParent, MessageType.Error, _ButtonsType.Close, "Error loading file")md.Runmd.DestroyEnd SubPrivate Sub OnDelete(ByVal sender As Object, _ByVal args As DeleteEventArgs)Application.QuitEnd SubPublic Shared Sub MainApplication.InitDim app As New GtkVBAppApplication.RunEnd SubEnd Class
在我们的示例中,我们将显示四种消息对话框。 信息,警告,问题和错误消息对话框。
Dim info As New Button("Information")Dim warn As New Button("Warning")Dim ques As New Button("Question")Dim erro As New Button("Error")
我们有四个按钮。 这些按钮中的每个按钮都会显示不同类型的消息对话框。
Private Sub OnInfo(ByVal sender As Object, ByVal args As EventArgs)Dim md As MessageDialog = New MessageDialog(Me, _DialogFlags.DestroyWithParent, MessageType.Info, _ButtonsType.Close, "Download completed")md.Runmd.DestroyEnd Sub
如果单击信息按钮,将显示“信息”对话框。 MessageType.Info指定对话框的类型。 ButtonsType.Close指定要在对话框中显示的按钮。 最后一个参数是已分发的消息。 该对话框使用Run方法显示。 程序员还必须调用Destroy或Hide方法。




AboutDialog
AboutDialog显示有关应用的信息。 AboutDialog可以显示徽标,应用名称,版本,版权,网站或许可证信息。 也有可能对作者,文档撰写者,翻译者和艺术家予以赞扬。
' ZetCode Mono Visual Basic GTK# tutorial'' This program shows the about' dialog'' author jan bodnar' last modified May 2009' website www.zetcode.comImports GtkPublic Class GtkVBAppInherits WindowPublic Sub NewMyBase.New("About dialog")Me.InitUIMe.SetDefaultSize(350, 300)Me.SetPosition(WindowPosition.Center)AddHandler Me.DeleteEvent, AddressOf Me.OnDeleteMe.ShowAllEnd SubPrivate Sub InitUIDim button As New Button("About")AddHandler button.Clicked, AddressOf Me.ShowDialogDim fixed As New Fixedfixed.Put(button, 20, 20)Me.Add(fixed)End SubPrivate Sub ShowDialog(ByVal sender As Object, _ByVal args As EventArgs)Dim about As New AboutDialogabout.ProgramName = "Battery"about.Version = "0.1"about.Copyright = "(c) Jan Bodnar"about.Comments = "Battery is a simple tool for battery checking"about.Website = "http://www.zetcode.com"about.Logo = New Gdk.Pixbuf("battery.png")about.Runabout.DestroyEnd SubSub OnDelete(ByVal sender As Object, _ByVal args As DeleteEventArgs)Application.QuitEnd SubPublic Shared Sub MainApplication.InitDim app As New GtkVBAppApplication.RunEnd SubEnd Class
该代码示例使用具有某些功能的AboutDialog。
Dim about As New AboutDialog
我们创建一个AboutDialog。
Dim about As New AboutDialogabout.ProgramName = "Battery"about.Version = "0.1"about.Copyright = "(c) Jan Bodnar"
通过设置对话框的属性,我们指定名称,版本和版权。
about.Logo = New Gdk.Pixbuf("battery.png")
此行创建徽标。

图:AboutDialog
FontSelectionDialog
FontSelectionDialog是用于选择字体的对话框。 它通常用于进行一些文本编辑或格式化的应用中。
' ZetCode Mono Visual Basic GTK# tutorial'' This program shows the FontSelectionDialog'' author jan bodnar' last modified May 2009' website www.zetcode.comImports GtkPublic Class GtkVBAppInherits WindowDim label As LabelPublic Sub NewMyBase.New("Font dialog")Me.InitUIMe.SetDefaultSize(350, 300)Me.SetPosition(WindowPosition.Center)AddHandler Me.DeleteEvent, AddressOf Me.OnDeleteMe.ShowAllEnd SubPrivate Sub InitUIlabel = New Label("The only victory over love is flight.")Dim button As New Button("Select font")AddHandler button.Clicked, AddressOf Me.ShowDialogDim fixed As New Fixedfixed.Put(button, 100, 30)fixed.Put(label, 30, 90)Me.Add(fixed)End SubPrivate Sub ShowDialog(ByVal sender As Object, _ByVal args As EventArgs)Dim fdia As New FontSelectionDialog("Select font name")AddHandler fdia.Response, AddressOf Me.SelectFontfdia.Runfdia.DestroyEnd SubPrivate Sub SelectFont(ByVal sender As Object, _ByVal args As ResponseArgs)If args.ResponseId = ResponseType.OkDim fontdesc As Pango.FontDescription = _Pango.FontDescription.FromString(sender.FontName)label.ModifyFont(fontdesc)End IfEnd SubSub OnDelete(ByVal sender As Object, _ByVal args As DeleteEventArgs)Application.QuitEnd SubPublic Shared Sub MainApplication.InitDim app As New GtkVBAppApplication.RunEnd SubEnd Class
在代码示例中,我们有一个按钮和一个标签。 单击按钮显示FontSelectionDialog。
Dim fdia As New FontSelectionDialog("Select font name")
我们创建FontSelectionDialog。
If args.ResponseId = ResponseType.OkDim fontdesc As Pango.FontDescription = _Pango.FontDescription.FromString(sender.FontName)label.ModifyFont(fontdesc)End If
如果单击“确定”按钮,则标签小部件的字体将更改为我们在对话框中选择的字体。

图:FontSelectionDialog
ColorSelectionDialog
ColorSelectionDialog是用于选择颜色的对话框。
' ZetCode Mono Visual Basic GTK# tutorial'' This program shows the ColorSelectionDialog'' author jan bodnar' last modified May 2009' website www.zetcode.comImports GtkPublic Class GtkVBAppInherits WindowDim label As LabelPublic Sub NewMyBase.New("Color dialog")Me.InitUIMe.SetDefaultSize(350, 300)Me.SetPosition(WindowPosition.Center)AddHandler Me.DeleteEvent, AddressOf Me.OnDeleteMe.ShowAllEnd SubPrivate Sub InitUIlabel = New Label("The only victory over love is flight.")Dim button As New Button("Select color")AddHandler button.Clicked, AddressOf Me.ShowDialogDim fixed As New Fixedfixed.Put(button, 100, 30)fixed.Put(label, 30, 90)Me.Add(fixed)End SubPrivate Sub ShowDialog(ByVal sender As Object, _ByVal args As EventArgs)Dim cdia As New ColorSelectionDialog("Select color")AddHandler cdia.Response, AddressOf Me.SelectColorcdia.Runcdia.DestroyEnd SubPrivate Sub SelectColor(ByVal sender As Object, _ByVal args As ResponseArgs)If args.ResponseId = ResponseType.Oklabel.ModifyFg(StateType.Normal, _sender.ColorSelection.CurrentColor)End IfEnd SubSub OnDelete(ByVal sender As Object, _ByVal args As DeleteEventArgs)Application.QuitEnd SubPublic Shared Sub MainApplication.InitDim app As New GtkVBAppApplication.RunEnd SubEnd Class
该示例与上一个示例非常相似。 这次我们更改标签的颜色。
Dim cdia As New ColorSelectionDialog("Select color")
我们创建ColorSelectionDialog。
If args.ResponseId = ResponseType.Oklabel.ModifyFg(StateType.Normal, _sender.ColorSelection.CurrentColor)End If
如果用户按下 OK,我们将获得颜色值并修改标签的颜色。

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