locked
Why when i store old list items the list is changing later ? I want to update the richTextBox with the new padding value RRS feed

  • Question

  • What i want to do is when using the numericupdown to change the padding number to change in real time the padding numbers of the lines in the richTextBox1.

    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 method AddNumbersToLines

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

    And the numericupdown event

    private void numericUpDown1_ValueChanged(object sender, EventArgs e)
            {
                AddNumbersToLines(richTextBox1, (int)numericUpDown1.Value);
            }

    First time when running the program it's padding the lines by 5. For example:

    00001 Hello

    00002 Hello World

    00003 Hi

    Now i want that when i change the value in the numericUpDown1_ValueChanged event to change the padding for all the lines in real time.

    So if i change the value of numericupdown for example to 3 i want that all the lines numbers will be changing to:

    001 Hello

    002 Hello World

    003 Hi

    But i can't figure out how to do it.

    I thought maybe to use the oldlines List in the method AddNumbersToLines so i will reset lines each time i'm changing the value and assign to lines the oldlines and then will add to lines the new padding numbers.

    But oldlines never keep the oldlines it change to the new lines with the padding numbers.

    Anyway my goal is to change the lines numbers padding in real time but i can't manage how to do it.



    Wednesday, July 12, 2017 11:15 PM

Answers

  • Hi Chocolade1972,

    List<T> is the reference type, so if you set the new line as below:

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

    Then the oldlines also will be changed when you try to change the new line named lines:

                for (int i = 0; i < lines.Count; i++)
                {
                    lines[i] = (i + 1).ToString("D" + PadValue) + " " + lines[i];
                }

    Solution:

    Change the contents within the AddNumbersToLines method as below:

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

    And do not set the value in the constructor:

    //AddNumbersToLines(richTextBox1, 5);

    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 Thursday, July 13, 2017 9:02 AM
    • Marked as answer by Chocolade1972 Thursday, July 13, 2017 10:39 AM
    Thursday, July 13, 2017 2:56 AM

All replies

  • Hi Chocolade1972,

    List<T> is the reference type, so if you set the new line as below:

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

    Then the oldlines also will be changed when you try to change the new line named lines:

                for (int i = 0; i < lines.Count; i++)
                {
                    lines[i] = (i + 1).ToString("D" + PadValue) + " " + lines[i];
                }

    Solution:

    Change the contents within the AddNumbersToLines method as below:

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

    And do not set the value in the constructor:

    //AddNumbersToLines(richTextBox1, 5);

    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 Thursday, July 13, 2017 9:02 AM
    • Marked as answer by Chocolade1972 Thursday, July 13, 2017 10:39 AM
    Thursday, July 13, 2017 2:56 AM
  • Hi, Stanly

    This is working great.

    I have another small question please.

    In the constructor i'm calling this method once:

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

    And in the constructor:

    RichTextboxColorFirstLetter(richTextBox1, Color.Red);

    And now i'm not calling in the constructor the line:

    AddNumbersToLines(richTextBox1, 5);

    Removed this line from the constructor. ( Btw: if i want to start the program with already padding numbers where should i call this line from ? )

    But after removing this line i'm getting exception inside the RichTextBoxColorFirstLetter since there are no padding numbers yet so it can't fine space index " "

    When i'm running the program it does color only the pad numbers and not all the lines. And that is fine but:

    The problem is when i change the pad value using the numericupdown it's also changing the color of all the lines all the text with in the richTextBox and i want to change the color only of the padding numbers. 


    Thursday, July 13, 2017 7:37 AM
  • I see now that it's nothing to do with the method RichTextBoxColorFirstLetter.

    I mean in the constructor it's coloring the numbers once.

    But then when changing the numericupdown value and the pad value it's not calling again the RichTextBoxColorFirstLetter but it still coloring the whole text. The numbers in red and the lines the text in oragne.

    This is when running the program first time:

    And this is after changing the numericupdown value

    Not sure why it's coloring the whole text.

    I'm calling the method RichTextBoxColorFirstLetter only once in the constructor.

    Later i will want to call RichTextBoxColorFirstLetter also inside the numericupdown1_valuechanged event too. But now only in the constructor once and it keep coloring the whole text when changing the numericupdown value( Also the pad value ).

    Thursday, July 13, 2017 8:18 AM
  • Hi Chocolade1972,

    Please post a new thread for a new question and close this thread first by marking the reply as answer if my reply provided you with a solution as this will help others looking for the same or similar issues down the road.

    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.

    • Edited by Stanly Fan Thursday, July 13, 2017 9:01 AM
    Thursday, July 13, 2017 9:00 AM