简体   繁体   English

处理多个表单之间的数据

[英]Handling data between multiple Forms

I am working on a program that generates a PDF file. 我正在研究一个生成PDF文件的程序。 Before the final generation of the file, I want to give the user the option to edit a portion of the file (The title of the graph that is about to be created). 在最终生成文件之前,我想给用户一个选项来编辑文件的一部分(即将创建的图形的标题)。 I want this to show up in a new form when the user clicks a button to export the PDF. 我希望当用户单击按钮以导出PDF时以新的形式显示。 Here is an outline of what I am trying to do... 这是我要做什么的概述...

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

And here is the Form that is being opened by this button click... 这是通过此按钮单击打开的窗体。

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

As of now, when I click on the button in Form2, nothing happens, there is something about the communication between the two Forms that I am not understanding! 到目前为止,当我单击Form2中的按钮时,什么都没有发生,这是我不了解的关于两个Forms之间通信的信息!

The answer to your problem is quite simple. 您问题的答案很简单。

NewPDF.Show();

Show() does not pause execution of the calling form. Show()不会暂停调用表单的执行。 Therefore, the check underneath that that verifies the Selected property if true will never execute properly, since that check is reached and verified just as the form starts appearing. 因此,如果选择的属性为true,则验证其下方的检查将永远不会正确执行,因为在表单开始显示时,已达到并验证了该检查。 ShowDialog() does pause execution and waits for the called form to close. ShowDialog()会暂停执行并等待调用的窗体关闭。

That aside; 那边 I would recommend one of two other ways to communicate between forms; 我建议使用两种其他方式在表单之间进行通信;

  1. Use a global variable. 使用全局变量。 Declare a variable holding the graph's name somewhere in a public module. 在公共模块中的某个地方声明一个保存图形名称的变量。 Call the dialog that asks the user to input a name with ShowDialog(), since that pauses execution of the calling form until the called form returns a result. 调用要求用户使用ShowDialog()输入名称的对话框,因为这会暂停执行调用表单,直到被调用表单返回结果为止。

     if(Form.ShowDialog() == DialogResult.OK) { // Save pdf, using title in global variable } 

    Make sure to set the DialogResult in the called form before Close()-ing it. 确保在Close()-ing之前以调用的形式设置DialogResult。

  2. Pass an instance variable of the calling form to the called name-input form to the constructor and save it. 将调用表单的实例变量传递给构造函数的被叫名称输入表单并保存。 That way, if you expose the graph name property as a public property, you should be able to access it from the called form in the code that closes the form, which is your: 这样,如果将图形名称属性公开为公共属性,则应该能够从关闭表单的代码中的被调用表单访问它,即:

      public void button1_Click(object sender, EventArgs e) { callingFormInstance.GraphNameProperty = textBox1.Text; Close(); } 

Hope that helps. 希望能有所帮助。 Cheers! 干杯!

You should change your Form2.GraphName like below 您应该像下面那样更改Form2.GraphName

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

then change your new Form2 creation like below, test it since I haven't run this through VS, but should work :) 然后更改您的新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
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM