<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>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                # 基本控制 > 原文: [http://zetcode.com/gui/vbwinforms/controls/](http://zetcode.com/gui/vbwinforms/controls/) Visual Basic Winforms 編程教程的這一部分將介紹基本控件。 Winforms 控件是應用的基本構建塊。 Winforms 具有廣泛的各種控件。 按鈕,復選框,滑塊,列表框等。程序員完成工作所需的一切。 在本教程的這一部分中,我們將描述幾個有用的控件。 ## `Label` `Label`是用于顯示文本或圖像的簡單控件。 它沒有得到關注。 ```vb ' ZetCode Mono Visual Basic Winforms tutorial ' ' This program shows lyrics of a song ' ' author jan bodnar ' last modified May 2009 ' website www.zetcode.com Imports System.Windows.Forms Imports System.Drawing Public Class WinVBApp Inherits Form Dim lyrics As String = "Meet you downstairs in the bar and heard" & vbNewLine & _ "your rolled up sleeves and your skull t-shirt" & vbNewLine & _ "You say why did you do it with him today?" & vbNewLine & _ "and sniff me out like I was Tanqueray" & vbNewLine & _ "" & vbNewLine & _ "cause you're my fella, my guy" & vbNewLine & _ "hand me your stella and fly" & vbNewLine & _ "by the time I'm out the door" & vbNewLine & _ "you tear men down like Roger Moore" & vbNewLine & _ "" & vbNewLine & _ "I cheated myself" & vbNewLine & _ "like I knew I would" & vbNewLine & _ "I told ya, I was trouble" & vbNewLine & _ "you know that I'm no good" Public Sub New() Me.Text = "You know I'm no Good" Me.Size = New Size(300, 250) Me.InitUI Me.CenterToScreen End Sub Private Sub InitUI Dim font As New Font("Serif", 10) Dim label As New Label label.Parent = Me label.Text = lyrics label.Font = font label.Location = New Point(10, 10) label.Size = New Size (290, 290) End Sub Public Shared Sub Main Application.Run(New WinVBApp) End Sub End Class ``` 在我們的示例中,我們在標簽控件中顯示歌曲的歌詞。 ```vb Dim lyrics As String = "Meet you downstairs in the bar and heard" & vbNewLine & _ "your rolled up sleeves and your skull t-shirt" & vbNewLine & _ ... ``` 我們定義了多行文字。 與 C# ,Python 或 Ruby 不同,沒有簡單的結構可以用 Visual Basic 語言創建多行文本。 若要在 Visual Basic 中創建多行文本,我們使用`vbNewLine`打印常量,`+`連接字符和`_`行終止字符。 ```vb Dim label As New Label ``` `Label`控件已創建。 ```vb label.Text = lyrics ``` 我們為標簽設置文本。 ```vb Dim font As New Font("Serif", 10) ... label.Font = font ``` 標簽文本的字體設置為 Serif,10px。 ![Label](https://img.kancloud.cn/d4/dd/d4dd1235756d77c2cfe261e24ddf4ad8_300x251.jpg) 圖:`Label` ## `CheckBox` `CheckBox`是具有兩個狀態的控件:開和關。 它是帶有標簽或圖像的盒子。 如果選中了`CheckBox`,則在方框中用勾號表示。 `CheckBox`可用于在啟動時顯示/隱藏啟動屏幕,切換工具欄的可見性等。 ```vb ' ZetCode Mono Visual Basic Winforms tutorial ' ' This program toggles the title of the ' window with the CheckBox control ' ' author jan bodnar ' last modified May 2009 ' website www.zetcode.com Imports System.Windows.Forms Imports System.Drawing Public Class WinVBApp Inherits Form Public Sub New Me.Text = "CheckBox" Me.Size = New Size(220, 170) Me.InitUI Me.CenterToScreen End Sub Private Sub InitUI Dim cb As New CheckBox cb.Parent = Me cb.Location = New Point(30, 30) cb.Text = "Show Title" cb.Checked = True AddHandler cb.CheckedChanged, AddressOf Me.OnChanged End Sub Private Sub OnChanged(ByVal sender As Object, ByVal e As EventArgs) If sender.Checked Text = "CheckBox" Else Text = "" End If End Sub Public Shared Sub Main Application.Run(New WinVBApp) End Sub End Class ``` 我們的代碼示例根據窗口的狀態顯示或隱藏窗口的標題。 ```vb Dim cb As New CheckBox ``` `CheckBox`控件已創建。 ```vb cb.Text = "Show Title" cb.Checked = True ``` 當應用啟動時,我們顯示標題。 然后將`CheckBox`控件設置為選中狀態。 ```vb AddHandler cb.CheckedChanged, AddressOf Me.OnChanged ``` 當我們單擊`CheckBox`控件時,將觸發`CheckedChanged`事件。 我們用`OnChanged`方法對這個特定事件做出反應。 ```vb If sender.Checked Text = "CheckBox" Else Text = "" End If ``` 在這里,我們切換窗口的標題。 ![CheckBox](https://img.kancloud.cn/9c/03/9c03fff56b27f629944c297bbd1a33ed_220x171.jpg) 圖:`CheckBox` ## `ComboBox` `ComboBox`是一個組合了按鈕或可編輯字段和下拉列表的控件。 用戶可以從下拉列表中選擇一個值,該列表應用戶的要求出現。 如果使組合框可編輯,則組合框將包含一個可編輯字段,用戶可以在其中輸入值。 ```vb ' ZetCode Mono Visual Basic Winforms tutorial ' ' In this program, we use the ComboBox ' control to select an option. ' The selected option is shown in the ' Label component. ' ' 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 label As Label Public Sub New Me.Text = "ComboBox" Me.Size = New Size(240, 240) Me.InitUI Me.CenterToScreen End Sub Private Sub InitUI Dim cb As New ComboBox cb.Parent = Me cb.Location = New Point(50, 30) cb.Items.AddRange(New Object() {"Ubuntu", _ "Mandriva", _ "Red Hat", _ "Fedora", _ "Gentoo"}) label = New Label label.Location = New Point(50, 140) label.Parent = Me label.Text = "..." AddHandler cb.SelectionChangeCommitted, AddressOf Me.OnChanged End Sub Private Sub OnChanged(ByVal sender As Object, ByVal e As EventArgs) label.Text = sender.Text End Sub Public Shared Sub Main Application.Run(New WinVBApp) End Sub End Class ``` 我們的代碼編程示例顯示了一個包含五個項目的組合框。 所選項目顯示在標簽控件中。 ```vb Dim cb As New ComboBox ``` `ComboBox`控件已創建。 ```vb cb.Items.AddRange(New Object() {"Ubuntu", _ "Mandriva", _ "Red Hat", _ "Fedora", _ "Gentoo"}) ``` `ComboBox`控件中充滿了項目。 ```vb AddHandler cb.SelectionChangeCommitted, AddressOf Me.OnChanged ``` 如果我們從組合框中選擇一個項目,則會觸發`SelectionChangeCommitted`事件。 ```vb Private Sub OnChanged(ByVal sender As Object, ByVal e As EventArgs) label.Text = sender.Text End Sub ``` 在這里,將從組合框中選擇的文本復制到標簽。 ![ComboBox](https://img.kancloud.cn/de/c5/dec54b9d7a99aee63e1d0fa9dd974c23_240x241.jpg) 圖:`ComboBox` ## `MonthCalendar` 在下一個示例中,我們將顯示`MonthCalendar`控件。 MonthCalendar 控件允許用戶使用視覺顯示選擇日期。 ```vb ' ZetCode Mono Visual Basic Winforms tutorial ' ' In this program, we use the MonthCalendar ' control to select a date. ' The selected date is shown in the ' Label control. ' ' 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 label As Label Public Sub New Me.Text = "MonthCalendar" Me.Size = New Size(240, 240) Me.InitUI Me.CenterToScreen End Sub Private Sub InitUI Dim calendar As New MonthCalendar calendar.Parent = Me calendar.Location = New Point(20, 20) label = New Label label.Location = New Point(40, 170) label.Parent = Me Dim dt As DateTime = calendar.SelectionStart label.Text = dt.Month & "/" & dt.Day & "/" & dt.Year AddHandler calendar.DateSelected, AddressOf Me.OnSel End Sub Private Sub OnSel(ByVal sender As Object, ByVal e As DateRangeEventArgs) Dim dt As DateTime = sender.SelectionStart label.Text = dt.Month & "/" & dt.Day & "/" & dt.Year End Sub Public Shared Sub Main Application.Run(New WinVBApp) End Sub End Class ``` 在示例中,我們顯示了`MonthCalendar`和`Label`。 后者顯示當前選擇的日期。 ```vb Private Sub OnSel(ByVal sender As Object, ByVal e As DateRangeEventArgs) Dim dt As DateTime = sender.SelectionStart label.Text = dt.Month & "/" & dt.Day & "/" & dt.Year End Sub ``` 當我們從`MonthCalendar`中選擇一個日期時,就會調用`OnSel`方法。 `SelectionStart`屬性獲取所選日期范圍的開始日期。 ![MonthCalendar](https://img.kancloud.cn/04/4b/044b53ac42218a6fe9b384a5760a27ba_240x241.jpg) 圖:`MonthCalendar` ## `TextBox` `TextBox`控件用于顯示或接受某些文本。 文本可以是單行或多行。 此控件還可以進行密碼屏蔽。 ```vb ' ZetCode Mono Visual Basic Winforms tutorial ' ' This program demonstrates the ' TextBox control. Text entered in the TextBox ' control is shown in a Label control. ' ' 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 label As Label Public Sub New Me.Text = "TextBox" Me.Size = New Size(250, 200) Me.InitUI Me.CenterToScreen End Sub Private Sub InitUI label = New Label label.Parent = Me label.Text = "..." label.Location = New Point(60, 40) label.AutoSize = True Dim tbox As New TextBox tbox.Parent = Me tbox.Location = New Point(60, 100) AddHandler tbox.KeyUp, AddressOf Me.OnKeyUp End Sub Private Sub OnKeyUp(ByVal sender As Object, ByVal e As KeyEventArgs) Me.label.Text = sender.Text End Sub Public Shared Sub Main Application.Run(New WinVBApp) End Sub End Class ``` 本示例顯示一個文本框和一個標簽。 我們在文本框中鍵入的文本將立即顯示在標簽控件中。 ```vb label = New Label ... label.AutoSize = True ``` `Label`控件已創建。 `AutoSize`屬性確保`Label`增長以顯示文本。 ```vb Dim tbox As New TextBox ... AddHandler tbox.KeyUp, AddressOf Me.OnKeyUp ``` 我們插入`KeyUp`事件。 釋放按鍵時,將調用`OnKeyUp`方法。 ```vb Private Sub OnKeyUp(ByVal sender As Object, ByVal e As KeyEventArgs) Me.label.Text = sender.Text End Sub ``` 在`OnKeyUp`方法中,我們使用文本框控件中的文本更新了標簽控件。 ![TextBox](https://img.kancloud.cn/e6/59/e65955cb4ffa7ac9d49a557710d6e25c_250x201.jpg) 圖:`TextBox` 我們已經完成了 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>

                              哎呀哎呀视频在线观看