RichTextBox: what event should I use?

robertb_NZ

Well-known member
Joined
May 11, 2010
Messages
146
Location
Auckland, New Zealand
Programming Experience
10+
I have a richtextbox that can be edited, also the text may change as a result of my program. I have some logic that I want to trigger when the text has been changed by editing, but I don't want the logic for changes caused by my program. Currently I solve this problem with the following rather clumsy code, in which I have a Boolean called "Edited" that I set false whenever my program logic makes a change: -
Dim Edited as Boolean = True ' Use together with Dirty to decide how to handle RTB.Text
...
' Somewhere in my program logic
Edited = false
RTB.Text = "Some new value"
...
Private Sub RTB_TextChanged(sender As System.Object, e As System.EventArgs) Handles RTB.TextChanged
' Event may be triggered by editing, or by processing like reading a file, parsing it, etc
If Edited = False Then ' Set true for File/Open, and program-caused changes during editing
Edited = True ' Maintain default value
Exit Sub
End If
...

I'd like something that behaves like TextChanged in ASP.NET, where its definition in that environment: -
"The TextChanged event is raised when the content of the text box changes between posts to the server"
means that program-caused changes don't raise the event. However I can't see any such event, and so it seems that I'm stuck with clumsy code like that above. Has anybody got a better idea?

Thank you, Robert
 
Using a Boolean in code is one way to do it, another is to add/remove event handlers dynamically using AddHandler/RemoveHandler statement. A third option is to write a class that inherits RichTextBox and override CanRaiseEvents method to dynamically alter the behaviour for when events can be raised or not.

ASP.Net is an altogether different environment and you can't compare those two.
 
Back
Top