与此问题相同,但最后一个答案是我想要的那个。我需要c#wpf ...我尝试了最后一个答案,但我似乎无法获得间隔..有时可以看到按下的键,有时它没有..按键时如何获得相同的间隔?
private void TextBox_KeyDown(object sender, KeyEventArgs e)
{
dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
dispatcherTimer.Interval = new TimeSpan(0, 0, 0, 1);
dispatcherTimer.Start();
}
我试过了
dispatcherTimer.Interval = TimeSpan.FromSeconds(5);
但还是一样
编辑
private void dispatcherTimer_Tick(object sender, EventArgs e)
{
string str = "";
for(int i = 0; i < txtPass.Text.Length; i++)
str += char.ConvertFromUtf32(8226);
txtPass.Text = str;
txtPass.Select(txtPass.Text.Length, 0);
}
您不需要每次都订阅新的事件处理程序。只需在构造函数中执行一次
dispatcherTimer = new DispatcherTimer();
dispatcherTimer.Tick += dispatcherTimer_Tick;
dispatcherTimer.Interval = new TimeSpan(0, 0, 0, 1);
在这种情况Keydown
下,只需重置timer
(遗憾的是我们没有任何Reset
方法)
private void TextBox_KeyDown(object sender, KeyEventArgs e)
{
dispatcherTimer.Stop();
dispatcherTimer.Start();
}
随着计时器的滴答声,只有Stop
计时器和您的工作
void dispatcherTimer_Tick(object sender, EventArgs e)
{
dispatcherTimer.Stop();
//your code
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句