How do I check if a checkbox is checked on a form from a class file?
I'm using C#, how do I check if a checkbox is checked on a form from a class file?
It's a winform ap开发者_开发问答p.
CheckBox.Checked Property
This sounds like you are going down the road of tight coupling and that's something you should avoid. Your worker class should not directly talk to objects on the form.
You can send the Checked
value to the worker class when you initialize it or as a method parameter if you are calling it from the form.
If you are sending the form to the worker class, you should provide a public property on the form that returns the appropriate state of the CheckBox and access that property in your worker class.
You can also make your CheckBox itself public or internal on the form by changing the Modifiers value in the Designer. I don't recommend this though.
On the form create a public property that returns the checkbox's state, and then read that property.
You need to somehow gain a reference to that CheckBox from inside your class. Or you can have the class hook into an event that signifies that the CheckBox has changed state. For example, the class constructor could take a CheckBox, and when you instantiate the class you pass the correct CheckBox, then the class could check the CheckBox.Checked property at any time.
Assuming WinForms or WPF, the designer/VS has generated a member variable that represens your checkbox. In WinForms, there is the Checked property, in WPF, the IsChecked property (and the XAML requires the x:Name attribute).
If My.Forms.Form1.CheckBox1.Checked = True Then
'ur code
End If
精彩评论