Click here to Skip to main content
14,268,557 members
Rate this:
Please Sign up or sign in to vote.
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
Updated 13-Aug-19 5:05am
Comments
__John_ 6-Nov-12 8:58am
   
UserControl does not have a Shown event, but it does have a Load event.
pieterjann 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_ 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?
Rate this:
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
 }
}
   
Rate this:
Please Sign up or sign in to vote.

Solution 2

OK, this is old, but as I often face issues concerning 'stuff' that needs a visible window/window handle, I use a System.Windows.Forms.Timer like this:

(This UserControl hosts a native NamespaceTreeControl that needs a visible handle to initialize)

   Private Sub NamespaceTreeControlLite_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            Dim tmr As New Timer() With {.Interval = 100, .Enabled = True}
            AddHandler tmr.Tick, AddressOf WaitForUCTRLVisible
End Sub


  Private Sub WaitForUCTRLVisible(sender As Object, e As EventArgs)
' Do stuff here that require .Visible=True
            If Me.Visible Then

                Try
                    Marshal.ThrowExceptionForHR(Me.NamespaceTreeBase1.Initialize(Me, Me.ClientRectangle))
                    Me.SetRootPath()
                Catch ex As Exception
                    MsgBox(ex.ToString(), MsgBoxStyle.Critical)
                End Try

                Dim tmr = TryCast(sender, Timer)
                If Not tmr Is Nothing Then
                    tmr.Dispose()
                    tmr = Nothing
                End If
            End If
        End Sub



(It also releaves the PIA of having global variables that are used for this sole purpose...)
   

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




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