C# getting status of checkbox from a different class

Posted on 2009-12-19
Last Modified: 2012-05-08
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!
Question by:NoisyStudent
    LVL 30

    Expert Comment

    by:Reza Rad
    you can do this in c# as simple as
    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

    Author Comment

    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

    LVL 30

    Accepted Solution

    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

    LVL 30

    Expert Comment

    by:Reza Rad
    Thanks for points,
    LVL 85

    Expert Comment

    by:Mike Tomlinson

    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.  =)
    LVL 30

    Expert Comment

    by:Reza Rad
    Thanks for your explanation, In fact I hadn't similar experience in this manner on your comment was helpful, I appreciate it.


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
    Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now