How to program a telephone keypad GUI?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Jonathan Radant

    #1

    How to program a telephone keypad GUI?

    I have created a GUI with a series of buttons numbered 0 through 9 plus a backspace key laid out like a telephone keypad, I also placed a read-only textbox above the buttons. The goal is to create a program works like one would text with a 10 button phone (i.e. "1" button = 1,a,b,c -- "2" button = 2,d,e,f --- etc.) When one clicks button 1, for example, the character 1 should be displayed in the textbox and if that button is clicked again less than one second later that one should change to an "a", another click less that a second after "a" displays should result in "b" being displayed in the textbox. I have setup a one second timer between clicks, which if it expires, the character is set in the textbox and the character clicked next is appended to the back of the previous character.

    Here's some of my code for example:

    Code:
    namespace WindowsFormsApplication1
    
    {
    
    public partial class Form1 : Form
    
    {
      string  newKeyPress = "";
      string oldKeyPress = "";
      string screenChar = "";
    
    public Form1()
    {
    InitializeComponent();
    }
    
    private void keypad_Click(object sender, EventArgs e)
    {
     if (sender.ToString() == "System.Windows.Forms.Button, Text: 0") 
      {
        newKeyPress = "0";
      }
     if (sender.ToString() == "System.Windows.Forms.Button, Text: 1")
      {
        newKeyPress = "1";
      }
     if (sender.ToString() == "System.Windows.Forms.Button, Text: 2") 
      {
        newKeyPress = "2";
      }
      txtNewKey.Text = newKeyPress;
      txtOldKey.Text = oldKeyPress;
        if (newKeyPress == oldKeyPress)
        {
         tmr1.Stop();
         tmr1.Start();
    
         if (newKeyPress == "1")
         {                   
          switch(screenChar)
           {
            case "": screenChar = "1"; break;
            case "1": screenChar = "a"; break;
            case "a": screenChar = "b"; break;
            case "b": screenChar = "c"; break;
            case "c": screenChar = "1"; break;
           }
            UpdateDisplay();
            }
          if (newKeyPress == "2")
          {
           switch (screenChar)
            {
             case "": screenChar = "2"; break;
             case "2": screenChar = "d"; break;
             case "d": screenChar = "e"; break;
             case "e": screenChar = "f"; break;
             case "f": screenChar = "2"; break;
            }
             UpdateDisplay();
              }
    
           else
           {
            txtDisplay.Text += newKeyPress;
           }
            oldKeyPress = newKeyPress;
           }
    
            private void UpdateDisplay()
            {
             int displayLength = txtDisplay.Text.Length;
             txtDisplay.Text = txtDisplay.Text.Remove    (displayLength - 1);
    
                txtDisplay.Text += screenChar;
            }
    
            private void tmr1_Tick(object sender, EventArgs e)
            {
                tmr1.Stop();
                oldKeyPress = "";
                screenChar = ""; 
            }
    private void btnBack_Click(object sender, EventArgs e)
            {
                int displayLength = txtDisplay.Text.Length;
                if(displayLength > 0)
                txtDisplay.Text = txtDisplay.Text.Remove(displayLength - 1);
            }
        }
    }
    I am running into a problem though, When I click through the characters assigned to a button and wait for the timer to expire, then click the same button again, the character that should have been set in the textbox previously changes. For example 2 changes to d, or d changes to e, etc. If I click a different button things work fine though.

    Any ideas are appreciated --- Thanks in advance
  • GaryTexmo
    Recognized Expert Top Contributor
    • Jul 2009
    • 1501

    #2
    I took a look through your code and I don't see anything wrong but I'm most likely just missing it. You're saying that when the timer expires, if you press the same key again it changes the letter that you were just using? Hmm, maybe in your timer tick do you maybe need to call UpdateDisplay? No, that should have happened at the end of the last key press.

    I dunno man... I'd need to dig into the running version of the code to debug it, which I can't do 'cause I don't have the GUI portion of your code. Perhaps someone with more awake debugging eyes than I have this morning will spot it.

    I would like to make a recommendation though... I'm looking at your code and it looks like you're going to have a very big button click method. You can probably do a couple of things to reduce the amount of work you have to do...

    1) Set up your keys in a data structure... perhaps a list of lists. The first list is an index to a second list, which contains the sequence of letters associated with that slot. Something like...

    Code:
    buttons[0] = "abc"
    buttons[1] = "def"
    ...
    When you click, the button sets the index (1 sets 0, 2 sets 1, etc... and subsequent clicks set an offset. That might let you remove the big blocks of if (newKeyPress == "<the key text>". You might even be able to put the buttons themselves into a hash table that maps to the key number that would be in the array, so you could just look it up...

    Code:
    int keyIndex = buttonHash[sender as Button];
    Something like that... just some friendly suggestions. If you're not interested or they don't help you, no worries. Good luck! :)

    Comment

    • gen123
      New Member
      • May 2015
      • 1

      #3
      Csharp mobile keypad

      Code:
      class Program
          {
              static void Main(string[] args)
              {
                  char[][] arr = new char[10][];
                  arr[0] = new char[2];
                  arr[1] = new char[2];
                  arr[2] = new char[4];
                  arr[3] = new char[4];
                  arr[4] = new char[4];
                  arr[5] = new char[4];
                  arr[6] = new char[4];
                  arr[7] = new char[5];
                  arr[8] = new char[4];
                  arr[9] = new char[5];
                  arr[0][0] = ' ';
                  arr[2][1] = 'a';
                  arr[2][2] = 'b';
                  arr[2][3] = 'c';
                  arr[3][1] = 'd';
                  arr[3][2] = 'e';
                  arr[3][3] = 'f';
                  arr[4][1] = 'g';
                  arr[4][2] = 'h';
                  arr[4][3] = 'i';
                  arr[5][1] = 'j';
                  arr[5][2] = 'k';
                  arr[5][3] = 'l';
                  arr[6][1] = 'm';
                  arr[6][2] = 'n';
                  arr[6][3] = 'o';
                  arr[7][1] = 'p';
                  arr[7][2] = 'q';
                  arr[7][3] = 'r';
                  arr[7][4] = 's';
                  arr[8][1] = 't';
                  arr[8][2] = 'u';
                  arr[8][3] = 'v';
                  arr[9][1] = 'w';
                  arr[9][2] = 'x';
                  arr[9][3] = 'y';
                  arr[9][4] = 'z';
      
                  string abc = Console.ReadLine();
      
                  char[] w = abc.ToCharArray();
      
                  for (int k = 0; k < w.Length; k++)
                  {
                      for (int i = 0; i < arr.Length; i++)
                      {
      
                          for (int j = 0; j < arr[i].Length; j++)
                          {
                              if (w[k].Equals(arr[i][j]))
                              {
                                  Console.Write(Array.IndexOf(arr, arr[i]) + " ");
                                  Console.WriteLine(Array.IndexOf(arr, arr[j]));
                              }
                          }
                      }
                  }
          }
          }
      Last edited by Rabbit; May 26 '15, 03:59 PM. Reason: Please use [CODE] [/CODE] tags when posting code or formatted data. Personal link removed per forum policy

      Comment

      Working...