Click here to Skip to main content
11,580,580 members (71,872 online)
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 2: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)

  Print Answers RSS
0 OriginalGriff 1,088
1 Sergey Alexandrovich Kryukov 750
2 Abhinav S 638
3 Suvendu Shekhar Giri 433
4 F-ES Sitecore 420


Advertise | Privacy | Mobile
Web03 | 2.8.150603.1 | Last Updated 6 Nov 2012
Copyright © CodeProject, 1999-2015
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