0

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)?

3 Answers 3

0

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

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;
}
1
  • This does not solve the problem in question. It does not indent multiline selections
    – testalino
    Oct 12, 2010 at 9:37
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.