Click here to Skip to main content
15,920,383 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am developing a windows application and I have four textboxes in the main form, form1, and one other textbox and button in my other form, form2. Now I want to write all textbox values in one textfile which is stored in my project in my.txt when I press a button, which is in form2.
How can i so this can any one help me.
Posted
Updated 20-Jan-12 2:01am
v2
Comments
RDBurmon 20-Jan-12 8:10am    
How you navigate from main from to form2 ?
jai000 20-Jan-12 8:12am    
How are you opening form2? You can pass values of all the textboxes of Form1 to form2, if you are opening Form2 from Form1 and later you can write these values to your my.txt file...

You can get the text out of a textbox by:
C#
string str = textbox.Text;

then write it to a file with a StreamWriter[^].
 
Share this answer
 
Comments
Scubapro 20-Jan-12 8:10am    
Since the buttonclick event is on Form2, you'll need to use: Form1.Textbox1.Text.
Richard MacCutchan 20-Jan-12 8:12am    
Well I assumed anyone coding in C# would be able to figure that out for themselves.
Scubapro 20-Jan-12 8:19am    
Looking at the question, my guess is that this is exactly the problem were the OP is struggling with. So, why withhold this vital information?
Richard MacCutchan 20-Jan-12 8:24am    
I'm not withholding anything, I'm trying to get the OP to think for himself; something that far too many posters here seem incapable of.
Scubapro 20-Jan-12 8:26am    
Okay, and you don't assume anyone coding in C#, would be able to extract the text from a Textbox ???
Hi,

In button Click event of Form2, Use this code

C#
using (StreamWriter sw = new StreamWriter("my.txt"))
            {
                
                    sw.WriteLine(Form1.Textbox1.Text);
                    sw.WriteLine(Form1.Textbox2.Text);
                    sw.WriteLine(Form1.Textbox3.Text);
                    sw.WriteLine(Form1.Textbox4.Text);
                    sw.WriteLine(Textbox1.Text);
              
            }
 
Share this answer
 
v2
C#
TextWriter tw = new StreamWriter("D:\\date.txt");
// write a line of text to the file
tw.WriteLine(TextBox1.Text);//TextBox1 is the id of the textbox

// close the stream
tw.Close();
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900