查看输入文本的文本框在第二和改变passwordchar(View Input Text for a

2019-10-21 16:03发布

同样的问题 ,因为这一个,但最后的答案是一个我想要的。我需要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);
}

Answer 1:

你不需要每次订阅新的事件处理程序。 只要做到这一次在构造

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
}


文章来源: View Input Text for a second in TextBox and change to passwordchar
标签: c# wpf textbox