Silverlight 文本框中的捕获选项卡 [英] Capturing tab in Silverlight TextBox

查看:30
本文介绍了Silverlight 文本框中的捕获选项卡的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何捕获在 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.

推荐答案

这是我所做的(类似于 Johannes 的代码):

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;
        }
    }

即使您选择一些文本并按下 ol 的Tab"键,这也会如您所愿.

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

还有一件事:我尝试将制表符字符串设为\t",但无济于事.制表符呈现,但只有一个空格的宽度 - 因此 Tab const 的值为四个空格.

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.

这篇关于Silverlight 文本框中的捕获选项卡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆