tags:

views:

177

answers:

5

Hi,

I'm using C#, how do I check if a checkbox is checked on a form from a class file?

It's a winform app.

Thanks Lee

A: 

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.

Yuriy Faktorovich
+1  A: 

CheckBox.Checked Property

Asad Butt
Thanks but I need to access this from a class file.
LeeW
A: 

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).

Timores
+1  A: 

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.

Austin Salonen
A: 

On the form create a public property that returns the checkbox's state, and then read that property.

Jeffrey L Whitledge
Assuming you have access to the Form inside the class.
Yuriy Faktorovich
@Yuriy Faktorovich - Indeed that is the assumption. I like to put the simplist answer that conforms to the constraints stated in the question. :-)
Jeffrey L Whitledge