C# 在两个WinForms之间传递的XPTable数据
我用C# 在两个WinForms之间传递的XPTable数据,c#,winforms,C#,Winforms,我用xptables创建了两个表单。单击form1中的按钮时,将显示form2。我在那里做一些手术。之后,结果将存储在form2的xptable中。单击form2中的“导出”按钮时,它应将form2xptable的数据传递给form1xptable,并保存在新的文本文件中。当我运行代码时,结果存储在文本文件中,但不存储在form1的xptable中 编辑:从form1调用form2: private void but_form2_Click(object sender, EventArgs e)
xptables
创建了两个表单。单击form1中的按钮时,将显示form2。我在那里做一些手术。之后,结果将存储在form2的xptable
中。单击form2中的“导出”按钮时,它应将form2xptable
的数据传递给form1xptable
,并保存在新的文本文件中。当我运行代码时,结果存储在文本文件中,但不存储在form1的xptable
中
编辑:从form1调用form2:
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();
}
。在form1编码中:
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);
}
有人能帮我吗?我需要改变什么
提前感谢…在这里,我为朋友们找到了答案
- 必须将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); })); } }
formf=Application.OpenForms[“Form1”]; 对于(int df=0;df
谢谢大家 在
btnExport\u中单击
方法,而不是创建新的Form1
对象,您应该使用现有对象
好的解决方案是使用事件
。
简单的解决方法是将Form1
作为参数传递给Form2
。代码:
添加到表单2
类:
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();
}
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");
}
(...)
}
在表格2中
更改方法:
private void but_form2_Click(object sender, EventArgs e)
{
Form2 tempForm = new Form2(this);
this.AddOwnedForm(tempForm);
tempForm.Show();
}
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");
}
(...)
}
Form2 h=新的Form2();h、 隐藏();h、 Close()@kavithai_kannan你能解释什么是xptable吗?@Avirasingh这里是了解xptable的链接:,