Silverlight 文本框中的“捕获”选项卡

发布于 2024-07-16 01:46:52 字数 80 浏览 6 评论 0原文

如何捕获在 Silverlight 文本框中输入的选项卡并在其位置呈现 4 个空格(或选项卡)?

我不知道如何阻止选项卡导航。

How can I capture a tab entered in a Silverlight TextBox and render 4 spaces (or a tab) in it's place?

I can't figure out how to block the tab navigation.

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

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

发布评论

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

评论(3

忆梦 2024-07-23 01:46:52

这就是我所做的(类似于约翰内斯的代码):

        private const string Tab = "    ";
    void textBox_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.Key == Key.Tab)
        {
            int selectionStart = textBox.SelectionStart;
            textBox.Text = String.Format("{0}{1}{2}", 
                textBox.Text.Substring(0, textBox.SelectionStart),
                Tab,
                textBox.Text.Substring(textBox.SelectionStart + textBox.SelectionLength, (textBox.Text.Length) - (textBox.SelectionStart + textBox.SelectionLength))
                );
            e.Handled = true;
            textBox.SelectionStart = selectionStart + Tab.Length;
        }
    }

即使您选择一些文本并按“Tab”键,它的行为也正是您所期望的。

另一件事:我尝试将制表符字符串设置为“\t”,但无济于事。 选项卡已渲染,但宽度为单个空格 - 因此选项卡 const 的值为四个空格。

Here is what I do (similar to Johannes' code):

        private const string Tab = "    ";
    void textBox_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.Key == Key.Tab)
        {
            int selectionStart = textBox.SelectionStart;
            textBox.Text = String.Format("{0}{1}{2}", 
                textBox.Text.Substring(0, textBox.SelectionStart),
                Tab,
                textBox.Text.Substring(textBox.SelectionStart + textBox.SelectionLength, (textBox.Text.Length) - (textBox.SelectionStart + textBox.SelectionLength))
                );
            e.Handled = true;
            textBox.SelectionStart = selectionStart + Tab.Length;
        }
    }

This behaves just how you expect even if you select some text and hit the ol' "Tab" key.

One more thing: I tried having the tab string as "\t", but to no avail. The tab rendered, but was the width of a single space - hence the value for the Tab const being four spaces.

维持三分热 2024-07-23 01:46:52

我不知道如何解决你的问题,我拼凑了一个解决方案,尽管这似乎有效。

设置 KeyDown 事件如下。

expenses.KeyDown += new KeyEventHandler(expenses_KeyDown);

在这种情况下,我输入以下代码:

void expenses_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Tab)
            {
                expenses.Text += "    ";
                expenses.Focus();
                expenses.LostFocus += new RoutedEventHandler(expenses_LostFocus);
            }
        }

然后在 LostFocus 中:

void expenses_LostFocus(object sender, RoutedEventArgs e)
        {
            expenses.Focus();
            expenses.Select(expenses.Text.Length - 1, 0);
        }

LostFocus 中的最后一行将编辑光标设置到文本的末尾,否则,当它获得焦点时,光标位置位于文本框的开头

I am not sure how to solve your problem, I hacked together a solution though that seems to work.

Set the KeyDown event as below.

expenses.KeyDown += new KeyEventHandler(expenses_KeyDown);

In that event I put the following code:

void expenses_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Tab)
            {
                expenses.Text += "    ";
                expenses.Focus();
                expenses.LostFocus += new RoutedEventHandler(expenses_LostFocus);
            }
        }

And then in LostFocus:

void expenses_LostFocus(object sender, RoutedEventArgs e)
        {
            expenses.Focus();
            expenses.Select(expenses.Text.Length - 1, 0);
        }

The last line in LostFocus sets the editing cursor to the end of the text, otherwise, when it gets focus, the cursor position is in the beginning of the textbox

梦亿 2024-07-23 01:46:52

这似乎对我来说效果很好,并且不需要第二个事件处理程序或文本框名称的硬编码:

void TabbableTextBox_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Tab)
            {
                e.Handled = true;

                var tb = ((TextBox)sender);
                tb.Text += "\t";
                tb.Select(tb.Text.Length, 0);
            }
        }

This seemed to work well for me, and does not require the second event handler or hard coding of the text box name:

void TabbableTextBox_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Tab)
            {
                e.Handled = true;

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