如我的总结中所述,我目前正在VB.Net中开发虚拟操作系统。我正在处理会话,因为我完成了登录的工作。
我遇到一个带按钮外观的复选框。我想将CheckState设置为Checked,如果我点击带有Click()事件的按钮,如下所示:
Private Sub btnApps_Click(Byval sender As Object, Byval e As EventArgs) Handles btnApps.Click()
If btnApps.CheckState = CheckState.Checked Then
btnApps.CheckState = CheckState.Unchecked
Else
btnApps.CheckState = CheckState.Checked
End If
End Sub
我也尝试了Checked属性。
这段代码根本不起作用,如果我在CheckedChanged事件中放入整个If-End If部分,我得到一个StackOverflowException。我做错了什么?
CheckBox是一个自定义控件b.t.w。
答案 0 :(得分:1)
如果您想阻止Checkbox自动更改状态并使用您自己的Click事件更改外观,则可以将AutoCheck设置为false。
https://msdn.microsoft.com/en-us/library/system.windows.forms.checkbox.autocheck(v=vs.110).aspx
感谢此问题:How to cancel RadioButton or CheckBox checked change
Public Class Form1
Private WithEvents btnApps As New clsChk
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
btnApps.AutoCheck = False
Me.Controls.Add(btnApps)
End Sub
Private Sub btnApps_Click(sender As Object, e As EventArgs) Handles btnApps.Click
Debug.WriteLine(btnApps.CheckState)
If btnApps.CheckState = CheckState.Checked Then
btnApps.CheckState = CheckState.Unchecked
Else
btnApps.CheckState = CheckState.Checked
End If
End Sub
End Class