C# 处理多个表单之间的数据
我正在开发一个生成PDF文件的程序。在最终生成文件之前,我想让用户选择编辑文件的一部分(即将创建的图形的标题)。我希望当用户单击按钮导出PDF时,它以新的形式显示。这里是我正在尝试做的一个概要C# 处理多个表单之间的数据,c#,.net,windows,winforms,C#,.net,Windows,Winforms,我正在开发一个生成PDF文件的程序。在最终生成文件之前,我想让用户选择编辑文件的一部分(即将创建的图形的标题)。我希望当用户单击按钮导出PDF时,它以新的形式显示。这里是我正在尝试做的一个概要 private void button4_Click(object sender, EventArgs e) // Test PDF Code! { Form2 NewPDF = new Form2(chart3.Titles[chart3.Titles.IndexOf("Header")].Te
private void button4_Click(object sender, EventArgs e) // Test PDF Code!
{
Form2 NewPDF = new Form2(chart3.Titles[chart3.Titles.IndexOf("Header")].Text.ToString().Substring(0, chart3.Titles[chart3.Titles.IndexOf("Header")].Text.ToString().Length - 4));
NewPDF.Show();
if (NewPDF.Selected == true)
{
// Create PDF, open save file dialog, etc
}
}
这是通过这个按钮打开的表单单击
public partial class Form2 : Form
{
public bool Selected
{
get;
set;
}
public String GraphName
{
get;
set;
}
public Form2(String FileName)
{
InitializeComponent();
textBox1.Text = FileName;
GraphName = FileName;
Selected = false;
}
public void button1_Click(object sender, EventArgs e)
{
GraphName = textBox1.Text;
this.Selected = true; // After the button is selected I want the code written above to continue execution, however it does not!
}
}
到目前为止,当我点击表单2中的按钮时,什么都没有发生,这两个表单之间的通信有一些我不理解的地方 你的问题的答案很简单
NewPDF.Show();
Show()不会暂停调用窗体的执行。因此,下面验证所选属性(如果为true)的检查将永远不会正确执行,因为该检查是在表单开始出现时到达并验证的。ShowDialog()暂停执行并等待被调用表单关闭
除此之外;我推荐另外两种在表单之间进行通信的方法之一
if(Form.ShowDialog() == DialogResult.OK) {
// Save pdf, using title in global variable
}
在关闭()对话框之前,请确保在调用的窗体中设置该对话框结果 public void button1_Click(object sender, EventArgs e)
{
callingFormInstance.GraphNameProperty = textBox1.Text;
Close();
}
希望有帮助。干杯 你应该改变你的
格式2.图名如下
public String GraphName
{
get { return textBox1.Text }
}
然后像下面这样更改您的新Form2创建,测试它,因为我没有通过VS运行它,但应该可以工作:)
与论坛网站不同,我们不使用“感谢”或“感谢任何帮助”或签名。请参阅“.是否要使用NewPDF.ShowDialog()而不是NewPDF.Show()?如果使用ShowDialog(),ShowDialog()之后的代码将停止执行,直到关闭Form2。可能存在重复的
private void button4_Click(object sender, EventArgs e) // Test PDF Code!
{
// why on earth were you doing .Text.ToString()? it's already string...
Form2 NewPDF = new Form2(chart3.Titles[chart3.Titles.IndexOf("Header")].Text.Substring(0, chart3.Titles[chart3.Titles.IndexOf("Header")].Text.Length - 4));
// show as a dialog form, so it will wait for it to exit, and set this form as parent
NewPDF.ShowDialog(this);
if (NewPDF.Selected == true)
{
// get the name from the other form
string fileName = NewPDF.GraphName;
// Create PDF, open save file dialog, etc
}
}