【发布时间】:2016-02-03 10:39:36
【问题描述】:
我在 form1 中有一个文本框和一个按钮,当我在文本框中写入时,我可以使用我的按钮将其保存到计算机上的文件中。这是放在我的按钮内
public void button1_Click(object sender, EventArgs e)
{
string FileName = "C:\\sample\\sample.txt";
System.IO.StreamWriter WriteToFile;
WriteToFile = new System.IO.StreamWriter(FileName);
WriteToFile.Write(textBox1.Text);
WriteToFile.Close();
MessageBox.Show("Succeded, written to file");
但无论如何,我想将与 streamWriter 相关的所有内容移到他们自己的类 (Class1) 中,并从按钮内的主窗体中调用它。 如果我将所有内容移到 Button 中并将其移到方法内的 class1 中,则它声称 Textbox1 不存在,很明显。
对于我应该阅读更多内容,您有任何提示或链接吗?
最好的问候 数据库
【问题讨论】:
-
传递
textBox1.Text作为方法的参数 -
作为最佳实践,我还建议在 using 语句中打开 StreamWriter,或者让使用它的类实现 IDisposable 接口
标签: c# winforms class methods streamwriter