[英]How to pass dynamic variables to another form?
I have two Forms Form1 and Form2, both of them are already "connected" with eachother.我有两个 Form1 和 Form2,它们都已经相互“连接”了。 I am already passing button signals, trackbar values, timer ticks..., between them.
我已经在它们之间传递按钮信号、轨迹栏值、计时器滴答声……。
The connections looks like this inside Form1: Form1 中的连接如下所示:
private void Form1_Load(object sender, EventArgs e)
{
Form2 = new Form2(timer1,btnBoost,btnBrake);
Form2.Show();
}
and inside Form2:在 Form2 中:
public Form2(Timer timer,Button Boost,Button Brake)
{
InitializeComponent();
_timer = timer;
_boost = Boost;
_brake = Brake;
}
Now I would like to pass a variable from Form1 which is changing it's value every timertick to Form2, to create a graph.现在我想从 Form1 传递一个变量,它会在每个 timetick 到 Form2 时更改它的值,以创建一个图形。
Inside Form1 it looks like this在 Form1 中,它看起来像这样
public partial class Form1 : Form {
public double ValueThatIWant;
}
Way done im giving it a value方式完成我给它一个价值
private void Timer1_Tick1(object sender, EventArgs e){
ValueThatIWant = Math.Sqrt(somevalue.X,somevalue.Y);
}
I've already tried to access the variable by calling Form1 from Form2我已经尝试通过从 Form2 调用 Form1 来访问变量
Form1.valueThatIWant
but since但由于
public double valueThatIWant
is declared public, it's value is always 0.被声明为 public,它的值总是 0。
private void FillChart()
{
this.chart1.Series["Velocity"].Points.AddXY(time,Form1.ValueThatIWant);
}
//That's the method I've created in Form2 to create a chart.
I would like to call the variable from inside(?) the我想从内部(?)调用变量
public Form1()
method, so that i get the changing value, and not only 0.方法,以便我获得不断变化的值,而不仅仅是 0。
Hope that kinda describes my problem.希望这能描述我的问题。
Thanks in advance!提前致谢!
You need access to the instance where you change the values.您需要访问更改值的实例。 In your code
Form2
does not know anything about that instance except btnBoost
and btnBreak
.在你的代码
Form2
不知道除了实例什么btnBoost
和btnBreak
。 You need to provide a reference to your Form1
-instance to your constructor of Form2
:您需要为
Form2
的构造函数提供对Form1
实例的引用:
public Form2(Timer timer, Form1 f1, Button Boost, Button Brake)
{
InitializeComponent();
_form1 = f1;
_timer = timer;
_boost = Boost;
_brake = Brake;
}
In your Form1
-code you now need this:在你的
Form1
代码中,你现在需要这个:
private void Form1_Load(object sender, EventArgs e)
{
Form2 = new Form2(timer1,this,btnBoost,btnBrake);
Form2.Show();
}
instead of:代替:
private void Form1_Load(object sender, EventArgs e)
{
Form2 = new Form2(timer1,btnBoost,btnBrake);
Form2.Show();
}
Now you can access _form1.ValueIWant
within Form2
.现在您可以在
Form2
访问_form1.ValueIWant
。
Furthermore you could also omit the two buttons from the constructor, as they probably are components of Form1
anyway.此外,您还可以从构造函数中省略这两个按钮,因为无论如何它们都可能是
Form1
组件。 In this case you need to make the appropriate fields within Form1
public
properties, however:在这种情况下,您需要在
Form1
public
属性中创建适当的字段,但是:
class Form1
{
public Button BreakButton => btnBreak;
}
in which way you can now use _form1.BreakButton
.您现在可以通过哪种方式使用
_form1.BreakButton
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.