在文本文件中写入文本框值 [英] write text box value in text file

查看:51
本文介绍了在文本文件中写入文本框值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发Windows应用程序,并且在主窗体form1中有四个文本框,在其他窗体form2中有一个其他文本框和按钮.现在,我想将所有文本框值写入一个文本文件,当我按下位于form2中的按钮时,该文本文件将存储在项目中的my.txt中.

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.

推荐答案

您可以通过以下方式从文本框中获取文本:
You can get the text out of a textbox by:
string str = textbox.Text;


然后将其写入具有 StreamWriter [ ^ ].


then write it to a file with a StreamWriter[^].




在Form2的按钮Click事件中,使用此代码

Hi,

In button Click event of Form2, Use this code

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);
              
            }


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();


这篇关于在文本文件中写入文本框值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆