[英]Fill created form components from another form in C#
i create a new form using the code below. 我使用下面的代码创建一个新表单。
private void CNPictureBox2_DoubleClick(object sender, EventArgs e)
{
RefImgForm RefImgForm = new RefImgForm();
RefImgForm.MainFrm = this;
RefImgForm.Show();
}
I want to send data from the form that i create second form. 我想从创建第二个表单的表单发送数据。 The problem is that i cannot send data to the new form when creating it. 问题是创建新表单时我无法将数据发送到新表单。 I want to send data when i take some data from user and then send this data by button click event. 我想从用户那里获取一些数据然后通过按钮单击事件发送此数据时发送数据。 How can i do that? 我怎样才能做到这一点?
Define a new method in second form 定义第二种形式的新方法
public void ReceiveData(....)
{
...
}
and call this from first form on button click 并在按钮单击时从第一个表单调用它
private RefImgForm frm2 = null;
private void CNPictureBox2_DoubleClick(object sender, EventArgs e)
{
frm2 = new RefImgForm();
frm2.MainFrm = this;
frm2.Show();
}
private void Button_Click(...)
{
if (frm2 != null)
frm2.ReceiveData(...);
}
Create a delegate in the parent form like this: 像这样在父表单中创建一个委托:
delegate void SendData(object data);
SendData sendDataobj;
make a method in the child form like ProcessData
and use the following code: 在子窗体中制作一个像ProcessData
这样的方法,并使用以下代码:
private void CNPictureBox2_DoubleClick(object sender, EventArgs e)
{
RefImgForm RefImgForm = new RefImgForm();
RefImgForm.MainFrm = this;
sendDataobj = new SendData(RefImgForm.ProcessData)
RefImgForm.Show();
}
for calling the delegate you can use: 用于呼叫代表,您可以使用:
sendDataobj(data);
sendDataobj.Invoke(data);
both are synchronous calls. 两者都是同步呼叫。
if you want to call it asynchronously you can use: 如果要异步调用它,可以使用:
sendDataobj.BeginInvoke
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.