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

前端 未结 5 680
广开言路
广开言路 2020-12-29 15:15

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

5条回答
  •  独厮守ぢ
    2020-12-29 15:45

    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.

提交回复
热议问题