locked
Why when padding lines with numbers in a richTextBox it's coloring not only the padding numbers but the whole text ? RRS feed

  • Question

  • In the top of form1

    private List<string> oldlines = new List<string>();

    In the constructor

    richTextBox2.TabStop = false;
    richTextBox1.TabStop = false;
    string text = File.ReadAllText(@"C:\new 2.txt");
    richTextBox1.Text = text;
    string[] lines = richTextBox1.Lines;
    richTextBox2.Lines = lines;
    oldlines = richTextBox1.Lines.ToList();
    AddNumbersToLines(richTextBox1, 5);
    RichTextboxColorFirstLetter(richTextBox1, Color.Red);

    The AddNumbersToLines method:

    private void AddNumbersToLines(RichTextBox Rchtxt, int PadValue)
            {
                List<string> lines = new List<string>();
                lines = richTextBox1.Lines.ToList();
    
                for (int i = 0; i < lines.Count; i++)
                {
                    lines[i] = (i + 1).ToString("D" + PadValue) + " " + oldlines[i];
                }
                Rchtxt.Lines = lines.ToArray();
            }

    The method RichTextboxColorFirstLetter:

    private void RichTextboxColorFirstLetter(RichTextBox Rchtxt, Color color)
            {
                for (int i = 0; i < Rchtxt.Lines.Length; i++)
                {
                    int index = Rchtxt.Lines[i].IndexOf(" ");
                    string text = Rchtxt.Lines[i].Substring(0, index);
                    Rchtxt.Select(Rchtxt.GetFirstCharIndexFromLine(i), text.Length);
                    Rchtxt.SelectionColor = color;
                }
            }

    Then i'm using a numericupdown to change the pad value:

    private void numericUpDown1_ValueChanged(object sender, EventArgs e)
            {
                AddNumbersToLines(richTextBox1, (int)numericUpDown1.Value);
            }
    First time i'm running the program it's padding the lines with numbers and then coloring only the numbers.

    The problem is when i change the pad value with the numericupdown.

    It's changing the padding numbers fine but the problem is that for some reason the whole text is get color and not only the padding numbers like when running the program in the first time.

    This is what happening when changing the numericupdown value:

    What i want to do is first time when running the program pad the lines with numbers then color only the padding numbers.

    Then when changing the numericupdown value keep coloring only the padding numbers and not the whole text.

    Thursday, July 13, 2017 10:59 AM

Answers

  • Hi Chocolade1972,

    Please also set the color in the numericUpDown1_ValueChanged event:

            private void numericUpDown1_ValueChanged(object sender, EventArgs e)
            {
                AddNumbersToLines(richTextBox1, (int)numericUpDown1.Value);
    
                richTextBox1.SelectAll();
                richTextBox1.SelectionColor = Color.Black;
                RichTextboxColorFirstLetter(richTextBox1, Color.Red);
            }

    Hope this helps!

    Best Regards,

    Stanly


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Proposed as answer by Stanly Fan Friday, July 14, 2017 8:58 AM
    • Marked as answer by Chocolade1972 Friday, July 14, 2017 5:57 PM
    Friday, July 14, 2017 8:53 AM