views:

29

answers:

2

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

A: 

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;
}
Frédéric Hamidi
This does not solve the problem in question. It does not indent multiline selections
testalino
A: 

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