[英]How do I set a value if a method is called from a child form?
My application currently has 2 forms. 我的申请目前有2种形式。 It creates a sub form, Form2, which ends with the following code: 它创建一个子窗体Form2,它以以下代码结尾:
public partial class Form2 : Form
{ ...
Form1 frm = new Form1();
frm.rglu = glu;
frm.rdate = fulldate;
frm.sort();
Close();
}
Note that form1 is just a couple of buttons at the moment. 请注意,form1目前仅是几个按钮。 One starts off Form2 as follows: 一个从Form2开始,如下所示:
private void button2_Click(object sender, EventArgs e)
{
using (Form2 AcqForm = new Form2())
{
AcqForm.ShowDialog(this);
}
}
No other code runs except a button test();
除按钮test();
外,没有其他代码运行test();
shown later). 稍后显示)。 This frm.sort();
这个frm.sort();
runs the following code found in Form1: 运行在Form1中找到的以下代码:
public partial class Form1 : Form
{
public void sort()
{
datelist = new List<DateTime>(rdate);
datelist.Sort((a, b) => a.CompareTo(b));
var result = rdate
.Select((d, i) => new { Date = d, Int = rglu[i] })
.OrderBy(o => o.Date)
.ToArray();
this.rdate = result.Select(o => o.Date).ToArray();
this.rglu = result.Select(o => o.Int).ToArray(); //all works fine
for (int i = 7; i+7 <= rglu.Length; i++)
{
Console.WriteLine(Convert.ToString(rdate[i]) + Convert.ToString(rglu[i]));
} //This shows values as expected
}
}
However when I set a button to run some more code using rglu
and rdate
the I get null pointer errors: 但是,当我设置一个按钮以使用rglu
和rdate
运行更多代码时,出现空指针错误:
public partial class Form1 : Form
{
private void test(object sender, EventArgs e)
{
for (int i = 7; i < rglu.Length; i++){} //rglu is null! The values are lost.
}
}
I believe the solution requires a int[] rglu {get; set;}
我相信解决方案需要int[] rglu {get; set;}
int[] rglu {get; set;}
method. int[] rglu {get; set;}
方法。 However so far I have been unsuccessful in using these things at all. 但是到目前为止,我根本没有使用这些东西。 Has anyone encountered this problem? 有没有人遇到这个问题?
Edit: rglu is defined like this: 编辑:rglu是这样定义的:
public int[] rglu { get; set; } //I don't get how this works though
Purely from a defensive coding style point of view I think it would be good practice to test for rglu
being null before calling methods on it. 纯粹从防御性编码风格的角度来看,我认为在对rglu
调用方法之前测试rglu
是否为null是一个好习惯。
eg 例如
public void test()
{
if(rglu == null)
{
throw new InvalidOperationException("rglu is null!");
}
for (int i = 7; i < rglu.Length; i++){} //rglu is not null!
}
I'd also question why test() needs to be public if it's called from a button click event. 我还要质疑,如果从按钮单击事件中调用test(),为什么需要将其公开。
Form1 frm = new Form1();
frm.rglu = glu;
frm.rdate = fulldate;
frm.sort();
Close();
You don't use frm
variable anywhere else and don't display the form, so garbage collectior is the only one who is going to receive that data in rdate
and rglu
variables in this case. 您不会在其他任何地方使用frm
变量,也不显示表单,因此,在这种情况下,垃圾收集器是唯一将使用rdate
和rglu
变量接收该数据的人。
It seems like you wanted to operate on already existing form. 似乎您想对已经存在的表单进行操作。 In this case you must pass a reference to existing Form1
to your method in Form2
. 在这种情况下,必须将对现有Form1
的引用传递给Form2
的方法。
Update: 更新:
It may look like this: 它可能看起来像这样:
public partial class Form2 : Form
{
// ...
private readonly Form1 _parent;
public Form2(Form1 parent) : this()
{
_parent = parent;
}
// ... somewhere in a method which closes Form2:
Form1 frm = _parent;
frm.rglu = glu;
frm.rdate = fulldate;
frm.sort();
Close();
// ...
}
To show Form2
from Form1
, use 要显示来自Form1
Form2
,请使用
using(var form2 = new Form2(this))
{
form2.ShowDialog(this);
}
Problem is in form2, You mustn't write 'Form1 frm = new Form1();' 问题出在form2中,您不能写“ Form1 frm = new Form1();” this code. 此代码。
in Form1 wite code like this: 在Form1中,这样的代码:
using System;
using System.Collections.Generic;
using System.Windows.Forms;
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
public int rglu;
public DateTime rdate;
public Form1()
{
InitializeComponent();
}
private void btnShowForm2_Click(object sender, EventArgs e)
{
Form2 frm2=new Form2();
frm2.Form1Instance = this;
frm2.Show();
}
public void sort()
{
datelist = new List<DateTime>(rdate);
datelist.Sort((a, b) => a.CompareTo(b));
var result = rdate
.Select((d, i) => new { Date = d, Int = rglu[i] })
.OrderBy(o => o.Date) // Sort by whatever field you want
.ToArray();
this.rdate = result.Select(o => o.Date).ToArray();
this.rglu = result.Select(o => o.Int).ToArray(); //all works fine
for (int i = 7; i + 7 <= rglu.Length; i++)
{
Console.WriteLine(Convert.ToString(rdate[i]) + Convert.ToString(rglu[i]));
} //This returns values as expected
}
}
}
in Form2 write codes like this: 在Form2中编写如下代码:
using System;
using System.Windows.Forms;
namespace WindowsFormsApplication2
{
public partial class Form2 : Form
{
public Form1 Form1Instance;
public Form2()
{
InitializeComponent();
}
private void btnSortValues_Click(object sender, EventArgs e)
{
Form1Instance.rglu = glu;
Form1Instance.rdate = fulldate;
Form1Instance.sort();
Close();
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.