弄清楚一个文本框正在失去焦点,而另一个控件将被聚焦

本文关键字:焦点 另一个 控件 聚焦 失去 一个 文本 弄清楚 | 更新日期: 2023-09-27 18:27:26

所以基本上我有两个文本框:LoginMail和LoginPassword。我正在尝试为他们设置动画:

  • 用户进入LoginMail#1动画#1启动,用户退出Loginmail#1动画#2启动
  • 用户输入登录邮件#1动画#1启动,用户退出登录邮件#1并转到登录密码#2没有动画

代码

private void LoginEmail_GotFocus(object sender, RoutedEventArgs e)
{
    FocusAnimation.Begin();
}
private void LoginEmail_LostFocus(object sender, RoutedEventArgs e)
{
    UnfocusAnimation.Begin();
}
private void LoginPassword_GotFocus(object sender, RoutedEventArgs e)
{
    FocusAnimation.Begin();
}
private void LoginPassword_LostFocus(object sender, RoutedEventArgs e)
{
    UnfocusAnimation.Begin();
}

它现在不起作用,因为当用户输入登录邮件#1,然后转到登录密码#2时,会发生以下事件:

  • LoginMail_GotFocus(=>FocusAnimation.Beggin();)
  • LoginMail_LostFocus(=>UncusAnimation.Beggin();)
  • LoginPassword_GotFocus(=>FocusAnimation.Beggin();)

所以有必要弄清楚用户是从LoginMail转到LoginPassword,而不是显示UncusAnimation&第二焦点动画。不幸的是,我不知道该怎么做。

弄清楚一个文本框正在失去焦点,而另一个控件将被聚焦

您应该检查谁在LoginMail TextBox之后获得焦点。这样的东西应该有效:

private void LoginEmail_LostFocus(object sender, RoutedEventArgs e)
{
      var focusedControl = FocusManager.GetFocusedElement(this);
      if (focusedControl.GetType() != typeof(TextBox) || ((TextBox)focusedControl).Name != "LoginPassword")
      {
           UnfocusAnimation.Begin();            
      }
}

只要按下keyboard中的任何键,就可以简单地获得Textbox 1的焦点,如:

yourtextbox.Focus();

然后为了丢失focus,你可以使用这样的东西:

this.Focus();

看看这些了解更多:

https://stackoverflow.com/questions/

如何在WinForms中删除文本框中的焦点?