473,385 Members | 1,872 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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:

Expand|Select|Wrap|Line Numbers
  1. namespace WindowsFormsApplication1
  2.  
  3. {
  4.  
  5. public partial class Form1 : Form
  6.  
  7. {
  8.   string  newKeyPress = "";
  9.   string oldKeyPress = "";
  10.   string screenChar = "";
  11.  
  12. public Form1()
  13. {
  14. InitializeComponent();
  15. }
  16.  
  17. private void keypad_Click(object sender, EventArgs e)
  18. {
  19.  if (sender.ToString() == "System.Windows.Forms.Button, Text: 0") 
  20.   {
  21.     newKeyPress = "0";
  22.   }
  23.  if (sender.ToString() == "System.Windows.Forms.Button, Text: 1")
  24.   {
  25.     newKeyPress = "1";
  26.   }
  27.  if (sender.ToString() == "System.Windows.Forms.Button, Text: 2") 
  28.   {
  29.     newKeyPress = "2";
  30.   }
  31.   txtNewKey.Text = newKeyPress;
  32.   txtOldKey.Text = oldKeyPress;
  33.     if (newKeyPress == oldKeyPress)
  34.     {
  35.      tmr1.Stop();
  36.      tmr1.Start();
  37.  
  38.      if (newKeyPress == "1")
  39.      {                   
  40.       switch(screenChar)
  41.        {
  42.         case "": screenChar = "1"; break;
  43.         case "1": screenChar = "a"; break;
  44.         case "a": screenChar = "b"; break;
  45.         case "b": screenChar = "c"; break;
  46.         case "c": screenChar = "1"; break;
  47.        }
  48.         UpdateDisplay();
  49.         }
  50.       if (newKeyPress == "2")
  51.       {
  52.        switch (screenChar)
  53.         {
  54.          case "": screenChar = "2"; break;
  55.          case "2": screenChar = "d"; break;
  56.          case "d": screenChar = "e"; break;
  57.          case "e": screenChar = "f"; break;
  58.          case "f": screenChar = "2"; break;
  59.         }
  60.          UpdateDisplay();
  61.           }
  62.  
  63.        else
  64.        {
  65.         txtDisplay.Text += newKeyPress;
  66.        }
  67.         oldKeyPress = newKeyPress;
  68.        }
  69.  
  70.         private void UpdateDisplay()
  71.         {
  72.          int displayLength = txtDisplay.Text.Length;
  73.          txtDisplay.Text = txtDisplay.Text.Remove    (displayLength - 1);
  74.  
  75.             txtDisplay.Text += screenChar;
  76.         }
  77.  
  78.         private void tmr1_Tick(object sender, EventArgs e)
  79.         {
  80.             tmr1.Stop();
  81.             oldKeyPress = "";
  82.             screenChar = ""; 
  83.         }
  84. private void btnBack_Click(object sender, EventArgs e)
  85.         {
  86.             int displayLength = txtDisplay.Text.Length;
  87.             if(displayLength > 0)
  88.             txtDisplay.Text = txtDisplay.Text.Remove(displayLength - 1);
  89.         }
  90.     }
  91. }
  92.  
  93.  
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
Nov 18 '10 #1
2 5676
GaryTexmo
1,501 Expert 1GB
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...

Expand|Select|Wrap|Line Numbers
  1. buttons[0] = "abc"
  2. buttons[1] = "def"
  3. ...
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...

Expand|Select|Wrap|Line Numbers
  1. 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! :)
Nov 18 '10 #2
gen123
1
Expand|Select|Wrap|Line Numbers
  1. class Program
  2.     {
  3.         static void Main(string[] args)
  4.         {
  5.             char[][] arr = new char[10][];
  6.             arr[0] = new char[2];
  7.             arr[1] = new char[2];
  8.             arr[2] = new char[4];
  9.             arr[3] = new char[4];
  10.             arr[4] = new char[4];
  11.             arr[5] = new char[4];
  12.             arr[6] = new char[4];
  13.             arr[7] = new char[5];
  14.             arr[8] = new char[4];
  15.             arr[9] = new char[5];
  16.             arr[0][0] = ' ';
  17.             arr[2][1] = 'a';
  18.             arr[2][2] = 'b';
  19.             arr[2][3] = 'c';
  20.             arr[3][1] = 'd';
  21.             arr[3][2] = 'e';
  22.             arr[3][3] = 'f';
  23.             arr[4][1] = 'g';
  24.             arr[4][2] = 'h';
  25.             arr[4][3] = 'i';
  26.             arr[5][1] = 'j';
  27.             arr[5][2] = 'k';
  28.             arr[5][3] = 'l';
  29.             arr[6][1] = 'm';
  30.             arr[6][2] = 'n';
  31.             arr[6][3] = 'o';
  32.             arr[7][1] = 'p';
  33.             arr[7][2] = 'q';
  34.             arr[7][3] = 'r';
  35.             arr[7][4] = 's';
  36.             arr[8][1] = 't';
  37.             arr[8][2] = 'u';
  38.             arr[8][3] = 'v';
  39.             arr[9][1] = 'w';
  40.             arr[9][2] = 'x';
  41.             arr[9][3] = 'y';
  42.             arr[9][4] = 'z';
  43.  
  44.             string abc = Console.ReadLine();
  45.  
  46.             char[] w = abc.ToCharArray();
  47.  
  48.             for (int k = 0; k < w.Length; k++)
  49.             {
  50.                 for (int i = 0; i < arr.Length; i++)
  51.                 {
  52.  
  53.                     for (int j = 0; j < arr[i].Length; j++)
  54.                     {
  55.                         if (w[k].Equals(arr[i][j]))
  56.                         {
  57.                             Console.Write(Array.IndexOf(arr, arr[i]) + " ");
  58.                             Console.WriteLine(Array.IndexOf(arr, arr[j]));
  59.                         }
  60.                     }
  61.                 }
  62.             }
  63.     }
  64.     }
May 26 '15 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: RoDzZzZ | last post by:
anyone known how i can get this virtual keypad? access: https://www.secureweb.com.br not is possible copy the keypad virtual full? anyone have one to send for me?
4
by: Iftikhar Hussain | last post by:
Hello Everyone Nice to be back here, well all u guys have helped in past & now I would like a favor again. first what i want to develop is as follows: An application for rescue station, where...
0
by: Sushant Bhatia | last post by:
Hi. I am trying to develop an application that would be able to get keypad key press events even when the focus is not on the application. For instance, if the application is minimized to the...
3
by: Melson | last post by:
Hi May I know is there a way to change the Numeric keypad into mobile phone keypad? Regards Melson
10
by: Petr Jakeš | last post by:
I have a standard 12-key mobile phone keypad connected to my Linux machine as a I2C peripheral. I would like to write a code which allows the text entry to the computer using this keypad (something...
0
by: Claire | last post by:
Ive a modal form that offers message editing for an internal mail system. Its part of a touch screen application. I need to display a keypad form when necessary (some users have no keyboard). My...
0
saranjegan
by: saranjegan | last post by:
dudes,i need to access a PC game from a mobile keypad instead of playing via our PC keyboard,the mobile caller may call a particular number its handled by PBX via PBX i can get the input of data...
4
by: Dennieku | last post by:
Hi, I have to develop an on-screen keyboard and on-screen numeric keypad for a touchscreen UI. The hardest thing with this is that it has to be multi-lingual. Has anybody have ideas how to...
10
by: Nkhosinathie | last post by:
This isn't a homework,i want to improve my skills in programming and apply my understanding to pinters. i'm developing a program that will print telephone numbers randomly for a city for...
10
by: gopishk | last post by:
I have a text box and it can contain only numbers. Similar to a phone number text box while adding a new contact. Programatically how can we open only the numeric keypad instead of both text/numeric...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.