Winforms循环不起作用

时间:2017-09-24 13:58:11

标签: c# winforms

您好我们正在为我们学校申请,我们需要每5秒钟更新(刷新)一次。我们尝试了一个简单的“do while”循环和一个定时循环,但似乎没有任何工作,任何人都可以帮忙吗?

代码效率不高,但我们刚刚开始编程。 我们想要循环的部分是从timer1_tick部分到结尾的所有内容。

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();
        }
    }



}
}

1 个答案:

答案 0 :(得分:1)

您需要启动计时器:

timer1.Start();

你需要调用函数InitializeTimer,最好是在构造函数

public Form1()
{
     InitializeComponent();

     InitializeTimer();
}