<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # 對話框 > 原文: [http://zetcode.com/gui/vbwinforms/dialogs/](http://zetcode.com/gui/vbwinforms/dialogs/) 在 Visual Basic Winforms 教程的這一部分中,我們將討論對話框。 對話框窗口或對話框是大多數現代 GUI 應用必不可少的部分。 對話被定義為兩個或更多人之間的對話。 在計算機應用中,對話框是一個窗口,用于與應用“對話”。 對話框用于輸入數據,修改數據,更改應用設置等。對話框是用戶與計算機程序之間進行通信的重要手段。 基本上有兩種類型的對話框。 預定義對話框和自定義對話框。 ## `FolderBrowserDialog` 此對話框提示用戶選擇一個文件夾。 ```vb ' 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.com Imports System.Windows.Forms Imports System.Drawing Public Class WinVBApp Inherits Form Dim statusbar As StatusBar Public Sub New Me.Text = "FolderBrowserDialog" Me.Size = New Size(300, 250) Me.InitUI Me.CenterToScreen End Sub Private Sub InitUI Dim toolbar As New ToolBar Dim open As New ToolBarButton statusbar = New StatusBar statusbar.Parent = Me toolbar.Buttons.Add(open) Me.Controls.Add(toolbar) AddHandler toolbar.ButtonClick, AddressOf Me.OnClicked End Sub Private Sub OnClicked(ByVal sender As Object, _ ByVal e As ToolBarButtonClickEventArgs) Dim dialog As New FolderBrowserDialog If dialog.ShowDialog(Me) = DialogResult.OK statusbar.Text = dialog.SelectedPath End If End Sub Public Shared Sub Main Application.Run(New WinVBApp) End Sub End Class ``` 我們有一個工具欄和一個工具欄按鈕。 點擊按鈕,`FolderBrowserDialog`出現在屏幕上。 所選文件夾的名稱顯示在狀態欄中。 ```vb Dim dialog As New FolderBrowserDialog ``` `FolderBrowserDialog`已創建。 ```vb If dialog.ShowDialog(Me) = DialogResult.OK statusbar.Text = dialog.SelectedPath End If ``` `ShowDialog`方法在屏幕上顯示對話框。 如果單擊對話框的“確定”按鈕,則所選的目錄路徑將顯示在狀態欄上。 ![FolderBrowserDialog](https://img.kancloud.cn/72/7f/727faa65b7c38215686e4572fb856e4a_364x384.jpg) 圖:`FolderBrowserDialog` ## `ColorDialog` 此對話框顯示可用的顏色以及使用戶能夠定義自定義顏色的控件。 ```vb ' 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.com Imports System.Windows.Forms Imports System.Drawing Public Class WinVBApp Inherits Form Private col As Color Private Const rectWidth As Integer = 100 Private Const rectHeight As Integer = 100 Private Dim r As Rectangle Public Sub New Me.Text = "ColorDialog" Me.Size = New Size(300, 250) Me.InitUI Me.CenterToScreen End Sub Private Sub InitUI Dim tbar As New ToolBar Dim open As New ToolBarButton col = Color.Blue tbar.Buttons.Add(open) Me.LocateRect Me.SetStyle(ControlStyles.ResizeRedraw, True) Controls.Add(tbar) AddHandler Me.Paint, AddressOf Me.OnPaint AddHandler tbar.ButtonClick, AddressOf Me.OnClicked End Sub Private Sub OnPaint(ByVal sender As Object, ByVal e As PaintEventArgs) Dim g As Graphics = e.Graphics Me.LocateRect Dim brsh As New SolidBrush(col) g.FillRectangle(brsh, r) End Sub Private Sub OnClicked(ByVal sender As Object, _ ByVal e As ToolBarButtonClickEventArgs) Dim dialog As New ColorDialog If dialog.ShowDialog(Me) = DialogResult.OK col = dialog.Color Me.Invalidate End If End Sub Private Sub LocateRect Dim x As Integer = (Me.ClientSize.Width - rectWidth) / 2 Dim y As Integer = (Me.ClientSize.Height - rectHeight) / 2 r = New Rectangle(x, y, rectWidth, rectHeight) End Sub Public Shared Sub Main Application.Run(New WinVBApp) End Sub End Class ``` 在此代碼示例中,我們使用`ColorDialog`為位于窗體控件中間的矩形選擇顏色。 ```vb col = Color.Blue ``` 開始時,矩形的顏色是藍色。 我們使用`col`變量來確定矩形的顏色。 ```vb Dim dialog As New ColorDialog ``` `ColorDialog`已創建。 ```vb If dialog.ShowDialog(Me) = DialogResult.OK col = dialog.Color Me.Invalidate End If ``` 該代碼顯示顏色對話框。 如果單擊“確定”按鈕,則將獲得選定的顏色并調用`Invalidate`方法。 該方法會使控件的整個表面無效,并使控件重畫。 結果是用新的顏色值繪制了矩形。 ![ColorDialog](https://img.kancloud.cn/8b/63/8b63992a95fa974f1679263231a4b083_450x335.jpg) 圖:`ColorDialog` ## `FontDialog` `FontDialog`用于選擇字體。 ```vb ' 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.com Imports System.Windows.Forms Imports System.Drawing Public Class WinVBApp Inherits Form Private Dim txt As Label Public Sub New Me.Text = "FontDialog" Me.Size = New Size(300, 250) Me.InitUI Me.CenterToScreen End Sub Private Sub InitUI Dim tbar As New ToolBar tbar.Parent = Me Dim open As New ToolBarButton tbar.Buttons.Add(open) txt = New Label txt.Parent = Me txt.Text = "Winforms tutorial" Me.LocateText txt.AutoSize = True AddHandler Me.Resize, AddressOf Me.OnResize AddHandler tbar.ButtonClick, AddressOf Me.OnClicked End Sub Private Sub OnClicked(ByVal sender As Object, _ ByVal e As ToolBarButtonClickEventArgs) Dim dialog As New FontDialog If dialog.ShowDialog(Me) = DialogResult.OK txt.Font = dialog.Font Me.LocateText End If End Sub Private Sub LocateText txt.Top = (Me.ClientSize.Height - txt.Height) / 2 txt.Left = (Me.ClientSize.Width - txt.Width) / 2 End Sub Private Sub OnResize(ByVal sender As Object, ByVal e As EventArgs) Me.LocateText End Sub Public Shared Sub Main Application.Run(New WinVBApp) End Sub End Class ``` 我們在表單控件的中間繪制一些文本。 我們使用字體對話框更改此文本的字體。 ```vb Dim dialog As New FontDialog ``` `FontDialog`已創建。 ```vb If dialog.ShowDialog(Me) = DialogResult.OK txt.Font = dialog.Font Me.LocateText End If ``` 單擊“確定”按鈕時,將為`Label`控件設置新選擇的字體。 由于文本的大小會隨著字體的變化而變化,因此我們必須調用`LocateText`方法,該方法將文本定位在表單控件的中間。 ![FontDialog](https://img.kancloud.cn/43/0e/430e2fb72ea197312caf7dd147ffc947_432x321.jpg) 圖:`FontDialog` ## `OpenDialog` 此對話框用于打開文件。 ```vb ' 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.com Imports System.Windows.Forms Imports System.Drawing Imports System.IO Public Class WinVBApp Inherits Form Private txtBox As TextBox Public Sub New Me.Text = "OpenDialog" Me.Size = New Size(300, 250) Me.InitUI Me.CenterToScreen End Sub Private Sub InitUI Dim tbar As New ToolBar tbar.Parent = Me Dim open As New ToolBarButton tbar.Buttons.Add(open) txtBox = New TextBox txtBox.Parent = Me txtBox.Multiline = True txtBox.ScrollBars = ScrollBars.Both txtBox.WordWrap = False txtBox.Parent = Me txtBox.Dock = DockStyle.Fill AddHandler tbar.ButtonClick, AddressOf Me.OnClicked End Sub Private Sub OnClicked(ByVal sender As Object, _ ByVal e As ToolBarButtonClickEventArgs) Dim dia As New OpenFileDialog dia.Filter = "VB files (*.vb)|*.vb" If dia.ShowDialog(Me) = DialogResult.OK Dim reader As New StreamReader(dia.FileName) Dim data As String = reader.ReadToEnd reader.Close txtBox.Text = data End If End Sub Public Shared Sub Main Application.Run(New WinVBApp) End Sub End Class ``` 我們使用`OpenDialog`控件打開 VB 源文件。 我們有一個`TextBox`控件,用于顯示文件。 ```vb Dim dia As New OpenFileDialog ``` `OpenDialog`已創建。 ```vb dia.Filter = "VB files (*.vb)|*.vb" ``` 我們將`Filter`屬性設置為 VB 源文件。 此對話框實例只能選擇 VB 文件。 ```vb If dia.ShowDialog(Me) = DialogResult.OK Dim reader As New StreamReader(dia.FileName) Dim data As String = reader.ReadToEnd reader.Close txtBox.Text = data End If ``` 單擊確定后,我們讀取所選文件的內容并將其放入`TextBox`控件。 ![OpenDialog](https://img.kancloud.cn/80/ca/80cad872372b9112b36880c58e2821f1_350x270.jpg) 圖:`OpenDialog` 在 Visual Basic Winforms 教程的這一部分中,我們顯示了各種對話框。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看