C# - 菜单条复制无明显原因

时间:2013-05-03 05:46:43

标签: c# winforms

我正在使用计时器和数组创建基于Windows窗体的多点击(旧手机键盘)类型系统。但是,每当我单击一个按钮将文本附加到文本框时,菜单条会垂直复制,我不知道为什么看起来像我已经注意到我的CS中的菜单字符串。代码本身没有完成,我只是想阻止这种重复发生,并且数组中的字符实际上附加到Rich Text Box。任何帮助都将不胜感激,谢谢!

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

    namespace Mini_Keyboard
    {
        public partial class MiniKeyboard : Form
        {
            public MiniKeyboard()
            {
                InitializeComponent();
            }

            string currentMode = "Multi Tap"; // Sets the mode of the app on startup to be "Multi Tap"
            int intIntervalRequired = 1000; // Time interval in which the user has to switch through the characters
            string currentKey;
            string prevKey;
            int currentIndex = -1;
            string[] keyPad1 = new string[7] { ".", "~", "\"", "1", "'", ":", ";" }; // Characters for key 1
            string[] keyPad2 = new string[7] { "a", "b", "c", "2", "A", "B", "C" }; // Characters for key 2
            string[] keyPad3 = new string[7] { "d", "e", "f", "3", "D", "E", "F" }; // Characters for key 3
            string[] keyPad4 = new string[7] { "g", "h", "i", "4", "G", "H", "I;" }; // Characters for key 4
            string[] keyPad5 = new string[7] { "j", "k", "l", "5", "J", "K", "L" }; // Characters for key 5
            string[] keyPad6 = new string[7] { "m", "n", "o", "6", "M", "N", "O" }; // Characters for key 6
            string[] keyPad7 = new string[9] { "p", "q", "r", "s", "7", "P", "Q", "R", "S" }; // Characters for key 7
            string[] keyPad8 = new string[7] { "t", "u", "v", "8", "T", "U", "V" }; // Characters for key 8
            string[] keyPad9 = new string[9] { "w", "x", "y", "z", "9", "W", "X", "Y", "Z" }; // Characters for key 9
            string[] keyPad0 = new string[2] { "0", " " }; // Characters for key 0
            string[] keyPadStar = new string[3] { "*", "-", "_" }; // Characters for key Star
            string[] keyPadHash = new string[3] { "#", "-", "_" }; // Characters for key Hash
            Timer timer = new Timer();
            public void runTimer()
            {
                InitializeComponent();

                timer.Tick += new EventHandler(stopTimer);
                timer.Interval = intIntervalRequired;
                timer.Enabled = true;
                timer.Start();
            }

            public void stopTimer(object sender, EventArgs e)
            {
                timer.Stop();
                prevKey = currentKey;
                currentKey = "";
                currentIndex = -1;
            }

            private void btnChangeMode_Click(object sender, EventArgs e)
            {
                if (currentMode == "Multi Tap") // If the current mode is "Multi Tap", change it to "Prediction"
                {
                    currentMode = "Prediction";
                    txtCurrentMode.Text = currentMode;
                }
                else // If the current mode is "Prediction", change it to "Multi Tap"
                {
                    currentMode = "Multi Tap";
                    txtCurrentMode.Text = currentMode;
                }
            }

            private void btnKeyPadNo2_Click(object sender, EventArgs e)
            {
                currentKey = "2";
                appendChar(ref keyPad2);
            }

            public void appendChar(ref string[] key)
            {
                runTimer();
                if (currentIndex == -1)
                {
                    currentIndex++;
                    rtbCurrentString.AppendText(key[currentIndex]);
                }
            }
        }
    }

这是我之前制作过的形式的重新编码有同样的错误,我决定从头开始修复它,但事实并非如此。

以下是问题屏幕截图的链接:

http://i44.tinypic.com/30lkjlk.png

更新:关闭模式按钮不再有效,在发生这种情况之前一切正常。

1 个答案:

答案 0 :(得分:1)

删除InitializeComponent();中的runTimer。它应该在构造函数中调用一次。

您当前的流程是:

appendChar - > runTimer - >的InitializeComponent