|
-
Jul 16th, 2007, 06:46 PM
#1
Thread Starter
Frenzied Member
[RESOLVED] [2005] Mutually Aware Controls
Let's say I make a user control that is a composite of just a single Button control. Now I place two of those on a form.
Say, at runtime, I wanted to be able to change the backcolor of one control and have the other control pick up on the BackColor changed event of the first control and also change it's backcolor to match the first control's backcolor (just an example).
But I don't want to handle the event in the using application. I want the controls themselves to listen for and pick up on the event sent by other controls of the same type.
Is it possible to have one control listen for events that another control raises?
Thanks
-
Jul 16th, 2007, 09:59 PM
#2
Thread Starter
Frenzied Member
Re: [2005] Mutually Aware Controls
Ok, yes it works.
Each control just has to maintain a list of it's "sibling" controls and add a handler for each sibling control's event.
When the sibling raises an event it's "sister controls" pick up the event.
So this allows controls to communicate with each other independent of code in the using application.
I guess I should have known this to begin with...but now I know for sure
-
Jul 16th, 2007, 10:34 PM
#3
Re: [2005] Mutually Aware Controls
You only need one collection, e.g.
vb.net Code:
Public Class UserControl1
Private Shared instances As New List(Of UserControl1)
Public Sub New()
' This call is required by the Windows Form Designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
UserControl1.instances.Add(Me)
End Sub
Private Sub UserControl1_BackColorChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.BackColorChanged
For Each instance As UserControl1 In UserControl1.instances
If instance.BackColor <> Me.BackColor Then
instance.BackColor = Me.BackColor
End If
Next instance
End Sub
Private Sub UserControl1_Disposed(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Disposed
UserControl1.instances.Remove(Me)
End Sub
End Class
-
Jul 16th, 2007, 11:06 PM
#4
Thread Starter
Frenzied Member
Re: [2005] Mutually Aware Controls
Thanks jm
I should have known that too.
I'll change the code.
Thanks for taking the time.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|