所以我有一个包含以下代码的处理程序:
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
的行为有点像单身人士:
将静态Form1
成员添加到Form1
:
public static Form1 instance;
将其设置在Form1
的构造函数中:
instance = this;
然后在您的代码中访问它:
Form1.instance