Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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.

Posted

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 :)

  • *Experts*
Posted

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

"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
Posted
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.
[sIGPIC]e[/sIGPIC]

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...