读取表单中的文件

读取表单中的文件

本文介绍了读取表单中的文件,并以另一种形式加载到文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

我有2个窗体,窗体1和窗体2.窗体1有一个将读取文件的按钮(btnRead).如何加载在Form1中从按钮读取的文件并在Form2中加载到文本框.

请帮忙

Hello,

I have 2 window forms, form1 and form 2. Form1 had a button(btnRead) that will read a file. How can I load the file that reading from button in form1 and load to textbox in form2.

Please help

private void btnRead_Click(object sender, EventArgs e)
{
StreamReader streamRdr = new StreamReader(fileName);
textbox1.text = streamRdr.ReadToEnd();
streamRdr.Close();
}


上面的代码将读取文件并在Form1的文本框中显示内容.如何在Form2中加载到文本框.

谢谢,


The above code will read the file and display content in textbox in form1. How I can load to textbox in form2.

thanks,

推荐答案

string s = File.ReadAllText(fileName);


string[] lines = File.ReadAllLines(fileName);

当您加载多行TextBox时,后者很有用:

The later is useful when you load a multiline TextBox:

myTextBox.Lines = lines;



如何将文本移动到第二种形式(称为frmTwo):
在frmTwo中,创建一个属性:



How to move the text to the second form (call it frmTwo):
In frmTwo, create a property:

public string Text
    {
    get { return myTextBox.Text; }
    set { myTextBox.Text = value; }
    }


public string[] Lines
    {
    get { return myTextBox.Lines; }
    set { myTextBox.Lines = value; }
   }


在您的第一个表单中,创建第二个表单的实例,设置数据并显示它:


In your first form, create an instance of the second form, set the data, and show it:

frmTwo f = new frmTwo();
f.Text = File.ReadAllText(fileName);
f.ShowDialog();


frmTwo f = new frmTwo();
f.Lines = File.ReadAllLines(fileName);
f.ShowDialog();

您可以使用f.Show代替f.ShowDialog

You can use f.Show instead of f.ShowDialog


Properties.Settings.Default.filename = filename;



3)在第二个表单上添加一个计时器和一个代码(在计时器中):



3)On your second form add a timer and a code(in timer):

if(Properties.Settings.Default.filename != "")
{
textBox1.Text = File.ReadAllLines(Properties.Settings.Default.fileName);
Properties.Settings.Default.filename = "";
}



不要忘记启用添加的计时器



Dont forget to enable added timer


这篇关于读取表单中的文件,并以另一种形式加载到文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 05:09