检索文本框中插入符号的结束位置,以将插入符号放入下一个文本框中

发布于 2024-09-26 03:57:59 字数 46 浏览 2 评论 0原文

如何在 WPF 文本框中找到插入符的结束位置,以便我无法再使用插入符向右移动?

How do I find the end position of the Caret in a WPF textbox so I can`t move right anymore with the caret?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

玉环 2024-10-03 03:57:59

如果您需要找到 CaretIndex,请查看以下内容 问题

但是,如果您希望在某些条件下跳转到下一个文本框,请查看以下示例。这里我使用 TextBox 属性 MaxLength 和 KeyUp 事件在一个完成时跳转到下一个 TextBox。

这是 XAML:

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition/>
        <RowDefinition/>
    </Grid.RowDefinitions>
    <StackPanel
        Grid.Row="0">
        <TextBox Text="" MaxLength="3" KeyUp="TextBox_KeyUp" >
        </TextBox>
        <TextBox Text="" MaxLength="3" KeyUp="TextBox_KeyUp">
        </TextBox>
        <TextBox Text="" MaxLength="4" KeyUp="TextBox_KeyUp">
        </TextBox>
        </StackPanel>
</Grid>

这是来自代码隐藏的 KeyUp 事件:

private void TextBox_KeyUp(object sender, KeyEventArgs e)
{
   TextBox tb = sender as TextBox;
   if (( tb != null ) && (tb.Text.Length >= tb.MaxLength))
   {
      int nextIndex = 0;
      var parent = VisualTreeHelper.GetParent(tb);
      int items = VisualTreeHelper.GetChildrenCount(parent);
      for( int index = 0; index < items; ++index )
      {
         TextBox child = VisualTreeHelper.GetChild(parent, index) as TextBox;
         if ((child != null) && ( child == tb ))
         {
            nextIndex = index + 1;
            if (nextIndex >= items) nextIndex = 0;
            break;
         }
      }

      TextBox nextControl = VisualTreeHelper.GetChild(parent, nextIndex) as TextBox;
      if (nextControl != null)
      {
         nextControl.Focus();
      }
   }
}

编辑:
阅读以下内容后 answer 我修改了 TextBox_KeyUp 如下:

  private void TextBox_KeyUp(object sender, KeyEventArgs e)
  {
     Action<FocusNavigationDirection> moveFocus = focusDirection =>
     {
        e.Handled = true;
        var request = new TraversalRequest(focusDirection);
        var focusedElement = Keyboard.FocusedElement as UIElement;
        if (focusedElement != null)
           focusedElement.MoveFocus(request);
     };

     TextBox tb = sender as TextBox;
     if ((tb != null) && (tb.Text.Length >= tb.MaxLength))
     {
        moveFocus(FocusNavigationDirection.Next);
     }
  }
}

If you need to find the CaretIndex look at the following question.

However, if you are looking to jump to the next TextBox under certain conditions check out the following sample. Here I use the TextBox property MaxLength and the KeyUp event to jump to the next TextBox when one is completed.

Here is the XAML:

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition/>
        <RowDefinition/>
    </Grid.RowDefinitions>
    <StackPanel
        Grid.Row="0">
        <TextBox Text="" MaxLength="3" KeyUp="TextBox_KeyUp" >
        </TextBox>
        <TextBox Text="" MaxLength="3" KeyUp="TextBox_KeyUp">
        </TextBox>
        <TextBox Text="" MaxLength="4" KeyUp="TextBox_KeyUp">
        </TextBox>
        </StackPanel>
</Grid>

Here is the KeyUp event from the code-behind:

private void TextBox_KeyUp(object sender, KeyEventArgs e)
{
   TextBox tb = sender as TextBox;
   if (( tb != null ) && (tb.Text.Length >= tb.MaxLength))
   {
      int nextIndex = 0;
      var parent = VisualTreeHelper.GetParent(tb);
      int items = VisualTreeHelper.GetChildrenCount(parent);
      for( int index = 0; index < items; ++index )
      {
         TextBox child = VisualTreeHelper.GetChild(parent, index) as TextBox;
         if ((child != null) && ( child == tb ))
         {
            nextIndex = index + 1;
            if (nextIndex >= items) nextIndex = 0;
            break;
         }
      }

      TextBox nextControl = VisualTreeHelper.GetChild(parent, nextIndex) as TextBox;
      if (nextControl != null)
      {
         nextControl.Focus();
      }
   }
}

Edit:
After reading the following answer I modified the TextBox_KeyUp as follows:

  private void TextBox_KeyUp(object sender, KeyEventArgs e)
  {
     Action<FocusNavigationDirection> moveFocus = focusDirection =>
     {
        e.Handled = true;
        var request = new TraversalRequest(focusDirection);
        var focusedElement = Keyboard.FocusedElement as UIElement;
        if (focusedElement != null)
           focusedElement.MoveFocus(request);
     };

     TextBox tb = sender as TextBox;
     if ((tb != null) && (tb.Text.Length >= tb.MaxLength))
     {
        moveFocus(FocusNavigationDirection.Next);
     }
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文