XPTable数据在两个WinForms之间传递
本文关键字:WinForms 两个 之间 数据 XPTable | 更新日期: 2023-09-27 18:27:32
我用xptables
创建了两个表单。单击表格1中的按钮时,将显示表格2。我在那里做一些手术。之后的结果将存储在形式2的CCD_ 2中。当点击表单2中的导出按钮时,它应该将表单2 xptable
的数据传递给表单1 xptable
,并保存在新的文本文件中。当我运行代码时,结果存储在文本文件中,但不存储在form1的xptable
中。
编辑:从表单1:调用表单2
private void but_form2_Click(object sender, EventArgs e)
{
Form2 tempForm = new Form2();
this.AddOwnedForm(tempForm);
tempForm.Show();
}
这是我从form2导出的代码。
private void btnExport_Click(object sender, EventArgs e)
{
SaveFileDialog sfd = new SaveFileDialog();
sfd.Filter = "Save as txt (*.txt)|*.txt|All Files(*.*)|";
sfd.InitialDirectory = Application.StartupPath;
sfd.ShowDialog();
try
{
StreamWriter sw = new StreamWriter(new FileStream(sfd.FileName,
FileMode.OpenOrCreate,
FileAccess.Write));
Form1 obj = new Form1();
foreach (Row r in tblProxiesLive.Rows)
{
obj.loadsecondtable(r.Cells[1].Text);
sw.Write(r.Cells[1].Text + "'r'n");
}
sw.Close();
}
catch (Exception x)
{
string xp = x.ToString();
}
Form2 h = new Form2();
h.Hide();
h.Close();
}
形式1编码:
public void loadsecondtable(string s)
{
int snumber = this.tabproxmodel.Rows.Count + 1;
Row r = new Row();
r.Cells.Add(new Cell(snumber, Color.DarkBlue, Color.FromArgb(234, 215, 184), f2));
r.Cells.Add(new Cell(s, Color.FromArgb(225, 175, 91), Color.White, f2));
r.Cells.Add(new Cell("", (Image)new Bitmap(10, 10), Color.YellowGreen, Color.White, f2));
r.Cells.Add(new Cell("", (Image)new Bitmap(10, 10)));
r.Cells.Add(new Cell("", (Image)new Bitmap(10, 10)));
r.ForeColor = Color.FromArgb(6, 92, 155);
this.tabproxmodel.Rows.Add(r);
}
有人能帮我吗?我需要改变什么?
提前感谢。。。
在btnExport_Click
方法中创建新的Form1
对象时,您应该使用现有的对象。
好的解决方案是使用events
。简单的解决方案是将Form1
作为参数传递给Form2
。代码:
添加到Form2
类:
Form1 pointerToForm1;
public Form2(Form1 pointerToForm1) {
this.pointerToForm1 = pointerToForm1ł
}
在Form1
中更改方法:
private void but_form2_Click(object sender, EventArgs e)
{
Form2 tempForm = new Form2(this);
this.AddOwnedForm(tempForm);
tempForm.Show();
}
在Form2
中更改方法:
private void btnExport_Click(object sender, EventArgs e)
{
(...)
//Form1 obj = new Form1();
Form1 obj = pointerToForm1;
foreach (Row r in tblProxiesLive.Rows)
{
obj.loadsecondtable(r.Cells[1].Text);
sw.Write(r.Cells[1].Text + "'r'n");
}
(...)
}
这里我为这些朋友找到了答案。
-
必须将xptable修饰符从private更改为public形式2的编码如下:
Form f = Application.OpenForms["Form1"]; for (int df = 0; df < tblmodelform2.Rows.Count; df++) { int sNumber = ((Form1)f).tabmodelform1.Rows.Count; Row r = new Row(); //int ss = int.Parse(s); r.Cells.Add(new Cell(sNumber, Color.DarkBlue, Color.FromArgb(234, 215, 184), f2)); r.Cells.Add(new Cell(tblProxies22[df,1].Text, Color.FromArgb(225, 175, 91), Color.White, f2)); // r.Cells.Add(new Cell(pa, Color.FromArgb(225, 175, 91), Color.White, f2)); r.Cells.Add(new Cell("", (Image)new Bitmap(10, 10), Color.YellowGreen, Color.White, f2)); r.Cells.Add(new Cell("", (Image)new Bitmap(10, 10), Color.YellowGreen, Color.White, f2)); r.Cells.Add(new Cell("", (Image)new Bitmap(10, 10))); // r.Cells.Add(new Cell("", (Image)new Bitmap(10, 10))); if (!IsHandleCreated) { this.CreateControl(); ((Form1)f).tabmodelform1.Rows.Add(r); } else { this.Invoke(new MethodInvoker(delegate { ((Form1)f).tabmodelform1.Rows.Add(r); })); } }
此解决方案将适用于所有控件,而不仅仅是xptable。我们可以使用这个将listbox、datagridview项从一个表单传递到另一个表单。谢谢大家。。!