如何以其他形式保存委托列表值 [英] How to save delegate list values in other form
问题描述
我有一个主要表单,当我点击按钮一个表单1时有按钮n和类似于单击2按钮时表单2
所以我能够将表单1中的值传递给表格2但是当表格1关闭时,价值会回到0
我尝试过:
公共委托void abc(list< string> p1)
表格2 f =新表格2();
Abc del = new Abc(f.Fpass);
Del(lst);
表格2
Public void Fpass(list< string> p2){pvalue。添加(p2.ToArray());
}
I have a main form which has buttons n when i click on button one form 1 and similar to it form 2 when 2 button is clicked
So I was able to pass value from form 1 to form 2 but when form 1 is closed values come back to 0
What I have tried:
Public delegate void abc(list<string > p1)
Form 2 f =new form 2();
Abc del =new Abc(f. Fpass) ;
Del(lst) ;
In Form 2
Public void Fpass( list<string > p2) {pvalue. Add(p2. ToArray() ) ;
}
推荐答案
上面是关于如何在Form的构造函数中传递变量的Google搜索链接...
或者,如果这种方法不合适,即:初始化后需要传递值,那么下面的例子就可以解决这个问题:
第二表格(接收方):
Above is a Google Search link on how to pass variables in the Form's constructor...
Alternatively, if this approach is not suitable, ie: values need to be passed after initialization, then the following example will do the trick:
Second Form (receiver):
using System.Windows.Forms;
namespace WFPassVariablesBetweenForms
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
public void SetValue(string text)
{
textBox1.Text = text;
}
}
}
主表格(发件人):
Main Form (Sender):
using System;
using System.Data;
using System.Linq;
using System.Windows.Forms;
namespace WFPassVariablesBetweenForms
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
var xxx = new Form2();
xxx.Show();
}
private void button1_Click(object sender, EventArgs e)
{
var xxx = Application.OpenForms.Cast<Form>().Where(x => x.Name == "Form2").FirstOrDefault();
((Form2)xxx).SetValue(textBox1.Text);
}
}
}
这个解决方案的危险在于它是紧密绑定的。可以针对松散依赖进行重构,但这不是本示例的目的。
The danger with this solution isthat it is tightly bound. Can be refactored for loose dependency but that is not the purpose of this example.
这篇关于如何以其他形式保存委托列表值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!