WinForms RichTextBox: how to format in TextChanged?

I have a RichTextBox that I want to reformat when the contents of the RichTextBox change. I have a TextChanged event handler.

Re-formatting (changing the colors of the selected areas) raises the TextChanged event. This results in an endless loop of the TextChange event, formatting, TextChange event, reformatting, etc.

How can I distinguish text changes from the application and text changes from the user?

I could check the length of the text, but not sure if this is completely correct.

+1
winforms richtextbox
source share
1 answer

You may have a bool flag indicating whether you are already in TextChanged processing:

 private bool _isUpdating = false; private void Control_TextChanged(object sender, EventArgs e) { if (_isUpdating) { return; } try { _isUpdating = true; // do your updates } finally { _isUpdating = false; } } 

This way you stop additional TextChanged events from creating a loop.

+3
source share

All Articles