2

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.

Servy
  • 202,030
  • 26
  • 332
  • 449

1 Answers1

3

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
    }
}
Mark Hall
  • 53,938
  • 9
  • 94
  • 111
  • the GotFocus event did not fire a second time. is there something that I have to do to make that happen? – Sam I am says Reinstate Monica Jul 03 '12 at 19:46
  • @ Sam It does on my system, but I am running with .net 2.0 on my desktop. Make sure it is the default as far as tab is equal to 0. Also I have in the past used a one shot timer with a few millisecond delay at the end of my form_load event. – Mark Hall Jul 03 '12 at 19:48
  • I am running .net compact framework v3.5, and how do i "make sure it is the default as far as tab is equal to 0" – Sam I am says Reinstate Monica Jul 03 '12 at 19:51
  • @SamIam It looks like the behavior changed from 2.0 to 3.5, your best bet would be a one-shot timer – Mark Hall Jul 03 '12 at 19:56
  • 1
    Should work, the clean shortcut is to use the form's BeginInvoke() method. – Hans Passant Jul 05 '12 at 00:52
  • Is there also a way to execute code after the form is actually really visible. I mean the form has to be seen be the user, and only then start some code ? I tried the method from Hans here but it don't do this yet. I also put `Update();` in the `DoWorkMethod` – GuidoG Feb 10 '21 at 10:59