Jay1b Posted November 10, 2006 Posted November 10, 2006 Hi I have a richtextbox with a textchanged event. This event fires when the text box is first loaded with data, understandably. This is the code: Private Sub rtfText_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles rtfText.TextChanged bHasChanged = True End Sub I dont want bHasChanged to be set to true, upon loading, just when the user changes something. This value is then checked upon closing, and uses it to determine whether to prompt about saving changes. I can solve this by having a 'bFirstLoad' like below: Dim bFirstLoad as boolean = True Private Sub rtfText_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles rtfText.TextChanged If bFirstLoad Then bFirstLoad = False Else bHasChanged = True End If End Sub This will solve the problem, but it just seem unprofessional to me, is there a better way of doing this? Thanks. Quote
mskeel Posted November 10, 2006 Posted November 10, 2006 Are you using VB.Net 2005? This looks like it might do the trick. TextBoxBase.ModifiedChanged Event Quote
Jay1b Posted November 10, 2006 Author Posted November 10, 2006 Yes I am using 2005. I did actually look at that event, but it still fired when the data was loaded (although apparently it shouldnt do accordingly to that link). However looking at it again made me spot the richtextbox.modified. After setting this to false after loading i can use this just like i was the bhaschanged variable, and it looks so much more readable as well! So thank you :) Quote
*Experts* Nerseus Posted November 10, 2006 *Experts* Posted November 10, 2006 Glad you found it, and glad to learn about the new Modified property - I hadn't seen that before. A more general solution to avoiding the "loading" flags you mention, at least something we've been using for a few years: early-out of an event if the form's active control isn't the sender. Finally! A great use for "sender" when you're not sharing event handlers! Here's a sample - the syntax may be wrong, I'm a C# guy: Private Sub rtfText_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles rtfText.TextChanged If Me.ActiveControl != sender Then Exit Sub End Sub -ner Quote "I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
Leaders snarfblam Posted November 10, 2006 Leaders Posted November 10, 2006 Maybe I'm missing something, but why can't you store the original RTF? When the application closes or a new document is opened compare the current RTF to the original RTF and if they are not equal ask the user if he would like to save. Quote [sIGPIC]e[/sIGPIC]
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.