如何在静态方法中引用非静态对象

use*_*846 1 c# methods static textbox

我创建了一个文本框,想要在静态方法中引用它.我怎样才能做到这一点?在这里;我的代码

    private void Form1_Load(object sender, EventArgs e)
    {
        TextBox textbox2 = new TextBox();
        textbox2.Text = "A";
    }

    static void gettext() 
    {
        textbox2.Text = "B"; //here is my problem
    } 
Run Code Online (Sandbox Code Playgroud)

Llo*_*oyd 7

你需要以某种方式将它传递给静态方法,最简单的选择是只扩展方法签名以接受文本框:

static void gettext(TextBox textBox) 
{
    textBox.Text = "B"; //here is my problem
} 
Run Code Online (Sandbox Code Playgroud)