复选框问题

时间:2015-04-17 12:53:32

标签: vb.net

我有一个程序可以选择与程序中的图像相关的选项,例如,对于每个复选框,有一个或两个投影仪的选项,并带有适当的图像。当勾选每个复选框时,我已经调用了这个子程序,但我想知道我是否可以检查哪些已在组框中勾选?只是想让这更有效率

Sub chkValid()
    If chkRadioMic.Checked = True Then
        picRadioMic.BorderStyle = BorderStyle.Fixed3D
    Else
        picRadioMic.BorderStyle = BorderStyle.None
    End If
    If chkFixedMic.Checked = True Then
        picFixedMic.BorderStyle = BorderStyle.Fixed3D
    Else
        picFixedMic.BorderStyle = BorderStyle.None
    End If
    If chkMediaPlayer.Checked = True Then
        picMedia.BorderStyle = BorderStyle.Fixed3D
    Else
        picMedia.BorderStyle = BorderStyle.None
    End If
    If chkLighting.Checked = True Then
        picLighting.BorderStyle = BorderStyle.Fixed3D
    Else
        picLighting.BorderStyle = BorderStyle.None
    End If
    If chkRemote.Checked = True Then
        picRemote.BorderStyle = BorderStyle.Fixed3D
    Else
        picRemote.BorderStyle = BorderStyle.None
    End If
End Sub

Private Sub chkRadioMic_CheckedChanged(sender As Object, e As EventArgs) Handles chkRadioMic.CheckedChanged
    chkValid()
End Sub

Private Sub chkFixedMic_CheckedChanged(sender As Object, e As EventArgs) Handles chkFixedMic.CheckedChanged
    chkValid()
End Sub

Private Sub chkMediaPlayer_CheckedChanged(sender As Object, e As EventArgs) Handles chkMediaPlayer.CheckedChanged
    chkValid()
End Sub

Private Sub chkLighting_CheckedChanged(sender As Object, e As EventArgs) Handles chkLighting.CheckedChanged
    chkValid()
End Sub

Private Sub chkRemote_CheckedChanged(sender As Object, e As EventArgs) Handles chkRemote.CheckedChanged
    chkValid()
End Sub

3 个答案:

答案 0 :(得分:1)

是的,但这不是要走的路。您可以使用多个Handles将控件分组到同一事件,从而提高效率。此方法也不需要辅助子例程。像这样:

Private Sub chk_CheckedChanged(sender As Object, e As EventArgs) Handles chkRadioMic.CheckedChanged, chkFixedMic.CheckedChanged, chkMediaPlayer.CheckedChanged, chkLighting.CheckedChanged, chkRemote.CheckedChanged
    If chkRadioMic.Checked = True Then
        picRadioMic.BorderStyle = BorderStyle.Fixed3D
    Else
        picRadioMic.BorderStyle = BorderStyle.None
    End If
    If chkFixedMic.Checked = True Then
        picFixedMic.BorderStyle = BorderStyle.Fixed3D
    Else
        picFixedMic.BorderStyle = BorderStyle.None
    End If
    If chkMediaPlayer.Checked = True Then
        picMedia.BorderStyle = BorderStyle.Fixed3D
    Else
        picMedia.BorderStyle = BorderStyle.None
    End If
    If chkLighting.Checked = True Then
        picLighting.BorderStyle = BorderStyle.Fixed3D
    Else
        picLighting.BorderStyle = BorderStyle.None
    End If
    If chkRemote.Checked = True Then
        picRemote.BorderStyle = BorderStyle.Fixed3D
    Else
        picRemote.BorderStyle = BorderStyle.None
    End If
End Sub

编辑:或者你可以尝试这种更时尚的方法,但我无法保证它的工作原理,因为我现在无法尝试:

Private Sub chk_CheckedChanged(sender As Object, e As EventArgs) Handles chkRadioMic.CheckedChanged, chkFixedMic.CheckedChanged, chkMediaPlayer.CheckedChanged, chkLighting.CheckedChanged, chkRemote.CheckedChanged
    Dim assPicBox as PictureBox = CType(("pic" & sender.Name.Remove(0,3)).ToString, PictureBox)
    If sender.Checked = True Then
        assPicBox.BorderStyle = BorderStyle.Fixed3D
    Else
        assPicBox.BorderStyle = BorderStyle.None
    End If
End Sub

答案 1 :(得分:0)

也许是这样的想法:

表单加载

chkRadioMic.tag= picRadioMic
chkFixedMic.tag= picFixedMic
chkMediaPlayer.tag= picMedia
chkLighting.tag= picLighting
chkRemote.tag= picRemote

新方法:

Sub chkValid(p_check as checkbox, p_picRadioMic as picturebox)
   If p_check.Checked = True Then
      p_picRadioMic.BorderStyle = BorderStyle.Fixed3D
   Else
      p_picRadioMic .BorderStyle = BorderStyle.None
   End If
End Sub

单一处理程序:

Private Sub chkRadioMic_CheckedChanged(sender As Object, e As EventArgs) Handles chkRadioMic.CheckedChanged, chkFixedMic.CheckedChanged, chkMediaPlayer.CheckedChanged,chkLighting.CheckedChanged, chkRemote.CheckedChanged
chkValid(ctype(sender, checkbox),ctype(ctype(sender, checkbox).tag, picturebox) )
End Sub

答案 2 :(得分:0)

就CPU周期而言,这不是更有效,但对你的眼睛而言:

Dim allCheckedCheckBoxes = From chk in GroupBox1.Controls.OfType(Of CheckBox)
                           Where chk.Checked
For Each chk As CheckBox in allCheckedCheckBoxes
 ' ... '
Next

如果是单CheckBox,您可以使用FirstFirstOrdefault

Dim checkedCheckBox As CheckBox = allCheckedCheckBoxes.FirstOrdefault()
If checkedCheckBox IsNot Nothing Then
  ' .. '
End if