Results 1 to 4 of 4

Thread: [RESOLVED] [2005] Mutually Aware Controls

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2006
    Posts
    1,051

    Resolved [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

  2. #2

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2006
    Posts
    1,051

    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

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] Mutually Aware Controls

    You only need one collection, e.g.
    vb.net Code:
    1. Public Class UserControl1
    2.  
    3.     Private Shared instances As New List(Of UserControl1)
    4.  
    5.     Public Sub New()
    6.         ' This call is required by the Windows Form Designer.
    7.         InitializeComponent()
    8.  
    9.         ' Add any initialization after the InitializeComponent() call.
    10.  
    11.         UserControl1.instances.Add(Me)
    12.     End Sub
    13.  
    14.     Private Sub UserControl1_BackColorChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.BackColorChanged
    15.         For Each instance As UserControl1 In UserControl1.instances
    16.             If instance.BackColor <> Me.BackColor Then
    17.                 instance.BackColor = Me.BackColor
    18.             End If
    19.         Next instance
    20.     End Sub
    21.  
    22.     Private Sub UserControl1_Disposed(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Disposed
    23.         UserControl1.instances.Remove(Me)
    24.     End Sub
    25.  
    26. End Class
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2006
    Posts
    1,051

    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
  •  



Click Here to Expand Forum to Full Width