473,388 Members | 1,377 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

Strategies for controlling cascading events for interdependent controls on a form?


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

Jun 19 '07 #1
4 2123
On Jun 19, 11:07 am, Joergen Bech
<jbech<NOSPAM>@<NOSPAM>post1.tele.dkwrote:
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
I have dealt with this issue by using different events and not the
"Changed" event. Try attaching your events to other members like
txtTextBox.KeyUp() or txtTextBox.KeyPress(). these events are
external and will not trigger other events when changed strings in
textboxs.

I have been a victim of this a few times, but NO MORE! lol

cheers!

Jun 19 '07 #2
On Jun 19, 1:27 pm, Lucas <lucaslshaf...@gmail.comwrote:
On Jun 19, 11:07 am, Joergen Bech

<jbech<NOSPAM>@<NOSPAM>post1.tele.dkwrote:
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

I have dealt with this issue by using different events and not the
"Changed" event. Try attaching your events to other members like
txtTextBox.KeyUp() or txtTextBox.KeyPress(). these events are
external and will not trigger other events when changed strings in
textboxs.

I have been a victim of this a few times, but NO MORE! lol

cheers!- Hide quoted text -

- Show quoted text -
Example from what you showed above...

Private Sub TextBox1_TextChanged(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles TextBox1.KeyPress
_myText = TextBox1.Text
DisplayValues()
End Sub

Jun 19 '07 #3

--- snip---
>I have dealt with this issue by using different events and not the
"Changed" event. Try attaching your events to other members like
txtTextBox.KeyUp() or txtTextBox.KeyPress(). these events are
external and will not trigger other events when changed strings in
textboxs.

I have been a victim of this a few times, but NO MORE! lol

cheers!- Hide quoted text -

- Show quoted text -

Example from what you showed above...

Private Sub TextBox1_TextChanged(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles TextBox1.KeyPress
_myText = TextBox1.Text
DisplayValues()
End Sub
Yes, in this case you could get away with it because KeyPress,
MouseDown, etc etc per definition are "external" events (or user
events, if you like), but such a scheme requires that you stick to
a list of "safe" events. I am talking about a completely generic
system where everything happens in response to a single event
and no other events are accepted until everything has "come to rest"
again, so to speak.

An "external" event could also be if an object bound to a form
fires a custom Changed event and expect the form to reflect the
changes in that object, but not modify the object because of the
Changed event.

How about three complex usercontrols displaying different aspects
of a single object. When one usercontrol modifies a property of the
object, the object should raise a Changed event, which the other
usercontrols should use to display their aspect of the object.
In such a case the other usercontrols do not know - nor should they
care - that the change happened because of a KeyPress event in
another usercontrol.

Not sure I have made myself clear. I deal with graphical controls
with lots of interdependent objects with interactions that go some way
beyond the usual master-detail CRUD scenarios.

Regards,

Joergen Bech

Jun 19 '07 #4
On Jun 19, 1:57 pm, Joergen Bech <jbech<NOSPAM>@<NOSPAM>post1.tele.dk>
wrote:
--- snip---


I have dealt with this issue by using different events and not the
"Changed" event. Try attaching your events to other members like
txtTextBox.KeyUp() or txtTextBox.KeyPress(). these events are
external and will not trigger other events when changed strings in
textboxs.
I have been a victim of this a few times, but NO MORE! lol
cheers!- Hide quoted text -
- Show quoted text -
Example from what you showed above...
Private Sub TextBox1_TextChanged(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles TextBox1.KeyPress
_myText = TextBox1.Text
DisplayValues()
End Sub

Yes, in this case you could get away with it because KeyPress,
MouseDown, etc etc per definition are "external" events (or user
events, if you like), but such a scheme requires that you stick to
a list of "safe" events. I am talking about a completely generic
system where everything happens in response to a single event
and no other events are accepted until everything has "come to rest"
again, so to speak.

An "external" event could also be if an object bound to a form
fires a custom Changed event and expect the form to reflect the
changes in that object, but not modify the object because of the
Changed event.

How about three complex usercontrols displaying different aspects
of a single object. When one usercontrol modifies a property of the
object, the object should raise a Changed event, which the other
usercontrols should use to display their aspect of the object.
In such a case the other usercontrols do not know - nor should they
care - that the change happened because of a KeyPress event in
another usercontrol.

Not sure I have made myself clear. I deal with graphical controls
with lots of interdependent objects with interactions that go some way
beyond the usual master-detail CRUD scenarios.

Regards,

Joergen Bech- Hide quoted text -

- Show quoted text -
Yes, I understand better now. I am not aware of any semaphores used
to delegate interactions between objects. I do know that what you
have described in the initial post seems to exactly what you
described.

Hopefully, others can give a better example than me.

cheers!

Jun 19 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Kate | last post by:
I am learning VB by working on an existing application. It is dawning on me that this particular program was not designed very carefully, and I have come to the point where I have to deal with...
0
by: Scott McChesney | last post by:
I'm writing a Windows application for a client, and I've run into a problem I don't know how to solve. The UI is relatively simple - a listbox on the left (in a panel), and a third-party tab...
8
by: CJack | last post by:
hy, I have an mdi application, i create a child form and I want to know when a button is pressed while that child form is loaded. I have this code: private void frmTestBaby_KeyUp(object sender,...
3
by: Crucifix | last post by:
Hello, I'm writing a small C# app, and part of what I'm trying to do involves the dragging of PictureBox controls on a form. Unfortunately, MouseMove seems to be behaving very oddly, causing...
8
by: GaryDean | last post by:
We have been noticing that questions on vs.2005/2.0 don't appear to get much in answers so I'm reposting some questions posted by some of the programmers here in our organization that never got...
4
Rabbit
by: Rabbit | last post by:
Cascading Combo/List Boxes This tutorial is to guide you in the creation of Cascading combo/list boxes. That is when you have multiple combo/list boxes where the selection of an option in one...
3
kcdoell
by: kcdoell | last post by:
I have 5 cascading combo boxes on a form. Below is a sample of my vb in the first combo box: Private Sub CboDivision_AfterUpdate() 'When the Division is selected, the appropriate Segment...
16
by: Tarheel | last post by:
...You're fast! I appreciate that. Thanks! I look forward to your explanation. One last problem I have on my form is getting all of my comboboxes to update each other. I know how to update...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.