I have placed a textbox and other controls on a form. I have the event handler:
private void textbox_KeyPress(object sender, KeyPressEventArgs e)
{
//code to ensure text entered are numbers;
}
How can I test whether a key on the keyboard is actually pressed. If the user presses no key within say 5 seconds I wish to switch focus to another control.
I have tried writing:
if(textbox->Text == "") //then switch focus
in the Tick event of the timer but it does not work.
I would be very grateful for all help.
Scott LyslePosted May 17, 2008, 2:07 AM
With a textbox, button, and timer control set with an interval of 5000 msec:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace ButtonHitTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (!Char.IsDigit(e.KeyChar))
e.Handled = true;
else
timer1.Enabled = true;
}
private void timer1_Tick(object sender, EventArgs e)
{
if (!String.IsNullOrEmpty(textBox1.Text))
MessageBox.Show("There is something in the box");
timer1.Enabled = false;
}
}
}