gpt4 book ai didi

c# - 处理多个表单之间的数据

转载 作者:可可西里 更新时间:2023-11-01 11:55:30 26 4
gpt4 key购买 nike

我正在开发一个生成 PDF 文件的程序。在最终生成文件之前,我想让用户可以选择编辑文件的一部分(即将创建的图形的标题)。当用户单击按钮导出 PDF 时,我希望它以新形式显示。这是我正在尝试做的事情的概述......

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!
}
}

截至目前,当我点击 Form2 中的按钮时,没有任何反应,我不理解这两个 Form 之间的通信!

最佳答案

您应该像下面这样更改您的 Form2.GraphName

public String GraphName
{
get { return textBox1.Text }
}

然后像下面这样更改你的新 Form2 创建,测试它,因为我没有通过 VS 运行它,但应该可以工作:)

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
}
}

关于c# - 处理多个表单之间的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18111457/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com