获取文本的某些行c#
我想知道是否有人可以帮助我。我希望能够让我的程序使用户能够仅从文本文档中读取特定的代码块。不过,我希望将其放置在按钮后面,以便可以打开和关闭它。我尝试过不同的方法来做到这一点,但没有任何效果有丝毫不同。
这就是我的代码目前的情况。
namespace filestream
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private string Read(string file)
{
StreamReader reader = new StreamReader(file);
string data = reader.ReadToEnd();
reader.Close();
return data;
}
private void btn1_Click(object sender, EventArgs e)
{
textBox1.Text = "";
DialogResult result = openFileDialog1.ShowDialog();
if (result == DialogResult.OK)
{
string data = Read(openFileDialog1.FileName);
textBox1.Text = data;
}
else
{
//do nothing
}
}
private void button1_Click(object sender, EventArgs e)
{
}
}
}
我对此很陌生,因此我们将不胜感激。
I was wondering if someone could help me. I would like to able to be to have my program give the user the ability to only read a certain block of code from a text document. However I would like it to be placed behind a button so it can be turned on and off. I have experimented wih different ways of doing this but nothing has made the slightest bit of difference.
This is how my code stands at the moment.
namespace filestream
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private string Read(string file)
{
StreamReader reader = new StreamReader(file);
string data = reader.ReadToEnd();
reader.Close();
return data;
}
private void btn1_Click(object sender, EventArgs e)
{
textBox1.Text = "";
DialogResult result = openFileDialog1.ShowDialog();
if (result == DialogResult.OK)
{
string data = Read(openFileDialog1.FileName);
textBox1.Text = data;
}
else
{
//do nothing
}
}
private void button1_Click(object sender, EventArgs e)
{
}
}
}
Im quite new at this so any help would be greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
据我所知,从你那里的情况来看,它应该有效。我唯一能想到的原因是您的按钮连接到了button1_Click例程而不是btn1_Click例程。如果当您单击按钮时它没有执行任何操作,这是我能看到的唯一原因。该代码看起来像是要求用户选择一个文件,然后读取整个文件并将其放入文本框中。
As far as I can see from what you have there, it should be working. The only thing I can think of why it is not is that your button is connected to the button1_Click routine instead of the btn1_Click routine. If when you click the button and it does not do anything, that is the only reason I can see. The code looks like it is written to ask for the user to select a file and then read the whole file in and place it in the text box.
如果您可以使用 .Net 3.5 和 LINQ,这里是一个选项......
扩展疯狂以显示过滤、解析和格式化...
If you can use .Net 3.5 and LINQ here is an option...
... extended insanity to show filtering, parsing, and formatting ...
您是否尝试过将
btn1_click
的内容放入button1_click
中?有时它对我有用。Have you tried taking the contents of
btn1_click
and putting it inbutton1_click
? Sometimes it works for me.