向窗口添加一个textbox,一个button,一个timer; 定义三个全局变量: public static int i = 0;//记录按钮按下的次数 public static int time = 0;//记录时间,本例定义为3秒不按键,则记录字符 public static string str = "";用于控制textbox的显示 事件如下: private void button1_Click(object sender, EventArgs e)//按钮单击事件 { timer1.Start(); i++; if (i == 8) { i = 1; } if (time >= 3) { str = textBox1.Text; time = 0; } switch (i) { case 1: { textBox1.Text = str+"2"; break; } case 2: { textBox1.Text = str + "A"; break; } case 3: { textBox1.Text = str + "B"; break; } case 4: { textBox1.Text = str + "C"; break; } case 5: { textBox1.Text = str + "a"; break; } case 6: { textBox1.Text = str + "b"; break; } case 7: { textBox1.Text = str + "c"; break; } } } private void timer1_Tick(object sender, EventArgs e)//计时器事件 { time++; }
timer的Interval属性定为1000即1秒间隔。
在txtbox的KeyDown事件里面处理, 开一个定时器,然后就是时间判断,你想怎么实现都行
int m_intInterval = 1000; DateTime m_dtLast = DateTime.Now; char m_chrInput = '\0'; char[][] KEYPAD = new char[][] { new char[]{ '0' }, new char[]{'1'}, new char[] { '2', 'a', 'b', 'c' }, new char[] { '3', 'd', 'e', 'f' }, new char[] {'4','g','h','i'}, new char[] {'5','j','k','l'}, new char[] {'6','m','n','o'}, new char[] {'7','p','q','r','s'}, new char[] {'8','t','u','v'}, new char[] {'9','w','x','y','z'}}; int m_intIndex = 0; public Form1() { InitializeComponent(); } private void txtInput_KeyPress(object sender, KeyPressEventArgs e) { if (InKeyPad(e.KeyChar)) { if (this.m_chrInput == '\0') { this.m_dtLast = DateTime.Now; this.m_chrInput = e.KeyChar; this.m_intIndex = 0; } else { TimeSpan tsInterval = DateTime.Now - this.m_dtLast; if ((this.m_chrInput == e.KeyChar) && (tsInterval.TotalMilliseconds <= this.m_intInterval)) { int iKeyIndex = e.KeyChar - '0'; if (this.m_intIndex == this.KEYPAD[iKeyIndex].Length - 1) this.m_intIndex = 0; else this.m_intIndex++; e.KeyChar = this.KEYPAD[iKeyIndex][this.m_intIndex]; if (this.txtInput.Text.Length > 0) { this.txtInput.Text = this.txtInput.Text.Substring(0, this.txtInput.Text.Length - 1); this.txtInput.SelectionStart = this.txtInput.Text.Length; } this.m_dtLast = DateTime.Now; } else { this.m_dtLast = DateTime.Now; this.m_chrInput = e.KeyChar; this.m_intIndex = 0; } } } else { this.m_dtLast = DateTime.Now; this.m_chrInput = '\0'; this.m_intIndex = 0; } } private bool InKeyPad(char p_chrInput) { for (int i = 0; i < this.KEYPAD.Length; i++) { if (p_chrInput == this.KEYPAD[i][0]) return (true); } return (false); } }
fmlboy :你的程序可以很稳定的跑在windows界面下,如果是在compact framework下就不行了 报错 System.Windows.Forms.KeyPressEventArgs.KeyChar' cannot be assigned to -- it is read only不知道有没有解决方法,一旦解决,就给分 不够再加分
定义三个全局变量: public static int i = 0;//记录按钮按下的次数
public static int time = 0;//记录时间,本例定义为3秒不按键,则记录字符
public static string str = "";用于控制textbox的显示
事件如下:
private void button1_Click(object sender, EventArgs e)//按钮单击事件
{
timer1.Start();
i++;
if (i == 8)
{
i = 1;
}
if (time >= 3)
{
str = textBox1.Text;
time = 0;
}
switch (i)
{
case 1:
{
textBox1.Text = str+"2";
break;
}
case 2:
{
textBox1.Text = str + "A";
break;
}
case 3:
{
textBox1.Text = str + "B";
break;
}
case 4:
{
textBox1.Text = str + "C";
break;
}
case 5:
{
textBox1.Text = str + "a";
break;
}
case 6:
{
textBox1.Text = str + "b";
break;
}
case 7:
{
textBox1.Text = str + "c";
break;
}
}
} private void timer1_Tick(object sender, EventArgs e)//计时器事件
{
time++;
}
开一个定时器,然后就是时间判断,你想怎么实现都行
DateTime m_dtLast = DateTime.Now;
char m_chrInput = '\0';
char[][] KEYPAD = new char[][] { new char[]{ '0' },
new char[]{'1'},
new char[] { '2', 'a', 'b', 'c' },
new char[] { '3', 'd', 'e', 'f' },
new char[] {'4','g','h','i'},
new char[] {'5','j','k','l'},
new char[] {'6','m','n','o'},
new char[] {'7','p','q','r','s'},
new char[] {'8','t','u','v'},
new char[] {'9','w','x','y','z'}};
int m_intIndex = 0; public Form1()
{
InitializeComponent();
} private void txtInput_KeyPress(object sender, KeyPressEventArgs e)
{
if (InKeyPad(e.KeyChar))
{ if (this.m_chrInput == '\0')
{
this.m_dtLast = DateTime.Now;
this.m_chrInput = e.KeyChar;
this.m_intIndex = 0;
}
else
{
TimeSpan tsInterval = DateTime.Now - this.m_dtLast;
if ((this.m_chrInput == e.KeyChar) && (tsInterval.TotalMilliseconds <= this.m_intInterval))
{
int iKeyIndex = e.KeyChar - '0';
if (this.m_intIndex == this.KEYPAD[iKeyIndex].Length - 1)
this.m_intIndex = 0;
else
this.m_intIndex++;
e.KeyChar = this.KEYPAD[iKeyIndex][this.m_intIndex];
if (this.txtInput.Text.Length > 0)
{
this.txtInput.Text = this.txtInput.Text.Substring(0, this.txtInput.Text.Length - 1);
this.txtInput.SelectionStart = this.txtInput.Text.Length;
}
this.m_dtLast = DateTime.Now;
}
else
{
this.m_dtLast = DateTime.Now;
this.m_chrInput = e.KeyChar;
this.m_intIndex = 0;
}
}
}
else
{
this.m_dtLast = DateTime.Now;
this.m_chrInput = '\0';
this.m_intIndex = 0;
}
} private bool InKeyPad(char p_chrInput)
{
for (int i = 0; i < this.KEYPAD.Length; i++)
{
if (p_chrInput == this.KEYPAD[i][0])
return (true);
}
return (false);
}
}
报错
System.Windows.Forms.KeyPressEventArgs.KeyChar' cannot be assigned to -- it is read only不知道有没有解决方法,一旦解决,就给分
不够再加分
//e.KeyChar = this.KEYPAD[iKeyIndex][this.m_intIndex];
if (this.txtInput.Text.Length > 0)
{
this.txtInput.Text = this.txtInput.Text.Substring(0, this.txtInput.Text.Length - 1);
//this.txtInput.SelectionStart = this.txtInput.Text.Length;
}
this.txtInput.Text = this.txtInput.Text + this.KEYPAD[iKeyIndex][this.m_intIndex];
this.txtInput.SelectionStart = this.txtInput.Text.Length;
e.Handled = true;但这程序有个明显的缺陷,就是只对最后一位输入时才是正确的,所以要真正使用,要楼主改一下哟^_^