在 Visual Basic Winforms 教程的这一部分中,我们将讨论对话框。
对话框窗口或对话框是大多数现代 GUI 应用必不可少的部分。 对话被定义为两个或更多人之间的对话。 在计算机应用中,对话框是一个窗口,用于与应用“对话”。 对话框用于输入数据,修改数据,更改应用设置等。对话框是用户与计算机程序之间进行通信的重要手段。
基本上有两种类型的对话框。 预定义对话框和自定义对话框。
FolderBrowserDialog
此对话框提示用户选择一个文件夹。
' ZetCode Mono Visual Basic Winforms tutorial'' In this program we select a directory with a' FolderBrowser dialog. The selected directory's' name is shown in the statusbar.'' author jan bodnar' last modified May 2009' website www.zetcode.comImports System.Windows.FormsImports System.DrawingPublic Class WinVBAppInherits FormDim statusbar As StatusBarPublic Sub NewMe.Text = "FolderBrowserDialog"Me.Size = New Size(300, 250)Me.InitUIMe.CenterToScreenEnd SubPrivate Sub InitUIDim toolbar As New ToolBarDim open As New ToolBarButtonstatusbar = New StatusBarstatusbar.Parent = Metoolbar.Buttons.Add(open)Me.Controls.Add(toolbar)AddHandler toolbar.ButtonClick, AddressOf Me.OnClickedEnd SubPrivate Sub OnClicked(ByVal sender As Object, _ByVal e As ToolBarButtonClickEventArgs)Dim dialog As New FolderBrowserDialogIf dialog.ShowDialog(Me) = DialogResult.OKstatusbar.Text = dialog.SelectedPathEnd IfEnd SubPublic Shared Sub MainApplication.Run(New WinVBApp)End SubEnd Class
我们有一个工具栏和一个工具栏按钮。 点击按钮,FolderBrowserDialog出现在屏幕上。 所选文件夹的名称显示在状态栏中。
Dim dialog As New FolderBrowserDialog
FolderBrowserDialog已创建。
If dialog.ShowDialog(Me) = DialogResult.OKstatusbar.Text = dialog.SelectedPathEnd If
ShowDialog方法在屏幕上显示对话框。 如果单击对话框的“确定”按钮,则所选的目录路径将显示在状态栏上。

图:FolderBrowserDialog
ColorDialog
此对话框显示可用的颜色以及使用户能够定义自定义颜色的控件。
' ZetCode Mono Visual Basic Winforms tutorial'' In this program we use the ColorDialog' to change a color of a rectangle'' author jan bodnar' last modified May 2009' website www.zetcode.comImports System.Windows.FormsImports System.DrawingPublic Class WinVBAppInherits FormPrivate col As ColorPrivate Const rectWidth As Integer = 100Private Const rectHeight As Integer = 100Private Dim r As RectanglePublic Sub NewMe.Text = "ColorDialog"Me.Size = New Size(300, 250)Me.InitUIMe.CenterToScreenEnd SubPrivate Sub InitUIDim tbar As New ToolBarDim open As New ToolBarButtoncol = Color.Bluetbar.Buttons.Add(open)Me.LocateRectMe.SetStyle(ControlStyles.ResizeRedraw, True)Controls.Add(tbar)AddHandler Me.Paint, AddressOf Me.OnPaintAddHandler tbar.ButtonClick, AddressOf Me.OnClickedEnd SubPrivate Sub OnPaint(ByVal sender As Object, ByVal e As PaintEventArgs)Dim g As Graphics = e.GraphicsMe.LocateRectDim brsh As New SolidBrush(col)g.FillRectangle(brsh, r)End SubPrivate Sub OnClicked(ByVal sender As Object, _ByVal e As ToolBarButtonClickEventArgs)Dim dialog As New ColorDialogIf dialog.ShowDialog(Me) = DialogResult.OKcol = dialog.ColorMe.InvalidateEnd IfEnd SubPrivate Sub LocateRectDim x As Integer = (Me.ClientSize.Width - rectWidth) / 2Dim y As Integer = (Me.ClientSize.Height - rectHeight) / 2r = New Rectangle(x, y, rectWidth, rectHeight)End SubPublic Shared Sub MainApplication.Run(New WinVBApp)End SubEnd Class
在此代码示例中,我们使用ColorDialog为位于窗体控件中间的矩形选择颜色。
col = Color.Blue
开始时,矩形的颜色是蓝色。 我们使用col变量来确定矩形的颜色。
Dim dialog As New ColorDialog
ColorDialog已创建。
If dialog.ShowDialog(Me) = DialogResult.OKcol = dialog.ColorMe.InvalidateEnd If
该代码显示颜色对话框。 如果单击“确定”按钮,则将获得选定的颜色并调用Invalidate方法。 该方法会使控件的整个表面无效,并使控件重画。 结果是用新的颜色值绘制了矩形。

图:ColorDialog
FontDialog
FontDialog用于选择字体。
' ZetCode Mono Visual Basic Winforms tutorial'' In this program we use the FontDialog' to change a font of a label'' author jan bodnar' last modified May 2009' website www.zetcode.comImports System.Windows.FormsImports System.DrawingPublic Class WinVBAppInherits FormPrivate Dim txt As LabelPublic Sub NewMe.Text = "FontDialog"Me.Size = New Size(300, 250)Me.InitUIMe.CenterToScreenEnd SubPrivate Sub InitUIDim tbar As New ToolBartbar.Parent = MeDim open As New ToolBarButtontbar.Buttons.Add(open)txt = New Labeltxt.Parent = Metxt.Text = "Winforms tutorial"Me.LocateTexttxt.AutoSize = TrueAddHandler Me.Resize, AddressOf Me.OnResizeAddHandler tbar.ButtonClick, AddressOf Me.OnClickedEnd SubPrivate Sub OnClicked(ByVal sender As Object, _ByVal e As ToolBarButtonClickEventArgs)Dim dialog As New FontDialogIf dialog.ShowDialog(Me) = DialogResult.OKtxt.Font = dialog.FontMe.LocateTextEnd IfEnd SubPrivate Sub LocateTexttxt.Top = (Me.ClientSize.Height - txt.Height) / 2txt.Left = (Me.ClientSize.Width - txt.Width) / 2End SubPrivate Sub OnResize(ByVal sender As Object, ByVal e As EventArgs)Me.LocateTextEnd SubPublic Shared Sub MainApplication.Run(New WinVBApp)End SubEnd Class
我们在表单控件的中间绘制一些文本。 我们使用字体对话框更改此文本的字体。
Dim dialog As New FontDialog
FontDialog已创建。
If dialog.ShowDialog(Me) = DialogResult.OKtxt.Font = dialog.FontMe.LocateTextEnd If
单击“确定”按钮时,将为Label控件设置新选择的字体。 由于文本的大小会随着字体的变化而变化,因此我们必须调用LocateText方法,该方法将文本定位在表单控件的中间。

图:FontDialog
OpenDialog
此对话框用于打开文件。
' ZetCode Mono Visual Basic Winforms tutorial'' In this program we use the OpenDialog to' open a file and show its contents in' a TextBox control'' author jan bodnar' last modified May 2009' website www.zetcode.comImports System.Windows.FormsImports System.DrawingImports System.IOPublic Class WinVBAppInherits FormPrivate txtBox As TextBoxPublic Sub NewMe.Text = "OpenDialog"Me.Size = New Size(300, 250)Me.InitUIMe.CenterToScreenEnd SubPrivate Sub InitUIDim tbar As New ToolBartbar.Parent = MeDim open As New ToolBarButtontbar.Buttons.Add(open)txtBox = New TextBoxtxtBox.Parent = MetxtBox.Multiline = TruetxtBox.ScrollBars = ScrollBars.BothtxtBox.WordWrap = FalsetxtBox.Parent = MetxtBox.Dock = DockStyle.FillAddHandler tbar.ButtonClick, AddressOf Me.OnClickedEnd SubPrivate Sub OnClicked(ByVal sender As Object, _ByVal e As ToolBarButtonClickEventArgs)Dim dia As New OpenFileDialogdia.Filter = "VB files (*.vb)|*.vb"If dia.ShowDialog(Me) = DialogResult.OKDim reader As New StreamReader(dia.FileName)Dim data As String = reader.ReadToEndreader.ClosetxtBox.Text = dataEnd IfEnd SubPublic Shared Sub MainApplication.Run(New WinVBApp)End SubEnd Class
我们使用OpenDialog控件打开 VB 源文件。 我们有一个TextBox控件,用于显示文件。
Dim dia As New OpenFileDialog
OpenDialog已创建。
dia.Filter = "VB files (*.vb)|*.vb"
我们将Filter属性设置为 VB 源文件。 此对话框实例只能选择 VB 文件。
If dia.ShowDialog(Me) = DialogResult.OKDim reader As New StreamReader(dia.FileName)Dim data As String = reader.ReadToEndreader.ClosetxtBox.Text = dataEnd If
单击确定后,我们读取所选文件的内容并将其放入TextBox控件。

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