如何将我的 Form 1 从变量分配给 existing/loaded 表单对象?

How to assign my Form1 frm variable to the existing/loaded form object?

所以我有一个包含以下代码的处理程序:

    private static void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e)
    {
        SerialPort sp = (SerialPort)sender;
        Form1 frm = //want to set 'frm' to the existing, instantiated form1 already running.
        string indata = sp.ReadExisting(); //stores the char that fired the event into 'indata'
        if (indata == "\r") //check to see if char received indicates end of measurement, yes tells main form to add measurement, no tells to add char to string
        {
            frm.pendingMeasurement = true;
            MessageBox.Show(myString);
        }
        else
            myString += indata;
    }

第 4 行我正在创建一个 form1 对象,我想将它设置为现有的、已经 运行 的 form1 对象。我如何在语法方面访问该对象?

使 Form1 的行为有点像 Singleton:

添加静态 Form1 成员到 Form1:

public static Form1 instance;

Form1的构造函数中设置:

instance = this;

然后像这样在您的代码中访问它:

Form1.instance