none
save the code of textbox

    Question

  • I have to find the code how to save the context of a textbox(it belongs to Form1) in a file with button in a Form2?Could you help me?
    • Edited by Arianna Ts Sunday, January 13, 2019 12:10 AM
    • Moved by CoolDadTx Monday, January 14, 2019 3:02 PM Winforms related
    Sunday, January 13, 2019 12:10 AM

All replies

  • Hello,

    • Select the TextBox in question on form1.
    • Select properties, select   modifiers and change from private to public.

    • Create form2, set the owner to form1 (using 'this').
    using System;
    using System.Windows.Forms;
    
    namespace WindowsFormsApp3
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                var f = new Form2();
                f.Show(this);
            }
        }
    }
    

    Then in form2 write the contents of textbox1 on form1 to a file, in this case I write to a file in the same folder as the app.

    using System;
    using System.IO;
    using System.Windows.Forms;
    
    namespace WindowsFormsApp3
    {
        public partial class Form2 : Form
        {
            public Form2()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                File.WriteAllText(Path.Combine(
                    AppDomain.CurrentDomain.BaseDirectory,"MyFile.txt"), ((Form1)Owner).textBox1.Text);
            }
        }
    }
    

    Key parts, setting the owner as shown in the first code block. Referencing the text box via ((Form1)Owner)


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Sunday, January 13, 2019 2:22 AM
  • Hi Arianna,

    While Karen's suggestion works and is very simple to implement, I would argue that it ties the two Forms together too closely. May I suggest a more OOP-ish solution? Try using interfaces. I have a blog post that goes into more detail ( https://geek-goddess-bonnie.blogspot.com/2010/06/program-to-interface.html ), and here's an example of an Interface in action to solve your scenario:

    // First, the Interface. Name this whatever you want to, but note that the convention for
    // interfaces is to name them beginning with an upper-case I 
    // This can be defined anywhere
    public interface IGetDataFromElsewhere
    {
        string GetDataFromElsewhere();
    }
    

    // Form1 needs to implement the Interface
    public partial class Form1 : Form, IGetDataFromElsewhere
    {
        .....
        
        public string GetDataFromElsewhere()
        {
            return this.textBox1.Text;
        }
        
        private void button1_Click(object sender, EventArgs e)
        {
            // And needs to instantiate and show Form2 this way:
            var f2 = new Form2(this);
            f2.Show();
        }
    
        .....
    }
    

    // Form 2 needs an additional constructor and variable/field
    public partial class Form2 : Form
    {
        private IGetDataFromElsewhere remoteData = null;
        public Form2()
        {
            InitializeComponent();
        }
        public Form2(IGetDataFromElsewhere getData) : this()
        {
            remoteData = getData;
        }
        
        .....
        
        // And here's the button Click
        private void button1_Click(object sender, EventArgs e)
        {
            if (remoteData != null)
                File.WriteAllText(@"PathAndFileNameHere", remoteData.GetDataFromElsewhere());
        }
        
    }
    

    What this accomplishes is that now Form2 is not tightly coupled to Form1. It only cares about whether or not it has the Interface object and knows how to get what it needs from that object. It can be from Form1, as it is in this case, or in any other class that implements the IGetDataFromElsewhere interface. And Form1 does not have to expose its TextBox publicly.


    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    Monday, January 14, 2019 12:27 AM
  • Thank you very much.I would like you to ask you an something else.How can make textbox.text global in order to use it in other forms?I have watched many tutorials and I haven't figure it out.Thank you in advance for your time.
    Monday, January 14, 2019 1:15 AM
  • How can make textbox.text global in order to use it in other forms?

    In my opinion, Arianna, you don't want to do that. You want to use an Interface, as shown in my reply. Do you have a specific use in mind that can't be handled with my Interface example?

    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    Monday, January 14, 2019 2:27 AM
  • Hi Arianna Ts,

    The following article provide two methods about Passing Data Between Forms for your reference.

    https://www.codeproject.com/Articles/14122/Passing-Data-Between-Forms

    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, January 14, 2019 8:59 AM