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!
NoisyStudentAsked:
Who is Participating?
 
Reza RadConnect With a Mentor Consultant, TrainerCommented:
of course you need to instantiate of your form class at all, I thought that you did it! I assume that frmMain is an instance of your form.
however,your problem solved,Glad to hear it
Regards,

0
 
Reza RadConsultant, TrainerCommented:
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
0
 
NoisyStudentAuthor Commented:
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

0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
Reza RadConsultant, TrainerCommented:
Thanks for points,
Regards,
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
@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.  =)
0
 
Reza RadConsultant, TrainerCommented:
@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,

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.