简体   繁体   中英

How to send string value from parent form to child (and vice versa)

This segment of code is where I am saving the contents of a textbox to a .txt file through the use of a SaveFileDialog . What I would like to do (as described in the comment in the code) is to get the full directory of where the .txt file is being saved and have it be saved as a string value in the childMDI form.

This is just to keep track which directory each childMDI is associated with as their content can be saved anywhere (given the use of the SaveFileDialog).

To elaborate more specifically:

I have two forms. Form1 and Form2. Form1 has a save function where it takes the content of a textbox in Form2 and saves it to file. However, I can create multiple instances of the Form2 type. I want to be able to keep track of where these files are saved inside the program by saving the directory and path chosen when saving the txt file and have it associated with the specific instance of Form2 (my idea was to have a string "directory" in Form2 where this value could be stored). I know how to get the directory as a string, I just don't know how to pass this to from Form1 to Form2.

Form activeChild = this.ActiveMdiChild;
        TextBox txtBox = (TextBox)activeChild.ActiveControl;

        if (activeChild.Text == "untitled")
        {
            SaveFileDialog saveFileDialog1 = new SaveFileDialog();
            saveFileDialog1.Filter = "txt files (*.txt)|*.txt";
            saveFileDialog1.Title = "Save your Text File";
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                using (StreamWriter writeFile = new StreamWriter(saveFileDialog1.FileName))
                {
                    writeFile.Write(txtBox.Text);
                    writeFile.Close();
                }
            }
            this.Text = Path.GetFileName(saveFileDialog1.FileName);
            activeChild.Text = Path.GetFileNameWithoutExtension(saveFileDialog1.FileName);

            // set directory of activeChild
        }

Right, I currently have it as a property in Form2 (string directory)

Okay, so where you have "//set directory of activeChild", you'll want to cast to type Form2 so you can access that property:

if (this.ActiveMdiChild is Form2)
{
    Form2 f2 = (Form2)this.ActiveMdiChild;
    f2.Directory = System.IO.Path.GetDirectoryName(saveFileDialog1.FileName);
}

Have you tried this ?

    if (activeChild.Text == "untitled")
            {
                SaveFileDialog saveFileDialog1 = new SaveFileDialog();
                saveFileDialog1.Filter = "txt files (*.txt)|*.txt";
                saveFileDialog1.Title = "Save your Text File";
                if (saveFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    using (StreamWriter writeFile = new StreamWriter(saveFileDialog1.FileName))
                    {
                        writeFile.Write(txtBox.Text);
                        writeFile.Close();
                    }
                    this.Text = Path.GetFileName(saveFileDialog1.FileName);
                    activeChild.Text = Path.GetFileNameWithoutExtension(saveFileDialog1.FileName);
                }


                // set directory of activeChild
            }

Putting the this.Text = Path.GetFileName(saveFileDialog1.FileName); and activeChild.Text = Path.GetFileNameWithoutExtension(saveFileDialog1.FileName); inside the saveFileDialog1 if statement so that after you click the ok Dialog the this.Text will have a new value on it.

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