我想阅读单行并每隔1秒将其放入文本框中。我管理了这段代码:
private void button1_Click(object sender, EventArgs e)
{
while ((line = file.ReadLine()) != null)
{
timer.Start();
}
}
private void timer1_Tick(object sender, EventArgs e)
{
textBox1.text += line + "\r\n";
}
但line
无法访问。我也试过这样的事情:
private void button1_Click(object sender, EventArgs e)
{
timer.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
while ((line = file.ReadLine()) != null)
{
textBox1.Text += line + "\r\n";
}
}
但它忽略了计时器间隔。还有什么我不知道如何在两个例子中停止计时器。你能给我一个提示,我可以用这个做什么?
为什么此代码适用于if
而不适用于while
的任何想法?
private void timer1_Tick(object sender, EventArgs e)
{
while ((line1 = file1.ReadLine()) != null)
{
while ((line2 = file2.ReadLine()) != null)
{
try
{
//some code
}
catch
{
//some code
}
finally
{
//some code
}
}
}
timer1.Stop();
}
我想将file2
中的每一行与file1
中的每一行合并。
答案 0 :(得分:2)
此代码将从给定文件(当前为c:\ myfile \ test.xml)中取一行并将其读入数组,然后启动计时器。计时器启动后,它将确定您的数据是否已满足。如果仍有数据,则将其添加到文本框中。如果没有数据,计时器将停止。您可以再次按此按钮再次重新启动该过程。
//holds each line of the file contents
string[] lines = null;
//sets the current line number that you are at in the lines array
int curline = 0;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//reads all lines of files and starts the timer
lines = File.ReadAllLines(@"C:\myfile\test.xml");
curline = 0;
timer1.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
//if not end of data then insert on another line of the textbox
if (curline < lines.Length)
{
textBox1.Text += lines[curline] + "\r\n";
curline++;
}
else
{
//else stop the timer
timer1.Stop();
}
}
快乐编码, 杰森
答案 1 :(得分:1)
在第二个示例中,将代码更改为以下
private void timer1_Tick(object sender, EventArgs e) {
if((line = file.ReadLine()) != null)
{ textBox1.Text += line + "\r\n";
}
else
{
//Stop Timer here
}
}
答案 2 :(得分:1)
以下是解决方案:
如评论中所述,在开头和后台开始一切。首先阅读您的文件并准备一个容器。然后,启动计时器。现在每次"C:\Users\Public\TestFolder\Test.txt"
,我们都会从第一行开始显示容器中的一行。我们一次又一次地继续这个过程。
注意您的文件路径。
MyFile:using System;
using System.Linq;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class ExampleProgram : Form
{
// global variables
string[] MyLines = System.IO.File.ReadAllLines(@"C:\Users\Public\TestFolder\Test.txt"); // contain all lines
int MyCurrentLine = 0;
public ExampleProgram()
{
InitializeComponent();
StartProgram(); // start the program
}
private void StartProgram()
{
timer1.Interval = 1000; // set interval in milliseconds
timer1.Start(); // start timer
}
private void timer1_Tick(object sender, EventArgs e)
{
//timer1.Stop(); // first thing first - Edit: Looks like you did not need this.
textBox1.Text = MyLines[MyCurrentLine % MyLines.Count()]; // display next line
++MyCurrentLine; // increment counter
//timer1.Start(); // restart - Edit: And also won't need this. Less code.. Nice !
}
}
}
a
B'/ P>
c
d
ë
˚F
准备编译的代码:
Authorization
结果:
答案 3 :(得分:0)
我最好的选择:(假设myFilePath包含您正在使用的文件的路径)
private Queue<string> _lines;
private void button1_Click(object sender, EventArgs e)
{
_lines = new Queue<string>(System.IO.File.ReadAllLines(myFilePath));
textBox1.Text = string.Empty;
timer1.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
if (_lines.Any())
textBox1.Text += _lines.Dequeue() + Environment.NewLine;
else
timer1.Stop();
}