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:
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
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);
}
}
}
Any ideas are appreciated --- Thanks in advance
Comment