Link to home
Start Free TrialLog in
Avatar of NoisyStudent
NoisyStudentFlag for United States of America

asked on

C# getting status of checkbox from a different class

Hi guys, here's my latest problem...

The form that starts with my application is named frmMain .. it has check boxes on it and I need to access the value of those check boxes and other controls from a different class.

In VB.NET it was really easy since all i'd have to do is .. frmMain.chkBox1.Checked

Is anybody able to provide me with an example please, i'd greatly appreciate it!
Avatar of Reza Rad
Reza Rad
Flag of New Zealand image

you can do this in c# as simple as vb.net
but only trick is that in c# controls are private by default
you must click on check box in your form, and in properties window set access modifier to Internal or Public
and build project again
then you can access through the code like this: frmMain.chkBox1.Checked
Avatar of NoisyStudent

ASKER

That actually didn't work for me, although upon more research I think i've solved my problem. I need to instantiate a new object of frmMain in my new class.. like this ..

private frmMain main = new frmMain();

now I can access chkBox1 through main.chkBox1.Checked

ASKER CERTIFIED SOLUTION
Avatar of Reza Rad
Reza Rad
Flag of New Zealand image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks for points,
Regards,
@reza_rad,

In VB.Net you don't actually manually instantiate the "startup object" as you do in C# inside program.cs.  Instead, the Application Framework starts your app for you, and passes what is known as the "default instance" of the form designated as the startup object to the message pump.  It behaves similar to the singleton pattern so you can access it form anywhere in the app using just the Form name.

To the author's requirement:

    "I need to access the value of those check boxes and other controls from a different class."

For that you would pass an instance of your form itself into the Class.  THEN your suggestion of changing the Modifiers property comes into play as you can't see the controls by default.  =)
@Idle_Mind
Thanks for your explanation, In fact I hadn't similar experience in this manner on vb.net. your comment was helpful, I appreciate it.
Regards,