2013-05-03 54 views
1

我使用定时器和数组创建基于Windows窗体的多点(旧手机键盘)类型系统。然而,每当我点击一个按钮来追加文本到一个文本框,菜单条垂直复制,我不知道为什么看起来像我注意到引用菜单字符串在我的CS。代码本身没有完成,我只想停止发生这种重复,并且要从数组中的字符实际追加到Rich Text Box。任何帮助都将不胜感激,谢谢!C# - 无明显原因的菜单条重复

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

更新:原来模式按钮不再工作,这是罚款在此之前发生的事情。

+0

@大卫,感谢显示图像:) – 2013-05-03 06:00:42

+0

您应该使用枚举而不是纯文本用于检查CURRENTMODE ... – Max 2013-05-03 06:43:21

+0

@Mobstaa,谢谢,我会给枚举一个试试:) – 2013-05-03 07:44:06

回答

1

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

您当前的流程是:

appendChar - > runTimer - >的InitializeComponent

+0

哇...不能相信它是那么简单!哈!关于定时器,我仍然有点不确定,并且我在教程中找到了这个方法,我想这对我的实现来说有点太不同了。非常感谢! – 2013-05-03 06:05:59