取消选中 vb.net 复选框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22330987/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Unchecking vb.net checkboxes
提问by user3094822
I have a basic question about Vb.net.
我有一个关于 Vb.net 的基本问题。
I have a form, say "Form1" on which a checkbox "CheckBox1" triggers the pop-up of another form (say "Form2"). Form2 has a "cancel" button (say "Button1") which closes Form2 and unchecks "CheckBox1" on Form1.
我有一个表单,比如“Form1”,其中的复选框“CheckBox1”触发另一个表单的弹出窗口(比如“Form2”)。Form2 有一个“取消”按钮(比如“Button1”),它关闭 Form2 并取消选中 Form1 上的“CheckBox1”。
The Form2 "Cancel" button code is :
Form2“取消”按钮代码是:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Me.DialogResult = DialogResult.Cancel
Form1.CheckBox1.CheckState = 0
End Sub
It works, but I have a problem when trying to do the same with a third form ("Form3") triggered by another CheckBox1 on Form2, despite Form3 "Cancel" button is though coded the same way in Form3 :
它有效,但是当我尝试对由 Form2 上的另一个 CheckBox1 触发的第三个表单(“Form3”)执行相同操作时遇到问题,尽管 Form3 的“取消”按钮在 Form3 中的编码方式相同:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Me.DialogResult = DialogResult.Cancel
Form2.CheckBox1.CheckState = 0
End Sub
When I display MsgBox(Form2.CheckBox1.CheckState) in Form3 code, it's always "0", despite it's "1" when I display MsgBox(CheckBox1.CheckState) in Form2.
当我在 Form3 代码中显示 MsgBox(Form2.CheckBox1.CheckState) 时,它总是“0”,尽管我在 Form2 中显示 MsgBox(CheckBox1.CheckState) 时它是“1”。
Sorry for this noob question, but I can't see any explanation. Thanks in advance if anyone can help me.
很抱歉这个菜鸟问题,但我看不到任何解释。如果有人可以帮助我,请提前致谢。
采纳答案by Matt Wilko
Firstly, Switch Option Strict On, and change your CheckStateassignments to the correct Enumeration instead of 0 or 1.
首先,打开选项严格,并将您的CheckState分配更改为正确的枚举而不是 0 或 1。
Secondly, you should avoid setting the properties of controls on different forms.
其次,你应该避免在不同的表单上设置控件的属性。
You can use the return value from ShowDialogto determine whether the OK or Cancel button was clicked on a Form.
您可以使用 from 的返回值ShowDialog来确定是单击了表单上的 OK 还是 Cancel 按钮。
So what you would do is to specify the result value of the OK and Cancel buttons in Form2like this:
所以你要做的是在Form2 中指定 OK 和 Cancel 按钮的结果值,如下所示:
btnOK.DialogResult = Windows.Forms.DialogResult.OK
btnCancel.DialogResult = Windows.Forms.DialogResult.Cancel
Make sure you add the code in Form2to close the form when each of these are clicked:
确保在Form2 中添加代码以在单击其中每一个时关闭表单:
Private Sub btnCancel_Click(sender As Object, e As EventArgs) Handles btnCancel.Click
Me.Close()
End Sub
Private Sub btnOK_Click(sender As Object, e As EventArgs) Handles btnOK.Click
Me.Close()
End Sub
Then in Form1you call Form2.ShowDialog and inspect the DialogResult value to see which button was clicked:
然后在Form1 中调用 Form2.ShowDialog 并检查 DialogResult 值以查看单击了哪个按钮:
Dim result = Form2.ShowDialog
If result = Windows.Forms.DialogResult.OK Then
'ok button was clicked
CheckBox1.CheckState = CheckState.Checked
ElseIf result = Windows.Forms.DialogResult.Cancel Then
'cancel button was clicked
CheckBox1.CheckState = CheckState.Unchecked
End If
As an alternative, the Form1 code can be drastically simplified when you are happy with the logic to something like this:
作为替代方案,当您对这样的逻辑感到满意时,可以大大简化 Form1 代码:
CheckBox1.Checked = (Form2.ShowDialog = Windows.Forms.DialogResult.OK)

