简体   繁体   English

C#-没有明显原因的菜单条复制

[英]C# - Menu Strip Duplicating for no Apparent Reason

I'm creating a Windows form based multi tap (old phone keypad) type system, using timers and arrays. 我正在使用计时器和数组创建一个基于Windows窗体的多点击(旧电话键盘)类型的系统。 However, whenever I click a button to append text to a text box, the menu strips duplicates vertically, I have no idea why seem as I have note referenced the menu string in my CS. 但是,每当我单击按钮以将文本追加到文本框时,菜单就会垂直删除重复项,我不知道为什么要在CS中引用菜单字符串。 The code itself isn't finished, I'd just like to stop this duplication from happening, and for the character from the array to actually append to the Rich Text Box. 代码本身还没有完成,我只是想阻止这种重复发生,并且要使数组中的字符实际附加到Rich Text Box中。 Any help at all would be appreciated, thanks! 任何帮助将不胜感激,谢谢!

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

This is a recode of a form I made before which had the same bug, I decided to start from scratch to fix it, but it didn't. 这是我之前制作的具有相同错误的表单的重新编码,我决定从头开始修复它,但没有。

Here's a link to a screencap of the problem: 这是该问题的屏幕截图的链接:

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

UPDATE: Turns out the Mode button is no longer working, and it was fine before this happened. 更新:原来模式按钮不再起作用,并且在发生这种情况之前还可以。

Remove the InitializeComponent(); 删除InitializeComponent(); in runTimer . runTimer It should be called once in constructor. 应该在构造函数中调用一次。

Your current flow is: 您当前的流程是:

appendChar -> runTimer -> InitializeComponent appendChar-> runTimer-> InitializeComponent

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM