这个c#程序中的计时器不起作用

本文关键字:计时器 不起作用 程序 这个 | 更新日期: 2023-09-27 18:12:00

当我输入一个字母时,我得到了我在顶部声明的当前时间,但它不起作用,因为在声明了一定的时间后,字母没有被锁定。

例如

它会在5秒后从p变成Q当我想让它变成p(然后保存在文本框中)然后再p允许我制作单词

下面是我的代码:
namespace MiniKeyboardAssignment
{
    public partial class Form1 : Form
    {
        //How long i've set my interval to go for, this means how long my timer will be on for before the current letter is saved//
        int timerinterval = 2000;

        bool button_but1 = true;
        int Button1_clicked = -1;
        bool button_but2 = true;
        int Button2_clicked = -1;
        bool button_but3 = true;
        int Button3_clicked = -1;
        bool button_but4 = true;
        int Button4_clicked = -1;
        bool button_but5 = true;
        int Button5_Clicked = -1;
        bool button_but6 = true;
        int Button6_clicked = -1;
        bool button_but7 = true;
        int Button7_clicked = -1;
        bool button_but8 = true;
        int Button8_clicked = -1;
        bool button_but9 = true;
        int Button9_clicked = -1;
        bool button_but10 = true;
        int Button10_clicked = -1;
        bool button_but12 = true;
        int Button12_clicked = -1;
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            Mode_TextBox.Text = "Multipress";
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            counter = Convert.ToInt16(textBox1.Text);
            counter++;
            textBox1.Text = Convert.ToString(counter);
            timer1.Enabled = true; 
     }
        private void Button1_Click(object sender, EventArgs e)
        {
            string sequence1 = "1";
            if (button_but1 == true)
            {
                Button1_clicked++;
                textbox.Text = Convert.ToString(ListBox1.Items[Button1_clicked]);
                listBox11.Items.Add(sequence1);
                counter++;
                textBox1.Text = Convert.ToString(counter);
                button_but1 = false;
                timer1.Enabled = true;
            }
            else
            {
                Button1_clicked++;
            }
            characterstring[Convert.ToInt16(textBox1.Text)] = Convert.ToString(ListBox1.Items[Button1_clicked]);
            textbox.Text = characterstring[0] + characterstring[1] + characterstring [2] + characterstring [3] + characterstring [4] + characterstring [5] + characterstring [6] + characterstring [7] + characterstring [8] + characterstring [9] + characterstring [10] + characterstring [11] + characterstring [12] + characterstring [13] + characterstring [14] + characterstring [15] + characterstring [16] + characterstring [17] + characterstring [18] + characterstring [19] + characterstring [20];
        }

这个c#程序中的计时器不起作用

确保你已经正确初始化了你的textBox和timer:

public Form1()
    {
        InitializeComponent();
        this.textBox1.Text = "1";
        this.timer1.Interval = timerInterval;
        this.timer1.Start();
    }

也不需要设置timer1。在timer1_Tick方法中启用。将Enabled设置为true与调用Start相同(在我的示例中,这是在构造函数中完成的)。或者,您可以继续只在单击button1时启用计时器。