473,473 Members | 1,754 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 5698
GaryTexmo
1,501 Recognized Expert Top Contributor
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 New Member
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.