TextChanged

  • Thread starter Thread starter Nice Chap
  • Start date Start date
N

Nice Chap

TextBox.TextChanged Event fires even when the text is changed
programatically. How can I detect that the text has been changed by the user
and not changed programatically?

Thanks
 
Nice Chap,

Two ways.
Set a Boolean switch to evaluate that in the event
or
delete the handler when you do it programmatically

When you do it programmatically (and set it (back) afterwards)

I hope this helps,

Cor
 
Nice Chap said:
TextBox.TextChanged Event fires even when the text is changed
programatically. How can I detect that the text has been changed by the
user and not changed programatically?

You may want to set a flag (for example, by setting the control's 'Tag'
property) before changing the value programmatically and check this flag in
the 'TextChanged' event handler.
 
Herfried,
You may want to set a flag (for example, by setting the control's 'Tag'
property)

Is using that tag for that an idea from VBCom (I saw Larry once doing that),
I don't see the benefit from it.

However maybe can you explain that too me(serious)

Cor
 
Cor Ligthert said:
Is using that tag for that an idea from VBCom (I saw Larry once doing
that), I don't see the benefit from it.

However maybe can you explain that too me(serious)

Using the 'Tag' property might be easier because you don't need any
additional private variables. However, it's one of many possible solutions
and its up to personal preference whether to use 'Tag' or not.
 
Herfried,

I find (and found it forever) wrong to use datafields that have no
descriptive names only to save 1 machineword. (that forever was a while
after the time that I was using relays, however than we could not give that
relay a descriptive name). However it sounds for me a behaviour from that
time.

I hope you agree that.

Cor
 
Don't forget the MouseDown/MouseUp events, in case the user pastes text by
right-clicking and selecting Paste on the popup menu (if any).
 
Ok, I've read all of the other responses, but I have to ask: "Why do you have to tell it
to do anything."

Simply Dim a "global" variable as a string and then do this:

Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As _
System.EventArgs) Handles TextBox1.TextChanged

myvariable = TextBox1.Text

End Sub

it detects any changes.

Or is there something wrong with doing this.??


--
Steve Easton
Microsoft MVP FrontPage
95isalive
This site is best viewed..................
...............................with a computer
 
95isalive said:
Ok, I've read all of the other responses, but I have to ask: "Why do you
have to tell it
to do anything."

Simply Dim a "global" variable as a string and then do this:

Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As
_
System.EventArgs) Handles TextBox1.TextChanged

myvariable = TextBox1.Text

End Sub

it detects any changes.

Or is there something wrong with doing this.??


Well, the OP wants tol execute the code 'myvariable = TextBox1.Text' only if
the text has been changed by the user and not when it was changed
programmatically.

\\\
Private Sub TextBox1_TextChanged( _
ByVal sender As Object, _
ByVal e As EventArgs _
) Handles TextBox1.TextChanged
If Not DirectCast(sender, Control).Tag Then
...
End If
End Sub
..
..
..
With Me.TextBox1
.Tag = True
.Text = "Hello World!"
.Tag = False
End With
///
 
Or making it in my opinion as syntax nicer using Herfrieds sample

dim SwProramTextBox1Change as boolean
Private Sub TextBox1_TextChanged( _
ByVal sender As Object, _
ByVal e As EventArgs _
Handles TextBox1.TextChanged
If Not SwProgramTextBox1Change Then
...
End If
SwProgramChange = False
End Sub
///
\\\
SwProgramTextbox1Change = True
MeTextBox1.Text = "Hello World!"
///

:-)

Cor
 
Cor,

Cor Ligthert said:
Or making it in my opinion as syntax nicer using Herfrieds sample

dim SwProramTextBox1Change as boolean
Private Sub TextBox1_TextChanged( _
ByVal sender As Object, _
ByVal e As EventArgs _
Handles TextBox1.TextChanged
If Not SwProgramTextBox1Change Then
...
End If
SwProgramChange = False
End Sub
///
\\\
SwProgramTextbox1Change = True
MeTextBox1.Text = "Hello World!"
///

I thought about resetting the flag inside the 'TextChanged' event handler
too but then decided not to do that. By letting the user reset the
property/flag manually consecutive changes of the textbox's content are
possible.

\\\
With Me.TextBox1
.Tag = True
For i = 1 To 20
...
If ... Then
.Text = ...
End If
Next i
.Tag = False
End With
///

The code above will not execute the code in the 'TextChanged' event handler
even if the textbox' text is changed more than once.

Just my 2 Euro cents...
 
Herfried,

My main thing is that using a Tag (just because it is there) for what it is
not created for is in my opinion not right. (You know it is friday evening
and you know what that means, so I don't go in discussion anymore)

I think you are right about the side effect what you wrote. However don't
shoot me at the moment when I did understand you wrong.

:-))

Cor
 
Cor,

Cor Ligthert said:
My main thing is that using a Tag (just because it is there) for what it
is not created for is in my opinion not right. (You know it is friday
evening and you know what that means, so I don't go in discussion anymore)

Mhm... I don't think that the use of 'Tag' shown in my sample is that bad,
but as I already said, I accept that other people choose another solution.
I think you are right about the side effect what you wrote. However don't
shoot me at the moment when I did understand you wrong.

:-)
 
Back
Top