Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / Building Controls / October 2006

Tip: Looking for answers? Try searching our database.

Global Events that all instances of a control handles

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Phil - 11 Sep 2006 08:03 GMT
I have a user control (called MyControl that inherits from
CompositeControl).  This control will raise an event when the user changes
the color of the control. (MyControl.ColorChange).  Inside the code for
MyControl, I also listen and handle the event (ColorChange). - This actually
implements the change in color. This works OK for the control that raised
the event.

However, in the aspx form, I have 5 instances of the same user control
(MyControl).  When one of the controls change color (based on user input) I
would like to have each instance of the MyControl listen for the event and
handle the ChangeColor event.  When I try this only the control that raised
the event will hear the event.  Is there any way to globally raise an event
that all controls could listen too?

Thanks

Phil
RLJ - 01 Oct 2006 07:39 GMT
> I have a user control (called MyControl that inherits from
> CompositeControl).  This control will raise an event when the user
[quoted text clipped - 13 lines]
>
> Phil

In the user control
* Define a Delegate
* Define an event based on the delegate
* Fire the event

In the "listener" controls
* subscribe to the event
* write an event handler
* "wire" the event handler to the delegate

/*************** Event Publisher ****************/
  public PublisherClass {  // the class that changed it's color
      pubic delegate void myDelegate(string newColor);
      public event myDelegate myEvent;

     // "wire" this method to the OnChange property of your Select listbox.
     private void OnColorChange(string theNewColor) {
        this.myEvent();
     }
  }

/*********** meanwhile, in the listener contols' code ***********/
public ListenerClass {
  PublisherClass myPublisher = new PublisherClass();

  override protected void OnInit (EventArgs e) {
     myPublisher.myEvent += myPublisher.myDelegate(ChangeMyColor);
  }

  public void ChangeMyColor (string newColor) {
     // change the color to newColor
  }
}

/************ Caveat **************/
If you want every control to listen to every other control's
ChangeColor event then I'm not sure
how to neatly implement that. I guess every control will be both a
publisher and subscriber - sounds cool, if it works - Either every
control defines it's own event and all controls subscribe to all other
control's events.. OR .. here's something wacky: think it *could* go
like this (WARNING: wild-a.s guess follows):
* Define the delegate and event at the namespace level (i.e. at the
container page level?)
* Each control will have an OnChange handler for what I assume is a
drop-down select list of colors
* In that OnChange handler of every control, call the event
* in OnInit() of every control set "this.myEvent +=
this.myDelegate(ChangeMyColor)" (not too sure that "this" will cut it)
* so the drop-down list event handler fires the event and a separate
method (wired to the delegate) does the actual work
***** would this cause an infinite loop?
Phil - 19 Oct 2006 00:55 GMT
Thanks for the response. I managed a workaround, but your suggestions is
much neater.  Will try to implement it this way!

>> I have a user control (called MyControl that inherits from
>> CompositeControl).  This control will raise an event when the user
[quoted text clipped - 67 lines]
> method (wired to the delegate) does the actual work
> ***** would this cause an infinite loop?

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.