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

标签 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")].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/

相关文章:

c++ - 谁能解释为什么这两个转换为 unsigned long long 会产生不同的结果?

c# - 在类型上找不到事件

c# - 如何处理文件名

.net - 将 Observable<Timestamp<T>> 转换为 Observable<Time Interval<T>>

c++ - Windows GDI 上下文 - 函数失败并且 GetLastError() 返回 0

node.js - 如何修复( Node :12388) [DEP0066] DeprecationWarning: OutgoingMessage. prototype._headers 在 Windows 中已弃用

c# - 使用自动映射器从 HttpPostedFileBase 映射到 Byte[]

C# 获取 SelectedDate.Day 字符串值并将其绑定(bind)到 gridview

c# - 正则表达式可以从字符串中提取多个数字吗?

c# - 为什么 var 评估为 int 而不是 double?