Detecting textchanged event when user initiates it

J

Jerry Spence1

I want to fire an event when the user changes text, but I don't want to fire
it when the program changes the text. This must be a very common thing to
do, and I was wondering if there was a better event to use. I know there is
MouseClick but the user might not actually change anything. Any ideas?

-Jerry
 
G

Guest

Hi Jerry,
This may not be what your looking for, but what I do is set a form level
Bool (isLoading) value to true when the program is making changes, and in the
TextChanged event I put this line at the top of the event:
if isLoading then exit sub

Hope this helps.
Michael
 
C

Chris

Jerry said:
I want to fire an event when the user changes text, but I don't want to fire
it when the program changes the text. This must be a very common thing to
do, and I was wondering if there was a better event to use. I know there is
MouseClick but the user might not actually change anything. Any ideas?

-Jerry

If you have the code that changes the text centralized, you can remove
the handler to the textbox and then add it back on after you finished
your changes. This allows you to control when the event gets handled
and when it doesn't. You will need to remove the "Handles" tag from the
function and use addhandler & removehandler to do it instead.
 
J

Jerry Spence1

Chris said:
If you have the code that changes the text centralized, you can remove the
handler to the textbox and then add it back on after you finished your
changes. This allows you to control when the event gets handled and when
it doesn't. You will need to remove the "Handles" tag from the function
and use addhandler & removehandler to do it instead.

Thanks Chris - I might as well have a boolean which I set to True before
changing the text by program and in the changetext event detect it and exit
the sub. This all seems very messy and I was wondering if there was another
property or method that might do it in a neater way.

-Jerry
 
T

Theo Verweij

Private Sub TextBox1_Enter(ByVal sender As Object, ByVal e As
System.EventArgs) Handles TextBox1.Enter
'Save the original text
TextBox1.Tag = TextBox1.Text
End Sub

Private Sub TextBox1_Validating(ByVal sender As Object, ByVal e As
System.ComponentModel.CancelEventArgs) Handles TextBox1.Validating
'After editing
If CType(TextBox1.Tag, String) <> TextBox1.Text Then
TextBox1_TextChanged(sender, New System.EventArgs)
End If
End Sub

Private Sub TextBox1_TextChanged(ByVal sender As System.Object,
ByVal e As System.EventArgs)
'The text has been changed ...
End Sub
 
T

Tim Patrick

I picked up a bad habit in my VB3 days that I never got rid of. I set a form-level
variable that is named "IgnoreChanges" to True when I first initialize the
form's data and any other time I want to make changes myself. When I'm done
initializing, I set it to False. Then in the event handler, I only do my
special processing if the flag is set to False. There is probably a more
official way, such as checking for Me.Visible or another member like that.
 
T

Theo Verweij

I don't think this is a bad habit - I call it an
EventFlowControlVariable - it does wonders in a lot of scenario's.
 
G

Guest

I would suggest the "removehandler" and the "addhandler" way to go. When you
are updating from the program, you can remove the handler then add it back in
thus no need to keep track of a form variable. When you have a lot of
different controls to update from different places in the program, it's much
easier to keep track using the handler. I used to use the form variable in
VB3, etc. but found the handler manipulation much easier to keep track of and
not much more code usually.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top