如何根据从Form1中的Serialport收到的值更改Form2中的变量的值? [英] How Do I Change The Value Of A Variable In Form2 According To The Value Received From Serialport In Form1?
问题描述
嗨...
我有一个疑问。我认为我在form1中声明了一个变量,并且将从串口接收该变量的值。我也希望在form2中访问此变量。如果form1中的变量值为1,则form2中的变量也具有值1。如果form1从串口接收值2,则两种形式的变量值必须为'2'。是否可能?我如何实现这个?
Hi...
I have one doubt. consider I declare a variable in form1,and value of this variable will be received from serial port. And I want to access this variable in form2 also. If the value of variable in form1 is '1', variable in form2 also have the value '1'. If form1 receives value 2 from serial port, the value of the variable in both form must be '2'.Is it possible? How can I implement this?
推荐答案
一种方法是在Form2中创建Form1可以调用的方法,假设Form1创建Form2。
在Form2中:
One way is to create a method in Form2 that Form1 can call, assuming that Form1 creates Form2.
In Form2:
public void SerialPortValueUpdated(int value)
{
textBox1.Text = value.ToString();
}
当串口激活事件并处理传入的字符时,然后调用方法在Form2中。
When the serial port fires the event, and you take care of the incoming characters, you then call the method in Form2.
form2.SerialPortValueUpdated(2);
还有其他各种方法可以做到这一点,但这取决于您当前的实施方法,最佳方法是什么。
There are various other methods to do it, but it depends on your current implementation what is the best approach.
这篇关于如何根据从Form1中的Serialport收到的值更改Form2中的变量的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!