简体   繁体   中英

Handling data between multiple Forms

I am working on a program that generates a PDF file. 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. 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!

The answer to your problem is quite simple.

NewPDF.Show();

Show() does not pause execution of the calling form. 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. ShowDialog() does pause execution and waits for the called form to close.

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.

     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.

  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

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 :)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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