Perform action after form is shown.

Sam I am says Reinstate Monica picture Sam I am says Reinstate Monica · Jul 3, 2012 · Viewed 7.5k times · Source

I am developing a windows mobile application, and I want to do something after a form (a loading screen) is displayed to the user.

normally, I have a Form.Shown event, but with the .net compact framework v3.5, I can't find this event.

Does anyone know of an equivalent to the Shown event or a simple workaround I could use? I don't want to do my own multi-threading if I can help it.

Answer

Mark Hall picture Mark Hall · Jul 3, 2012

The only thing I can think of would be a bit of a hack, when your form is shown, if it has a default control then it will receive the focus. The Focus event will fire during the initial load before the form is shown, but will be fired the second time when it is visible. put a boolean in the Activate event that is set with the first Activates, then test in the default controls Got Focus event.


Other option would be to use a Timer. Set the Interval to something like 10, start it at the end of your Form Load event and then run your startup specific code.

private void Form1_Load(object sender, EventArgs e)
{
    timer1.Start();
}

private void timer1_Tick(object sender, EventArgs e)
{
    timer1.Stop();
    //Do something
}

An example per Hans's Comment:

public partial class Form1 : Form
{
    public delegate void DoWorkDelegate();
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        BeginInvoke(new DoWorkDelegate(DoWorkMethod));
    }

    public void DoWorkMethod()
    {
        //Do something
    }
}