none
RichTextBox Color Changes When Selected RRS feed

  • Domanda

  • I stumble upon a strange problem. I am using a richtextbox for a log, and I color the text by the severity of the problem(Error, warranty, info,..). 
    When I am running the app, everything is great except when I am starting to play 
    with the text inside the textbox. when I am selecting one or more lines, it some times changes the color of the lines.


    The code:

       private void AddToLog(IEnumerable<LogMessageBase> exeMsgs)
            {
                lock (messageWriterLog)
                {
                    foreach (var message in exeMsgs)
                    {
                        switch (message.Severity)
                        {
                            case LogSeverity.Fatal:
                            case LogSeverity.Error:
                                logTextBox1.SelectionColor = Color.Red;
                                break;
                            case LogSeverity.Info:
                                logTextBox1.SelectionColor = Color.Brown;
                                break;
                            case LogSeverity.Warn:
                                logTextBox1.SelectionColor = Color.BlueViolet;
                                break;
                            case LogSeverity.SuccessMessage:
                                logTextBox1.SelectionColor = Color.Green;
                                break;
                        }
    
                        logTextBox1.SelectionStart = logTextBox1.Text.Length;
                        logTextBox1.ScrollToCaret();
                        logTextBox1.SelectedText = Environment.NewLine + message.GetMessageText();
                    }
                }
            }

    Before selecting the lines:
    Before the selecting the lines

     after selecting the lines:

    It seems that when I am selecting the lines, it changes the lines color to be the recent add line color.

    obviously I don't wont that the color will change.  

    Does anybody know this bug and know how to solve it?

    mercoledì 14 giugno 2017 14:06

Tutte le risposte

  • It works fine for me. Is there any pattern or scenario that you are selecting. Or is there any property that you set and did not mention on the rich text box.

    Tried following code :

           private void button3_Click(object sender, EventArgs e)
            {
                var messages = new List<LogMessageBase>();
                messages.Add(new LogMessageBase { Message = "*****************************************************************", Severity = LogSeverity.Fatal });
                messages.Add(new LogMessageBase { Message = "it more difficult to retrieve if the computer crashed, and logging would not be available unless the database was available. A plain text format minimises dependencies on ", Severity = LogSeverity.Fatal });
                messages.Add(new LogMessageBase { Message = "*****************************************************************", Severity = LogSeverity.Fatal });
                messages.Add(new LogMessageBase { Message = "Message2", Severity = LogSeverity.Error});
                messages.Add(new LogMessageBase { Message = "*****************************************************************", Severity = LogSeverity.Fatal });
                messages.Add(new LogMessageBase { Message = "sis if it is stored in a form that can be analysed. This data can be structured in many ways for analysis. For example, storing it in a relational database would force the data into a query-able format. However, it would also make it more difficult to retrieve if the computer crashed, and logging would not be available unless the database was available. A plain text format minimises dependencies on other system processes, and assists logging at all phases of computer operation, including start-up and shut-down, where such ", Severity = LogSeverity.Info });
                messages.Add(new LogMessageBase { Message = "*****************************************************************", Severity = LogSeverity.Fatal });
                messages.Add(new LogMessageBase { Message = "The information stored is only available for later analysis if it is stored in a form that can be analysed. This data can be structured in many ways for analysis. For example, storing it in a relational database would force the data into a query-able format. However, it would also make it more difficult to retrieve if the computer crashed, and logging would not be available unless the database was available. A plain text format minimises dependencies on other system processes, and assists logging at all phases of computer operation, including start-up and shut-down, where such processes might be unavailable", Severity = LogSeverity.SuccessMessage });
                messages.Add(new LogMessageBase { Message = "*****************************************************************", Severity = LogSeverity.Fatal });
                messages.Add(new LogMessageBase { Message = "Log files are a standard tool for computer systems developers and administrators. They record the what happened when by whom of the system. This information can record faults and help their diagnosis. It can identify security breaches and other computer misuse. It can be used for auditing. It can be used for accounting purposes", Severity = LogSeverity.SuccessMessage });
                messages.Add(new LogMessageBase { Message = "Message6", Severity = LogSeverity.Warn });
                AddToLog(messages);
            }
    
    
    
            private void AddToLog(IEnumerable<LogMessageBase> exeMsgs)
            {
                lock (exeMsgs)
                {
                    foreach (var message in exeMsgs)
                    {
                        switch (message.Severity)
                        {
                            case LogSeverity.Fatal:
                            case LogSeverity.Error:
                                logTextBox1.SelectionColor = Color.Red;
                                break;
                            case LogSeverity.Info:
                                logTextBox1.SelectionColor = Color.Brown;
                                break;
                            case LogSeverity.Warn:
                                logTextBox1.SelectionColor = Color.BlueViolet;
                                break;
                            case LogSeverity.SuccessMessage:
                                logTextBox1.SelectionColor = Color.Green;
                                break;
                        }
    
                        logTextBox1.SelectionStart = logTextBox1.Text.Length;
                        logTextBox1.ScrollToCaret();
                        logTextBox1.SelectedText = Environment.NewLine + message.GetMessageText();
                    }
                }
    
            }
            public enum LogSeverity
            {
                Fatal,
                Error,
                Info,
                Warn,
                SuccessMessage
            }
            
    
            internal class LogMessageBase
            {
                public LogSeverity Severity;
                public string Message;
                public string GetMessageText()
                {
                    return Message;
                }
            }

    Output:

    Please let me know..

    Thanks

    Hari


    Hari Mothukuri

    mercoledì 14 giugno 2017 16:59
  • Hi Barak Ganon,

    Thank you fro posting here.

    According to your question is more related to Winform, I will move it to Windows Forms General forum for suitable support.

    The Visual C# discuss and ask the C# programming language, IDE, libraries, samples and tools.

    If you have some grammar or code errors, please feel free to contact us. We will try our best to give you a solution.

    Thanks for your understanding and cooperation.

    Best Regards,

    Wendy


    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.

    giovedì 15 giugno 2017 02:55
  • Hey Hari, Thank you for your answer. There two things that I forget to mention, and one of them is important. The lass important is that the richtextbox is read only - but I don't think that the problem is related to that.

    The second thing is that is scheduled app, it runes every minute or so. So the richtextbox get constant stream of data. Actually the problem occurred while you select or underlying lines while new data(new lines) are being added to the textbox.  

      Barak.
    giovedì 15 giugno 2017 07:04
  • If you select the text while AddToLog is running, then obviously the colour and text will be applied to different (manually selected) area.

    Try reordering the operations and also keeping the selection:

    var ss = logTextBox1.SelectionStart;
    var sl = logTextBox1.SelectionLength;
    logTextBox1.SelectionStart = logTextBox1.Text.Length;
    logTextBox1.SelectionColor = . . .
    logTextBox1.SelectedText = . . .
    if( logTextBox1.Focused )
    {
       logTextBox1.SelectionStart = ss;
       logTextBox1.SelectionLength = sl;
    }

    Perhaps it has to be further improved to reduce undesirable flickering, etc.

    giovedì 15 giugno 2017 07:41
  • Hi Barak,

    >>It seems that when I am selecting the lines, it changes the lines color to be the recent add line color.

    Do you use some another events to set the selected-line's color?

    You can refer to Hari's demo and it does not change the color when any line is selected

    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.

    giovedì 15 giugno 2017 07:56
    Moderatore
  • Hi Barak,

    >>It seems that when I am selecting the lines, it changes the lines color to be the recent add line color.

    Do you use some another events to set the selected-line's color?

    You can refer to Hari's demo and it does not change the color when any line is selected

    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.

    Hi Stanly, Thank you for your answer.

    If you want to reproduce my bug, you need to insert a stream of data to the text box. I mean you need to do kind of clock operated app that insert data constantly to the textbox.

    If you will do so, you will see that when you selecting lines they will change there color to the color of the new line that was added. 

    Best Regards,

    Barak.

    giovedì 15 giugno 2017 08:33
  • Hi Barak,

    Sorry for the late reply.

    If it is possible I suggest you can make a simple demo for this issue and provide it to the Microsoft OneDrive so that I can reproduce it in my PC and find a solution.

    Microsoft OneDrive

    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.

    lunedì 3 luglio 2017 09:22
    Moderatore