简体   繁体   中英

Saving form2 from form1 button

I currently have a form called form1 that allows me to create another form called form2, form2 has a textbox that I can input text into. On form1 I have a save button to save the text in form2 as a .txt file. I am currently having issues with one of my last steps where my current method does not exist and I am not sure how to fix this without messing anything else up.

Currently I have completed the following code for my button so I can save

private void bmSaveAs_Click(object sender, EventArgs e)
        {
            SaveFileDialog saveText = new SaveFileDialog();
            saveText.InitialDirectory = @"C:\";
            saveText.Filter = "TXT Files(*.txt;)|*.txt;";
            if (saveText.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                using (StreamWriter write = new StreamWriter(File.Create(saveText.FileName)))
                    write.Write(TextFile);           
            }

        }

Now under my second form(form2) I only have the following code

 public  partial class TextDocumentForm : Form
    {
        public TextDocumentForm()
        {
            InitializeComponent();
        }
        public string TextFile
        {
            get { return tbTextDoc.Text; }
            set { tbTextDoc.Text = value; }      
        }
    }

My current issue lies with my public string TextFile where I get the error that the current method does not exist in Form 1. Being fairly new I am unsure as to how to proceed and would appreciate any help as I have been stumped with this for a while.

public string TextFile is a member of Form2, and of course, form1 don't know it. You can try this:

public partial class Form1 : Form
    {
        Form2 frm2;
        public Form1()
        {
            InitializeComponent();
            frm2 = new Form2();
        }

        private void ShowForm2(object sender, EventArgs e)
        {         
            frm2.Show();
        }

        private void Save(object sender, EventArgs e)
        {
            MessageBox.Show(frm2.TextFile);
        }
    }

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