Reputation: 171
I want to read single line and put it into textbox every 1 second. I managed this code:
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";
}
But the line
is not accessable. I also tried something like this:
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";
}
}
But it's ignoring the timer interval. What's more I have no idea how can I stop the timer in both examples. Can you give me a tip what could I do with this?
Any ideas why this code works good with if
but not with 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();
}
I want to combine every row from file2
with every row from file1
.
Upvotes: 0
Views: 1998
Reputation: 9397
Here is the Solution:
As mentioned in the comments, start everything at the beginning and in the background. First read your file and prepare a container of your lines. Then, start a timer. Now every time the timer1.Ticks
, we will display a line from our container starting from the first line. We keep doing the process again and again.
Watch out for your file path.
MyFile: "C:\Users\Public\TestFolder\Test.txt"
a
b
c
d
e
f
Code ready to compile:
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 !
}
}
}
Result:
Upvotes: 0
Reputation: 1708
My best option: (assumes myFilePath contains the path to the file you're using)
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();
}
Upvotes: 0
Reputation: 993
In the second example change the code to following
private void timer1_Tick(object sender, EventArgs e) {
if((line = file.ReadLine()) != null)
{ textBox1.Text += line + "\r\n";
}
else
{
//Stop Timer here
}
}
Upvotes: 1
Reputation: 56
This code will take a line from a given file (currently c:\myfile\test.xml) and read it into an array, then start the timer. Once the timer has been started it will determine if the end of your data has been met. If there is still data left then you will have it appended into the textbox. If no data is left the timer will stop. You can press the button again to restart the process again.
//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();
}
}
Happy Coding, Jason
Upvotes: 1