原文: http://zetcode.com/gui/vbwinforms/controls/

Visual Basic Winforms 编程教程的这一部分将介绍基本控件。

Winforms 控件是应用的基本构建块。 Winforms 具有广泛的各种控件。 按钮,复选框,滑块,列表框等。程序员完成工作所需的一切。 在本教程的这一部分中,我们将描述几个有用的控件。

Label

Label是用于显示文本或图像的简单控件。 它没有得到关注。

  1. ' ZetCode Mono Visual Basic Winforms tutorial
  2. '
  3. ' This program shows lyrics of a song
  4. '
  5. ' author jan bodnar
  6. ' last modified May 2009
  7. ' website www.zetcode.com
  8. Imports System.Windows.Forms
  9. Imports System.Drawing
  10. Public Class WinVBApp
  11. Inherits Form
  12. Dim lyrics As String = "Meet you downstairs in the bar and heard" & vbNewLine & _
  13. "your rolled up sleeves and your skull t-shirt" & vbNewLine & _
  14. "You say why did you do it with him today?" & vbNewLine & _
  15. "and sniff me out like I was Tanqueray" & vbNewLine & _
  16. "" & vbNewLine & _
  17. "cause you're my fella, my guy" & vbNewLine & _
  18. "hand me your stella and fly" & vbNewLine & _
  19. "by the time I'm out the door" & vbNewLine & _
  20. "you tear men down like Roger Moore" & vbNewLine & _
  21. "" & vbNewLine & _
  22. "I cheated myself" & vbNewLine & _
  23. "like I knew I would" & vbNewLine & _
  24. "I told ya, I was trouble" & vbNewLine & _
  25. "you know that I'm no good"
  26. Public Sub New()
  27. Me.Text = "You know I'm no Good"
  28. Me.Size = New Size(300, 250)
  29. Me.InitUI
  30. Me.CenterToScreen
  31. End Sub
  32. Private Sub InitUI
  33. Dim font As New Font("Serif", 10)
  34. Dim label As New Label
  35. label.Parent = Me
  36. label.Text = lyrics
  37. label.Font = font
  38. label.Location = New Point(10, 10)
  39. label.Size = New Size (290, 290)
  40. End Sub
  41. Public Shared Sub Main
  42. Application.Run(New WinVBApp)
  43. End Sub
  44. End Class

在我们的示例中,我们在标签控件中显示歌曲的歌词。

  1. Dim lyrics As String = "Meet you downstairs in the bar and heard" & vbNewLine & _
  2. "your rolled up sleeves and your skull t-shirt" & vbNewLine & _
  3. ...

我们定义了多行文字。 与 C# ,Python 或 Ruby 不同,没有简单的结构可以用 Visual Basic 语言创建多行文本。 若要在 Visual Basic 中创建多行文本,我们使用vbNewLine打印常量,+连接字符和_行终止字符。

  1. Dim label As New Label

Label控件已创建。

  1. label.Text = lyrics

我们为标签设置文本。

  1. Dim font As New Font("Serif", 10)
  2. ...
  3. label.Font = font

标签文本的字体设置为 Serif,10px。

基本控制 - 图1

图:Label

CheckBox

CheckBox是具有两个状态的控件:开和关。 它是带有标签或图像的盒子。 如果选中了CheckBox,则在方框中用勾号表示。 CheckBox可用于在启动时显示/隐藏启动屏幕,切换工具栏的可见性等。

  1. ' ZetCode Mono Visual Basic Winforms tutorial
  2. '
  3. ' This program toggles the title of the
  4. ' window with the CheckBox control
  5. '
  6. ' author jan bodnar
  7. ' last modified May 2009
  8. ' website www.zetcode.com
  9. Imports System.Windows.Forms
  10. Imports System.Drawing
  11. Public Class WinVBApp
  12. Inherits Form
  13. Public Sub New
  14. Me.Text = "CheckBox"
  15. Me.Size = New Size(220, 170)
  16. Me.InitUI
  17. Me.CenterToScreen
  18. End Sub
  19. Private Sub InitUI
  20. Dim cb As New CheckBox
  21. cb.Parent = Me
  22. cb.Location = New Point(30, 30)
  23. cb.Text = "Show Title"
  24. cb.Checked = True
  25. AddHandler cb.CheckedChanged, AddressOf Me.OnChanged
  26. End Sub
  27. Private Sub OnChanged(ByVal sender As Object, ByVal e As EventArgs)
  28. If sender.Checked
  29. Text = "CheckBox"
  30. Else
  31. Text = ""
  32. End If
  33. End Sub
  34. Public Shared Sub Main
  35. Application.Run(New WinVBApp)
  36. End Sub
  37. End Class

我们的代码示例根据窗口的状态显示或隐藏窗口的标题。

  1. Dim cb As New CheckBox

CheckBox控件已创建。

  1. cb.Text = "Show Title"
  2. cb.Checked = True

当应用启动时,我们显示标题。 然后将CheckBox控件设置为选中状态。

  1. AddHandler cb.CheckedChanged, AddressOf Me.OnChanged

当我们单击CheckBox控件时,将触发CheckedChanged事件。 我们用OnChanged方法对这个特定事件做出反应。

  1. If sender.Checked
  2. Text = "CheckBox"
  3. Else
  4. Text = ""
  5. End If

在这里,我们切换窗口的标题。

基本控制 - 图2

图:CheckBox

ComboBox

ComboBox是一个组合了按钮或可编辑字段和下拉列表的控件。 用户可以从下拉列表中选择一个值,该列表应用户的要求出现。 如果使组合框可编辑,则组合框将包含一个可编辑字段,用户可以在其中输入值。

  1. ' ZetCode Mono Visual Basic Winforms tutorial
  2. '
  3. ' In this program, we use the ComboBox
  4. ' control to select an option.
  5. ' The selected option is shown in the
  6. ' Label component.
  7. '
  8. ' author jan bodnar
  9. ' last modified May 2009
  10. ' website www.zetcode.com
  11. Imports System.Windows.Forms
  12. Imports System.Drawing
  13. Public Class WinVBApp
  14. Inherits Form
  15. Private Dim label As Label
  16. Public Sub New
  17. Me.Text = "ComboBox"
  18. Me.Size = New Size(240, 240)
  19. Me.InitUI
  20. Me.CenterToScreen
  21. End Sub
  22. Private Sub InitUI
  23. Dim cb As New ComboBox
  24. cb.Parent = Me
  25. cb.Location = New Point(50, 30)
  26. cb.Items.AddRange(New Object() {"Ubuntu", _
  27. "Mandriva", _
  28. "Red Hat", _
  29. "Fedora", _
  30. "Gentoo"})
  31. label = New Label
  32. label.Location = New Point(50, 140)
  33. label.Parent = Me
  34. label.Text = "..."
  35. AddHandler cb.SelectionChangeCommitted, AddressOf Me.OnChanged
  36. End Sub
  37. Private Sub OnChanged(ByVal sender As Object, ByVal e As EventArgs)
  38. label.Text = sender.Text
  39. End Sub
  40. Public Shared Sub Main
  41. Application.Run(New WinVBApp)
  42. End Sub
  43. End Class

我们的代码编程示例显示了一个包含五个项目的组合框。 所选项目显示在标签控件中。

  1. Dim cb As New ComboBox

ComboBox控件已创建。

  1. cb.Items.AddRange(New Object() {"Ubuntu", _
  2. "Mandriva", _
  3. "Red Hat", _
  4. "Fedora", _
  5. "Gentoo"})

ComboBox控件中充满了项目。

  1. AddHandler cb.SelectionChangeCommitted, AddressOf Me.OnChanged

如果我们从组合框中选择一个项目,则会触发SelectionChangeCommitted事件。

  1. Private Sub OnChanged(ByVal sender As Object, ByVal e As EventArgs)
  2. label.Text = sender.Text
  3. End Sub

在这里,将从组合框中选择的文本复制到标签。

基本控制 - 图3

图:ComboBox

MonthCalendar

在下一个示例中,我们将显示MonthCalendar控件。 MonthCalendar 控件允许用户使用视觉显示选择日期。

  1. ' ZetCode Mono Visual Basic Winforms tutorial
  2. '
  3. ' In this program, we use the MonthCalendar
  4. ' control to select a date.
  5. ' The selected date is shown in the
  6. ' Label control.
  7. '
  8. ' author jan bodnar
  9. ' last modified May 2009
  10. ' website www.zetcode.com
  11. Imports System.Windows.Forms
  12. Imports System.Drawing
  13. Public Class WinVBApp
  14. Inherits Form
  15. Private Dim label As Label
  16. Public Sub New
  17. Me.Text = "MonthCalendar"
  18. Me.Size = New Size(240, 240)
  19. Me.InitUI
  20. Me.CenterToScreen
  21. End Sub
  22. Private Sub InitUI
  23. Dim calendar As New MonthCalendar
  24. calendar.Parent = Me
  25. calendar.Location = New Point(20, 20)
  26. label = New Label
  27. label.Location = New Point(40, 170)
  28. label.Parent = Me
  29. Dim dt As DateTime = calendar.SelectionStart
  30. label.Text = dt.Month & "/" & dt.Day & "/" & dt.Year
  31. AddHandler calendar.DateSelected, AddressOf Me.OnSel
  32. End Sub
  33. Private Sub OnSel(ByVal sender As Object, ByVal e As DateRangeEventArgs)
  34. Dim dt As DateTime = sender.SelectionStart
  35. label.Text = dt.Month & "/" & dt.Day & "/" & dt.Year
  36. End Sub
  37. Public Shared Sub Main
  38. Application.Run(New WinVBApp)
  39. End Sub
  40. End Class

在示例中,我们显示了MonthCalendarLabel。 后者显示当前选择的日期。

  1. Private Sub OnSel(ByVal sender As Object, ByVal e As DateRangeEventArgs)
  2. Dim dt As DateTime = sender.SelectionStart
  3. label.Text = dt.Month & "/" & dt.Day & "/" & dt.Year
  4. End Sub

当我们从MonthCalendar中选择一个日期时,就会调用OnSel方法。 SelectionStart属性获取所选日期范围的开始日期。

基本控制 - 图4

图:MonthCalendar

TextBox

TextBox控件用于显示或接受某些文本。 文本可以是单行或多行。 此控件还可以进行密码屏蔽。

  1. ' ZetCode Mono Visual Basic Winforms tutorial
  2. '
  3. ' This program demonstrates the
  4. ' TextBox control. Text entered in the TextBox
  5. ' control is shown in a Label control.
  6. '
  7. ' author jan bodnar
  8. ' last modified May 2009
  9. ' website www.zetcode.com
  10. Imports System.Windows.Forms
  11. Imports System.Drawing
  12. Public Class WinVBApp
  13. Inherits Form
  14. Private Dim label As Label
  15. Public Sub New
  16. Me.Text = "TextBox"
  17. Me.Size = New Size(250, 200)
  18. Me.InitUI
  19. Me.CenterToScreen
  20. End Sub
  21. Private Sub InitUI
  22. label = New Label
  23. label.Parent = Me
  24. label.Text = "..."
  25. label.Location = New Point(60, 40)
  26. label.AutoSize = True
  27. Dim tbox As New TextBox
  28. tbox.Parent = Me
  29. tbox.Location = New Point(60, 100)
  30. AddHandler tbox.KeyUp, AddressOf Me.OnKeyUp
  31. End Sub
  32. Private Sub OnKeyUp(ByVal sender As Object, ByVal e As KeyEventArgs)
  33. Me.label.Text = sender.Text
  34. End Sub
  35. Public Shared Sub Main
  36. Application.Run(New WinVBApp)
  37. End Sub
  38. End Class

本示例显示一个文本框和一个标签。 我们在文本框中键入的文本将立即显示在标签控件中。

  1. label = New Label
  2. ...
  3. label.AutoSize = True

Label控件已创建。 AutoSize属性确保Label增长以显示文本。

  1. Dim tbox As New TextBox
  2. ...
  3. AddHandler tbox.KeyUp, AddressOf Me.OnKeyUp

我们插入KeyUp事件。 释放按键时,将调用OnKeyUp方法。

  1. Private Sub OnKeyUp(ByVal sender As Object, ByVal e As KeyEventArgs)
  2. Me.label.Text = sender.Text
  3. End Sub

OnKeyUp方法中,我们使用文本框控件中的文本更新了标签控件。

基本控制 - 图5

图:TextBox

我们已经完成了 Visual Basic Winforms 教程的这一章,专门讨论基本控件。