VansFannel
VansFannel

Reputation: 45911

How can I know when a form is shown?

I'm developing a Windows Mobile 5.0 or above with .Net Compact Framework 2.0 SP2 and C#.

I have a WinForm that only has a Custom Control. I want to call a method of that custom control when the form has finished loading and display the control.

Now I using Activated Event, but these event is thrown everytime when I close a messagebox.

Is there an event thrown in that moment?

Thank you.

Upvotes: 1

Views: 6914

Answers (4)

John
John

Reputation: 11

private void Utility_VisibleChanged(object sender, EventArgs e)
{
    if (this.Visible == true) {
     //* Form is being showed

           
    }else{
    //* Form is being hid 
           
    }
}

Upvotes: 1

Giorgos Papadopoulos
Giorgos Papadopoulos

Reputation: 11

This is what I did:

1) Create a dummy, empty pictureBox control on the form

2) Use a boolean variable to tell when the pictureBox's Paint event is called after the form's Load event and call my own OnShow() function

public partial class MyForm: Form
{
    bool form_shown = false;

    private void MyForm_Load(object sender, EventArgs e)
    {
        form_shown = true;
    }

    private void pictureDummy_Paint(object sender, PaintEventArgs e)
    {
        if (form_shown)
        {
            MyOnShow();
            form_shown = false;    
        }
    }
}

Upvotes: 1

jac
jac

Reputation: 9726

I don't think you have a Shown event in the Compact Framework, but you should be able to use the Load event. Set your form's Visible property to true and you should be able to access your custom control after that.

MyForm_Load(object sender, EventArgs e)
{
    ' this procedure runs only once, when the form loads
    ' make the form visible to the user now
    this.Visible = true
    ' the form is now visible

    ' ... more code
}

Upvotes: 2

user227997
user227997

Reputation: 246

Simply use the activate event and have a boolean in your form that is set to true when you have called the method on your custom control. When the form's activated event is triggered again, you just make a check on this boolean.

Upvotes: 6

Related Questions