简体   繁体   English

从Form1按钮保存Form2

[英]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. 目前,我有一个名为form1的表单,该表单允许我创建另一个名为form2的表单,form2具有一个可以输入文本的文本框。 On form1 I have a save button to save the text in form2 as a .txt file. 在form1上,我有一个保存按钮,用于将form2中的文本另存为.txt文件。 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 现在在第二种形式(form2)下,我只有以下代码

 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 ,在该错误中,我收到错误消息,表示当前方法在Form 1中不存在。而。

public string TextFile is a member of Form2, and of course, form1 don't know it. public string TextFile是Form2的成员,当然,form1不知道。 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);
        }
    }

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

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