question

Chocolade-4229 avatar image
0 Votes"
Chocolade-4229 asked Viorel-1 commented

Why part of the string in the richTextBox is colored in green?

 private void ColorRichTextbox(RichTextBox box, string text, Color color, bool appendNewLine = true)
         {
             int length = box.TextLength;
             if (!string.IsNullOrWhiteSpace(box.Text) && appendNewLine)
             {
                 box.AppendText("\r\n" + text);
             }
             else
             {
                 box.AppendText(text);
             }
             box.SelectionStart = length;
             box.SelectionLength = text.Length;
             box.SelectionColor = color;
         }

Then in button click event the first line in the richTextBox will be light green :

 ColorRichTextbox(richTextBoxLogChanges, "Watching Event Started : ", Color.White);
 ColorRichTextbox(richTextBoxLogChanges, DateTime.Now.ToString(), Color.LightGreen, false);


Then i'm adding another line in yellow :

 private void Fsw_Deleted(object sender, FileSystemEventArgs e)
         {
             ColorRichTextbox(richTextBoxLogChanges, $"Deleted: {e.FullPath}", Color.Yellow);
         }

The yellow line the last char get the light green color of the first line :

In the screenshot in this example the last char S of the css is in light green and it should be in yellow.

195880-line1.jpg



Screenshot of the last problem mentioned in my comment/s to the solution :

195932-watcher1.jpg


dotnet-csharpwindows-forms
line1.jpg (9.7 KiB)
watcher1.jpg (38.2 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

Viorel-1 avatar image
0 Votes"
Viorel-1 answered Viorel-1 commented

Check a possible fix:

 private void ColorRichTextbox(RichTextBox box, string text, Color color, bool appendNewLine = true)
 {
    int length = box.TextLength;
    int sl;
    if (!string.IsNullOrWhiteSpace(box.Text) && appendNewLine)
    {
       box.AppendText("\r\n" + text);
       sl = text.Length + 2;
    }
    else
    {
       box.AppendText(text);
       sl = text.Length;
    }
    box.SelectionStart = length;
    box.SelectionLength = sl;
    box.SelectionColor = color;
 }

· 4
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

It's working for the first time but then when i add the line in yellow again it's adding it twice instead once.

0 Votes 0 ·

I just edited my question added the full code but it's waiting for admin to be improved.


When i click the button btnWacther it's start watching write a line in light green then when i delete a file or two or more it's adding yellow lines of the deleted files.
Then i click the button btnWacther again to end this event of watching.

Then i click the button btnWacther again to make a new event watching and this time when i delete a file it's adding the yellow line twice instead one.

Here is a screenshot :
On the second Watching Event Started the yellow line added twice instead once.


Here is a screenshot : https://imgur.com/a/NQVHiLH

NQVHiLH


0 Votes 0 ·

It's working for the first time but then when i start a second event of watching and deleting one file it's adding the yellow line twice instead once. i added a screenshot to my question at the bottom showing the problem.

0 Votes 0 ·
Viorel-1 avatar image Viorel-1 Chocolade-4229 ·

Maybe the problem is solved, but the issue is caused by a code that is not shown, or maybe the file system watcher generates duplicate events in some cases.

0 Votes 0 ·