文本框控件中的文本缩进

发布于 2024-09-26 22:19:43 字数 81 浏览 1 评论 0原文

有没有一种简单的方法来缩进多行选择,通过按 tab 键或 shift-tab 来删除 C# 文本框控件 (VS2008) 中每个选定行开头的制表符?

Is there a simple way to indent a multiline selection by pressing the tab-key or shift-tab to remove tabs at the beginning of each selected line in a C# textbox control (VS2008)?

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

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

发布评论

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

评论(3

余厌 2024-10-03 22:19:43

以下代码片段使用了我编写的两个(简单的)扩展方法(SelectFullLinesReplaceRegex),但方法应该很清楚:

private void txtScript_KeyDown(object sender, KeyEventArgs e)
{
    txtScript.SelectFullLines();

    if (!e.Shift) {
        txtScript.SelectedText = txtScript.SelectedText.ReplaceRegex("^", "\t", System.Text.RegularExpressions.RegexOptions.Multiline);
    } else {
        txtScript.SelectedText = txtScript.SelectedText.ReplaceRegex("^\\t", "", System.Text.RegularExpressions.RegexOptions.Multiline);
    }
}

您还需要设置 txtScript.AcceptsTab = 真

The following snippet uses two (trivial) extensions methods I wrote (SelectFullLines, ReplaceRegex), but the approach should be clear:

private void txtScript_KeyDown(object sender, KeyEventArgs e)
{
    txtScript.SelectFullLines();

    if (!e.Shift) {
        txtScript.SelectedText = txtScript.SelectedText.ReplaceRegex("^", "\t", System.Text.RegularExpressions.RegexOptions.Multiline);
    } else {
        txtScript.SelectedText = txtScript.SelectedText.ReplaceRegex("^\\t", "", System.Text.RegularExpressions.RegexOptions.Multiline);
    }
}

You also need to set txtScript.AcceptsTab = True.

方圜几里 2024-10-03 22:19:43

也许这实际上很复杂,我不知道。但下面的解决方案给了你一个想法并且基本上可行。您必须自己实现删除,并且我没有真正测试代码。这只是为了让您了解如何做到这一点。

class Class1 : TextBox
{
    protected override bool IsInputKey(Keys keyData)
    {
        switch (keyData & Keys.KeyCode)
        {
            case Keys.Tab:
                return true;
        }
        return base.IsInputKey(keyData);
    }

    protected override void OnKeyPress(KeyPressEventArgs e)
    {
        if (e.KeyChar == '\t' && SelectedText.Contains(Environment.NewLine))
        {
            string start = string.Empty;
            if(SelectionStart > 0)
                start = Text.Substring(0, SelectionStart);
            string exchange = Text.Substring(SelectionStart, SelectionLength);
            string end = string.Empty;
            if(SelectionStart + SelectionLength < Text.Length - 1)
                end = Text.Substring(SelectionStart + SelectionLength);
            Text = start + '\t' + 
                exchange.Replace(Environment.NewLine, Environment.NewLine + "\t") + 
                end;
            e.Handled = true;
        }
        base.OnKeyPress(e);
    }
}

Maybe this is actually complicated, I don't know. But the following solution gives you an idea and basically works. You have to implement the removal yourself and I didn't really test the code. This is just to give you an idea how this can be done.

class Class1 : TextBox
{
    protected override bool IsInputKey(Keys keyData)
    {
        switch (keyData & Keys.KeyCode)
        {
            case Keys.Tab:
                return true;
        }
        return base.IsInputKey(keyData);
    }

    protected override void OnKeyPress(KeyPressEventArgs e)
    {
        if (e.KeyChar == '\t' && SelectedText.Contains(Environment.NewLine))
        {
            string start = string.Empty;
            if(SelectionStart > 0)
                start = Text.Substring(0, SelectionStart);
            string exchange = Text.Substring(SelectionStart, SelectionLength);
            string end = string.Empty;
            if(SelectionStart + SelectionLength < Text.Length - 1)
                end = Text.Substring(SelectionStart + SelectionLength);
            Text = start + '\t' + 
                exchange.Replace(Environment.NewLine, Environment.NewLine + "\t") + 
                end;
            e.Handled = true;
        }
        base.OnKeyPress(e);
    }
}
北方的巷 2024-10-03 22:19:43

您可以设置文本框的 AcceptsTab属性设置为true。这将允许您在控件中输入制表符。

但是,不支持开箱即用地使用 Shift+Tab 删除缩进。您可能必须处理 KeyPress 事件并自行删除插入符号下的制表符。

编辑:我误读了这个问题。要实现诸如 Visual Studio 的缩进/缩进功能之类的功能,请尝试以下操作:

private bool _shiftTabKeyDown;

private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
    _shiftTabKeyDown = false;
    if (e.KeyCode == Keys.Tab && e.Shift) {
        _shiftTabKeyDown = true;
    }
}

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
    if (textBox1.SelectionLength == 0 || e.KeyChar != '\t') {
        return;
    }

    bool startNewLineAdded = false;
    bool endNewLineRemoved = false;
    string selection = textBox1.SelectedText;

    if (!selection.StartsWith(Environment.NewLine)) {
        selection = Environment.NewLine + selection;
        startNewLineAdded = true;
    }

    if (selection.EndsWith(Environment.NewLine)) {
        selection = selection.SubString(0, selection.Length
            - Environment.NewLine.Length);
        endNewLineRemoved = true;
    }

    if (_shiftTabKeyDown) {
        selection = selection.Replace(Environment.NewLine + '\t',
            Environment.NewLine);
    } else {
        selection = selection.Replace(Environment.NewLine,
            Environment.NewLine + '\t');
    }

    if (startNewLineAdded) {
        selection = selection.SubString(Environment.NewLine.Length);
    }

    if (endNewLineRemoved) {
        selection += Environment.NewLine;
    }

    textBox1.SelectedText = selection;
    e.Handled = true;
}

You can set the TextBox' AcceptsTab property to true. That will allow you to enter tab characters into the control.

Removing indent using Shift+Tab, however, is not supported out of the box. You'll probably have to handle the KeyPress event and remove the tab character under the caret yourself.

EDIT: I misread the question. To achieve something like the indent/dedent feature of Visual Studio, try something like:

private bool _shiftTabKeyDown;

private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
    _shiftTabKeyDown = false;
    if (e.KeyCode == Keys.Tab && e.Shift) {
        _shiftTabKeyDown = true;
    }
}

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
    if (textBox1.SelectionLength == 0 || e.KeyChar != '\t') {
        return;
    }

    bool startNewLineAdded = false;
    bool endNewLineRemoved = false;
    string selection = textBox1.SelectedText;

    if (!selection.StartsWith(Environment.NewLine)) {
        selection = Environment.NewLine + selection;
        startNewLineAdded = true;
    }

    if (selection.EndsWith(Environment.NewLine)) {
        selection = selection.SubString(0, selection.Length
            - Environment.NewLine.Length);
        endNewLineRemoved = true;
    }

    if (_shiftTabKeyDown) {
        selection = selection.Replace(Environment.NewLine + '\t',
            Environment.NewLine);
    } else {
        selection = selection.Replace(Environment.NewLine,
            Environment.NewLine + '\t');
    }

    if (startNewLineAdded) {
        selection = selection.SubString(Environment.NewLine.Length);
    }

    if (endNewLineRemoved) {
        selection += Environment.NewLine;
    }

    textBox1.SelectedText = selection;
    e.Handled = true;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文