• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 320
  • Last Modified:

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!
0
NoisyStudent
Asked:
NoisyStudent
  • 4
1 Solution
 
Reza RadCommented:
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
 
Reza RadCommented:
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
NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

 
Reza RadCommented:
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 RadCommented:
@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

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now