C# GUI program in Visual Studio 2005: can a user control use a public variable of its parent form?

Posted on 2009-02-21
Last Modified: 2013-12-17
Hi Experts:

    I am working on a Visual Studio 2005 C# project.
    I have a user defined control, which is used by a form (note that they are in different .CS files, but in the same name space). There is DataGridView inside the user control, and I wanted to let an event handler of this DataGridView read a public variable member of the parent form. However, that variable is not recogonized in the event handler.
    Does it mean that the scope of the parent form public variable does not include the event handlers of the user control?
    Thank you!
Question by:huangs3
    LVL 85

    Accepted Solution

    That's correct.  You would need to either:

    (1) Pass a reference to the Form INTO the UserControl.
    (2) Make the Form Variable a STATIC member so it can be accessed from anywhere.
    LVL 85

    Expert Comment

    by:Mike Tomlinson
    Obviously there are other options if you change your design...

    Author Comment

    changing the Form variable to STATIC doesn't help. I am thinking about alternative decide. Any idea?

    Author Comment

    I found STATIC variable in the FORM works now, and trying to find other alternative.
    Idle Mind already answered my question completely.

    Featured Post

    Gigs: Get Your Project Delivered by an Expert

    Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

    Join & Write a Comment

    A basic question.. “What is the Garbage Collector?” The usual answer given back: “Garbage collector is a background thread run by the CLR for freeing up the memory space used by the objects which are no longer used by the program.” I wondered …
    For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK ( for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
    This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
    The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…

    745 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

    17 Experts available now in Live!

    Get 1:1 Help Now