Home All Groups Group Topic Archive Search About

Detecting textchanged event when user initiates it

Author
27 Oct 2006 3:24 PM
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

Author
27 Oct 2006 3:34 PM
Michael
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

Show quoteHide quote
"Jerry Spence1" wrote:

> 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
>
>
>
Author
27 Oct 2006 3:43 PM
Chris
Jerry Spence1 wrote:
> 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.
Author
27 Oct 2006 3:53 PM
Jerry Spence1
Show quote Hide quote
"Chris" <no@spam.com> wrote in message
news:uVQYn6d%23GHA.1196@TK2MSFTNGP02.phx.gbl...
> Jerry Spence1 wrote:
>> 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.

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
Author
27 Oct 2006 4:04 PM
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

Jerry Spence1 wrote:
Show quoteHide quote
> 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
>
>
Author
27 Oct 2006 4:30 PM
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.

-----
Tim Patrick
Start-to-Finish Visual Basic 2005

Show quoteHide quote
> 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
>
Author
27 Oct 2006 4:32 PM
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.

Tim Patrick wrote:
Show quoteHide quote
> 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.
>
> -----
> Tim Patrick
> Start-to-Finish Visual Basic 2005
>
>> 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
>>
>
>
Author
28 Oct 2006 3:56 PM
Dennis
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.
--
Dennis in Houston


Show quoteHide quote
"Theo Verweij" wrote:

> I don't think this is a bad habit - I call it an
> EventFlowControlVariable - it does wonders in a lot of scenario's.
>
> Tim Patrick wrote:
> > 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.
> >
> > -----
> > Tim Patrick
> > Start-to-Finish Visual Basic 2005
> >
> >> 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
> >>
> >
> >
>