J
Joergen Bech
Just out of curiosity: What is your favorite method of making sure
that anything that happens on a form, only happens in response to
a single, external event?
Take the example below. I have made it as simple as I could:
Put two textboxes on a form, paste the code and run it.
The idea is that whatever is typed in one box is displayed in the
other box, only reversed. This is a simple example of two repre-
sentations of the same data on a single form.
---snip---
Public Class Form1
Private _myText As String
Private Sub DisplayValues()
Debug.WriteLine("----------")
Debug.WriteLine(_myText)
Debug.WriteLine(ReverseString(_myText))
TextBox1.Text = _myText
TextBox2.Text = ReverseString(_myText)
End Sub
Private Function ReverseString(ByVal value As String) As String
Dim reverse() As Char = value.ToCharArray
Array.Reverse(reverse)
Return New String(reverse)
End Function
Private Sub TextBox1_TextChanged(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles TextBox1.TextChanged
_myText = TextBox1.Text
DisplayValues()
End Sub
Private Sub TextBox2_TextChanged(ByVal sender As Object, ByVal e
As System.EventArgs) Handles TextBox2.TextChanged
_myText = ReverseString(TextBox2.Text)
DisplayValues()
End Sub
End Class
---snip---
Problem is: If I start editing the value in TextBox1, I get a
TextBox1_TextChanged event, which will update the value
in TextBox2, which in turn causes TextBox2_TextChanged to
fire, which in turn ... and so on until the program runs out of
stack space.
We only want to respond to the first, external event, so the
natural inclination is to set a boolean variable which tells us
if an event is external or internal to the form. If internal, the
event should be ignored.
My own approach looks something like this:
Private Sub SomeObject_SomeEvent(...) Handles ...
If Me.AcceptEvents Then
Try
(update data)
DisplayValues
Finally
Me.EnableEvents
End Try
End Sub
All my forms are inherited from a base form class that implements
the AcceptEvents function and EnableEvents method.
AcceptEvents will return True *and* set the internal _acceptEvents
variable to False *if* _acceptEvents is True.
EnableEvents just sets _acceptEvents to True.
This allows me to enclose all my event code in as few lines as
possible(?), yet ensures that any event I process is only in response
to a single, external event (such as a mouseclick, keydown, etc).
The question is: Is there an elegant, generalized .Net way of
doing this or do you just add such boolean variables as you go
along, depending on whether or not the form needs it?
What I have works for me and I am quite happy with it, but I
would like to see other approaches to dealing with this problem.
Not looking for a solution to the specific sample, but general
approaches. Any suggestions?
Regards,
Joergen Bech
that anything that happens on a form, only happens in response to
a single, external event?
Take the example below. I have made it as simple as I could:
Put two textboxes on a form, paste the code and run it.
The idea is that whatever is typed in one box is displayed in the
other box, only reversed. This is a simple example of two repre-
sentations of the same data on a single form.
---snip---
Public Class Form1
Private _myText As String
Private Sub DisplayValues()
Debug.WriteLine("----------")
Debug.WriteLine(_myText)
Debug.WriteLine(ReverseString(_myText))
TextBox1.Text = _myText
TextBox2.Text = ReverseString(_myText)
End Sub
Private Function ReverseString(ByVal value As String) As String
Dim reverse() As Char = value.ToCharArray
Array.Reverse(reverse)
Return New String(reverse)
End Function
Private Sub TextBox1_TextChanged(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles TextBox1.TextChanged
_myText = TextBox1.Text
DisplayValues()
End Sub
Private Sub TextBox2_TextChanged(ByVal sender As Object, ByVal e
As System.EventArgs) Handles TextBox2.TextChanged
_myText = ReverseString(TextBox2.Text)
DisplayValues()
End Sub
End Class
---snip---
Problem is: If I start editing the value in TextBox1, I get a
TextBox1_TextChanged event, which will update the value
in TextBox2, which in turn causes TextBox2_TextChanged to
fire, which in turn ... and so on until the program runs out of
stack space.
We only want to respond to the first, external event, so the
natural inclination is to set a boolean variable which tells us
if an event is external or internal to the form. If internal, the
event should be ignored.
My own approach looks something like this:
Private Sub SomeObject_SomeEvent(...) Handles ...
If Me.AcceptEvents Then
Try
(update data)
DisplayValues
Finally
Me.EnableEvents
End Try
End Sub
All my forms are inherited from a base form class that implements
the AcceptEvents function and EnableEvents method.
AcceptEvents will return True *and* set the internal _acceptEvents
variable to False *if* _acceptEvents is True.
EnableEvents just sets _acceptEvents to True.
This allows me to enclose all my event code in as few lines as
possible(?), yet ensures that any event I process is only in response
to a single, external event (such as a mouseclick, keydown, etc).
The question is: Is there an elegant, generalized .Net way of
doing this or do you just add such boolean variables as you go
along, depending on whether or not the form needs it?
What I have works for me and I am quite happy with it, but I
would like to see other approaches to dealing with this problem.
Not looking for a solution to the specific sample, but general
approaches. Any suggestions?
Regards,
Joergen Bech