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

How to access to Application windows controls ?

Hi experts,

how to access the main appclication form  controls ?

I don't like it doing it in the way below:
class newObject
        private control xy;

        public newObject(MainApplicationForm application)
            this.xy = application.control;

Is there another chance to access to the controls ?
1 Solution
If you want to access controls from any place from the program, expose them as properties from main form class. Make public static property with form reference and access controls using it.

class MainAplicationForm
    public TextBox TextBox1
           return this.textBox1;
    // ... add property for every control you want to expose

    static MainAplicationForm mainAplicationForm;

    public static MainAplicationForm MainForm
          return mainAplicationForm;

    public MainAplicationForm()
        mainAplicationForm = this;

To access textBox1 from any place of the program, assuming that main form is already created and there is only one instance of it:

MainAplicationForm.MainForm.TextBox1.Text = "...";
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.

Join & Write a Comment

Featured Post

Hire Technology Freelancers with Gigs

Work with 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.

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