Click here to Skip to main content
15,921,371 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
How can I add a TAB(or any other character) to the begin of each line in the selected text?
I only found these Pieces of code, but I don't know how to do the rest
Sergey Alexandrovich Kryukov 15-Nov-11 12:26pm    
Don't try to "find" piece of code, write it. :-)
What's the problem, anyway?
BillWoodruff 16-Nov-11 2:32am    
There is a clear question here: how do you add a tab character to the start of each line in the current selection.

Can you use this?


And can we have some more info on the problem as the poster above requested?
Share this answer
1. Write a loop that will go through the text.
2. Every time you detect a new line character you will have to add a "\t" in that index.
Share this answer
Try this to get started:
private void button4_Click(object sender, EventArgs e)
        // get the selected text
        string selText = allText.Substring(richTextBox1.SelectionStart, richTextBox1.SelectionLength);

        // get the collection of Lines
        string[] rtfLines = richTextBox1.Lines;

        string theLine;

        for (int i = 0; i < rtfLines.Length; i++)
            // compare each line
            theLine = rtfLines[i];

            // to see if it's in the selection
            if (selText.Contains(theLine))
                // in selection : add a tab
                rtfLines[i] = "\t" + theLine;

        // reset the content of the RichTextBox
        // this necessary because changing a line in the
        // collection of Lines does not affect the RTf content
        richTextBox1.Lines = rtfLines;
Note this only will add a tab at the start of lines that are fully contained in the current selection. If you have a partial selection in a line, that will be ignored.

Tabbing in a partial selection could be achieved, but that's another chapter: perhaps one you will write ?
Share this answer
ikkentim 16-Nov-11 3:46am    
Got it working with this, thanks,
Used a code i wrote by myself yesterday, but it seems like i can'tpost an solution by myself..
BillWoodruff 16-Nov-11 5:07am    
Good that you got it working on your own ! I think you can post a solution to your own question here, and I encourage you to do so; we can all learn from the different ways we might have used to address the same problem.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900