日期:2009-05-14  浏览次数:20439 次

  2.在窗体上放置五个radio button,并在窗体上增加一个label。

  3.双击一个radio button,会转到它的CheckedChanged事件处理程序,修改 Handles子句以包含其他四个的CheckedChanged事件。

Private Sub RadioButton1_CheckedChanged( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles RadioButton1.CheckedChanged, _
RadioButton2.CheckedChanged, _
RadioButton3.CheckedChanged, _
RadioButton4.CheckedChanged, _
RadioButton5.CheckedChanged
End Sub


  4.创建一个radio buttons的数组作为一个私有成员变量:Dim radioButtons(4) As RadioButton

  5.创建一个整形私有变量:Dim selectedOption As Integer = 0

  6.在窗体构造器代码中增加代码以把这些radio button填充到数组中

Public Sub New()
MyBase.New()
'This call is required by the
Windows Form Designer.
InitializeComponent()
'Add any initialization after
the InitializeComponent() call
radioButtons(0) = RadioButton1
radioButtons(1) = RadioButton2
radioButtons(2) = RadioButton3
radioButtons(3) = RadioButton4
radioButtons(4) = RadioButton5
End Sub


  7.在刚才的CheckedChanged程序中加入如下代码:

Private Sub RadioButton1_CheckedChanged( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles RadioButton1.CheckedChanged, _
RadioButton2.CheckedChanged, _
RadioButton3.CheckedChanged, _
RadioButton4.CheckedChanged, _
RadioButton5.CheckedChanged
Dim i As Integer = 0
Dim found As Boolean = False
While i < radioButtons.GetLength(0)
And Not found
If radioButtons(i).Checked Then
found = True
selectedOption = i + 1
Label1.Text = CStr(selectedOption)
End If
i += 1
End While
End Sub


  编译运行这个程序,你会发现每次选择不同的radio button的时候,在label中显示相应的序号。是不是和控件数组一样的效果?