How to access form objects from another cs file in C#

57,242

Solution 1

Select your button in designer, go to it's properties and change "Modifiers" property from Private to Public.

Then you can get access to it from another class, something like this:

public static class Test
{
    public static void DisalbeMyButton()
    {
        var form = Form.ActiveForm as Form1;

        if (form != null)
        {
            form.MyButton.Enabled = false;
        }
    }
}

Note: it's just an example and definitely not a pattern for good design :-)

Solution 2

There is Form object already instanced in Program.cs, except it have no reference. With simple editing you can turn

Application.Run(new Form1());

to

Application.Run(formInstance = new Form1());

declare it like

public static Form1 formInstance;

and use

Program.formInstance.MyFunction(params);

Solution 3

I worry whenever I hear someone talking about "another .cs file" or "another .vb file". It often (though not always) indicates a lack of understanding of programming, at least of OO programming. What's in the files? One class? Two?

You're not trying to access these things from another file, you're trying to access them from a method of a class, or possibly of a module in VB.

The answer to your question will depend on the nature of the class and method from which you're trying to access these things, and the reason why you want to access them.

Once you edit your question to include this information, the answers you receive will probably show you that you shouldn't be accessing these private pieces of the form in classes other than the form class itself.

Solution 4

Although I agree with John Saunders, one thing you may be doing wrong, assuming that you have everything accessible through public modifiers, is that you don't have the instance of that form.

For example, this is how you would do it:

Form1 myForm = new Form1;
string theButtonTextIAmLookingFor = myForm.MyButton.Text;

I am assuming that you may be trying to access it like it's static, like this:

string theButtonTextIAmLookingFor = Form1.MyButton.Text;

Just something you might want to check.

Share:
57,242
P. Duw
Author by

P. Duw

Updated on November 23, 2020

Comments

  • P. Duw
    P. Duw over 3 years

    In the form.cs file I have two buttons,a memo and a timer.My question is: How do I access the timer or the memo from another cs file?

    I've tried to make the objects public,but it didn't work,please give me a source or a project,so I can see where I'm mistaken.

    Thanks!

  • mmx
    mmx about 15 years
    throw new StackOverflowException(); ;)
  • P. Duw
    P. Duw about 15 years
    pastebin.com/m183c1bcf It throws an error,I have marked where the error is placed.Doesn't work.
  • tvanfosson
    tvanfosson about 15 years
    +1 I suspect the problem goes much deeper than just the visibility of the properties.
  • P. Duw
    P. Duw about 15 years
    You've edited your answer,I tried that already.When I try to write "Form1.MyButton" it doesnt recognize it.
  • Jon B
    Jon B about 15 years
    @John: please see my answer below. Forms in .NET don't work like forms in VB6.
  • Konstantin Tarkus
    Konstantin Tarkus about 15 years
    @John, try to recompile your app.
  • prabhakaran
    prabhakaran about 12 years
    Changing the "Modifiers" property from "private" to "public" is the key part in this answer.
  • Khainestar
    Khainestar almost 9 years
    I like this method, it makes the top level form always available. I was having problems with ActiveForm targeting the wrong form (Wrong from my codes point of view, right according to all conventional logic.)