Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# User-controls Events
Hi,
 
I have a user control and i need to edit some things on the event similar to "Shown" with forms.
 
I tried the following but that does not work:
 
Added the following line to the designer:
 
this.Shown += this.HandleControlShown
 
and added this to the class of the control:
 
public event EventHandler Shown;
 
private void HandleControlShown(object sender, EventArgs e)
{
    this.OnShown(EventArgs.Empty);
    //code to execute on shown event goes here.... but does not execute
}
 
protected virtual void OnShown(EventArgs e)
{
    EventHandler handler = this.Shown;
    if (handler != null)
    {
        handler(this, e);
    }
}
 
Where am i doing something wrong?
Posted 6-Nov-12 3:47am
Comments
__John_ at 6-Nov-12 8:58am
   
UserControl does not have a Shown event, but it does have a Load event.
pieterjann at 6-Nov-12 9:04am
   
I know, but I need the Shown event, the Load event is only casted once. Isn't there a way to add the Shown event manually?
__John_ at 6-Nov-12 9:19am
   
You can add your own event but you would need some trigger to fire it.
What do you mean by Shown? i.e. Bring to front? Paint? Or something else?

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Eevent declaration code, may be helpful to you.
//declare a delegate
public delegate ShownEventHandler(string args);
 
public event ShownEventHandler Shown;
private void HandleControlShown(object sender, EventArgs e)
{
 if(this.Shown!=null)
 {
    this.Shown(String.Empty/*your args*/);
    //code to execute on shown event goes here.... but does not execute
 }
}
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Advertise | Privacy | Mobile
Web02 | 2.8.141220.1 | Last Updated 6 Nov 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100