Craze Joker Craze Joker - 25 days ago 7
C# Question

Winforms loop doesnt work

Hi we're making an application for our school and we need the form to update (refresh) every 5 seconds. we tried a simple "do while" loop and a timed loop, but nothing seems to work, can anybody help?

The code isn't very efficient but we're just beginning programmers.
the part we want to loop is everything from the timer1_tick part to the end.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Schoolbord_2
{
public partial class Form1 : Form
{


public Form1()
{
InitializeComponent();


}
private void Form1_Load(object sender, EventArgs e)
{

}

// This variable will be the loop counter.
private int counter;

private void InitializeTimer()
{
// Run this procedure in an appropriate event.
counter = 0;
timer1.Interval = 600;
timer1.Enabled = true;
// Hook up timer's tick event handler.
this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
}

private void timer1_Tick(object sender, System.EventArgs e)
{
if (counter >= 10)
{
// Exit loop code.
timer1.Enabled = false;
counter = 0;
}
else
{
string contents = File.ReadAllText(@"D:\Test.txt");
if (contents.Contains("4A"))
{
if (contents.Contains("1e"))
{
contents = contents.Remove(3, 6);
textBox6.Text = contents;
}
}

if (contents.Contains("5A"))
{
if (contents.Contains("1e"))
{
contents = contents.Remove(3, 6);
textBox48.Text = contents;
}
}

if (contents.Contains("6A"))
{
if (contents.Contains("1e"))
{
contents = contents.Remove(3, 6);
textBox49.Text = contents;
}
}

if (contents.Contains("4A"))
{
if (contents.Contains("2e"))
{
contents = contents.Remove(3, 6);
textBox9.Text = contents;
}
}

if (contents.Contains("5A"))
{
if (contents.Contains("2e"))
{
contents = contents.Remove(3, 6);
textBox51.Text = contents;
}
}


if (contents.Contains("6A"))
{
if (contents.Contains("2e"))
{
contents = contents.Remove(3, 6);
textBox52.Text = contents;
}
}


if (contents.Contains("4A"))
{
if (contents.Contains("3e"))
{
contents = contents.Remove(3, 6);
textBox12.Text = contents;
}
}

if (contents.Contains("5A"))
{
if (contents.Contains("3e"))
{
contents = contents.Remove(3, 6);
textBox54.Text = contents;
}
}

if (contents.Contains("6A"))
{
if (contents.Contains("3e"))
{
contents = contents.Remove(3, 6);
textBox55.Text = contents;
}
}

if (contents.Contains("4A"))
{
if (contents.Contains("4e"))
{
contents = contents.Remove(3, 6);
textBox15.Text = contents;
}
}

if (contents.Contains("5A"))
{
if (contents.Contains("4e"))
{
contents = contents.Remove(3, 6);
textBox57.Text = contents;
}
}

if (contents.Contains("6A"))
{
if (contents.Contains("4e"))
{
contents = contents.Remove(3, 6);
textBox58.Text = contents;
}
}

if (contents.Contains("4A"))
{
if (contents.Contains("5e"))
{
contents = contents.Remove(3, 6);
textBox18.Text = contents;
}
}

if (contents.Contains("5A"))
{
if (contents.Contains("5e"))
{
contents = contents.Remove(3, 6);
textBox60.Text = contents;
}
}

if (contents.Contains("6A"))
{
if (contents.Contains("5e"))
{
contents = contents.Remove(3, 6);
textBox61.Text = contents;
}
}

if (contents.Contains("4A"))
{
if (contents.Contains("6e"))
{
contents = contents.Remove(3, 6);
textBox21.Text = contents;
}
}

if (contents.Contains("5A"))
{
if (contents.Contains("6e"))
{
contents = contents.Remove(3, 6);
textBox44.Text = contents;
}
}

if (contents.Contains("6A"))
{
if (contents.Contains("6e"))
{
contents = contents.Remove(3, 6);
textBox47.Text = contents;
}
}

if (contents.Contains("4A"))
{
if (contents.Contains("7e"))
{
contents = contents.Remove(3, 6);
textBox24.Text = contents;
}
}

if (contents.Contains("5A"))
{
if (contents.Contains("7e"))
{
contents = contents.Remove(3, 6);
textBox56.Text = contents;
}
}

if (contents.Contains("6A"))
{
if (contents.Contains("7e"))
{
contents = contents.Remove(3, 6);
textBox59.Text = contents;
}
}

if (contents.Contains("4A"))
{
if (contents.Contains("8e"))
{
contents = contents.Remove(3, 6);
textBox27.Text = contents;
}
}

if (contents.Contains("5A"))
{
if (contents.Contains("8e"))
{
contents = contents.Remove(3, 6);
textBox66.Text = contents;
}
}

if (contents.Contains("6A"))
{
if (contents.Contains("8e"))
{
contents = contents.Remove(3, 6);
textBox67.Text = contents;
}
}

if (contents.Contains("4A"))
{
if (contents.Contains("9e"))
{
contents = contents.Remove(3, 6);
textBox30.Text = contents;
}
}

if (contents.Contains("5A"))
{
if (contents.Contains("9e"))
{
contents = contents.Remove(3, 6);
textBox70.Text = contents;
}
}

if (contents.Contains("6A"))
{
if (contents.Contains("9e"))
{
contents = contents.Remove(3, 6);
textBox71.Text = contents;
}
}

if (contents.Contains("4A"))
{
if (contents.Contains("10e"))
{
contents = contents.Remove(3, 7);
textBox33.Text = contents;
}
}

if (contents.Contains("5A"))
{
if (contents.Contains("10e"))
{
contents = contents.Remove(3, 7);
textBox74.Text = contents;
}
}

if (contents.Contains("6A"))
{
if (contents.Contains("10e"))
{
contents = contents.Remove(3, 7);
textBox75.Text = contents;
}
}

if (contents.Contains("4H"))
{
if (contents.Contains("1e"))
{
contents = contents.Remove(3, 6);
textBox5.Text = contents;
}
}

if (contents.Contains("4H"))
{
if (contents.Contains("2e"))
{
contents = contents.Remove(3, 6);
textBox8.Text = contents;
}
}

if (contents.Contains("4H"))
{
if (contents.Contains("3e"))
{
contents = contents.Remove(3, 6);
textBox14.Text = contents;
}
}

if (contents.Contains("4H"))
{
if (contents.Contains("3e"))
{
contents = contents.Remove(3, 6);
textBox17.Text = contents;
}
}

if (contents.Contains("4H"))
{
if (contents.Contains("4e"))
{
contents = contents.Remove(3, 6);
textBox20.Text = contents;
}
}

if (contents.Contains("4H"))
{
if (contents.Contains("5e"))
{
contents = contents.Remove(3, 6);
textBox23.Text = contents;
}
}

if (contents.Contains("4H"))
{
if (contents.Contains("7e"))
{
contents = contents.Remove(3, 6);
textBox26.Text = contents;
}
}

if (contents.Contains("4H"))
{
if (contents.Contains("8e"))
{
contents = contents.Remove(3, 6);
textBox29.Text = contents;
}
}

if (contents.Contains("4H"))
{
if (contents.Contains("9e"))
{
contents = contents.Remove(3, 6);
textBox32.Text = contents;
}
}

if (contents.Contains("4H"))
{
if (contents.Contains("10e"))
{
contents = contents.Remove(3, 7);
textBox35.Text = contents;
}
}

if (contents.Contains("5H"))
{
if (contents.Contains("1e"))
{
contents = contents.Remove(3, 6);
textBox34.Text = contents;
}
}

if (contents.Contains("5H"))
{
if (contents.Contains("2e"))
{
contents = contents.Remove(3, 6);
textBox35.Text = contents;
}
}

if (contents.Contains("5H"))
{
if (contents.Contains("3e"))
{
contents = contents.Remove(3, 6);
textBox36.Text = contents;
}
}

if (contents.Contains("5H"))
{
if (contents.Contains("4e"))
{
contents = contents.Remove(3, 6);
textBox38.Text = contents;
}
}

if (contents.Contains("5H"))
{
if (contents.Contains("5e"))
{
contents = contents.Remove(3, 6);
textBox41.Text = contents;
}
}

if (contents.Contains("5H"))
{
if (contents.Contains("6e"))
{
contents = contents.Remove(3, 6);
textBox62.Text = contents;
}
}

if (contents.Contains("5H"))
{
if (contents.Contains("7e"))
{
contents = contents.Remove(3, 6);
textBox50.Text = contents;
}
}

if (contents.Contains("5H"))
{
if (contents.Contains("8e"))
{
contents = contents.Remove(3, 6);
textBox64.Text = contents;
}
}

if (contents.Contains("5H"))
{
if (contents.Contains("9e"))
{
contents = contents.Remove(3, 6);
textBox68.Text = contents;
}
}

if (contents.Contains("5H"))
{
if (contents.Contains("10e"))
{
contents = contents.Remove(3, 7);
textBox72.Text = contents;
}
}
counter = counter + 1;
label1.Text = "Procedures Run: " + counter.ToString();
}
}



}
}

Answer Source

You need to start the timer:

timer1.Start();

And you need to call the function InitializeTimer , preferably in the constructor

public Form1()
{
     InitializeComponent();

     InitializeTimer();
}