Set focus on winform after start

10,630

Solution 1

Its important to use the Activate() method in the "shown" state of the form, so create a listener for the shown event and use your focus / front methods there

Solution 2

you can trying this code

this.ShowDialog();

the code above same as MessageBox with attention. So the last form appear must close and can access the other form again.

Share:
10,630
dontcare
Author by

dontcare

Updated on June 11, 2022

Comments

  • dontcare
    dontcare almost 2 years

    I found a few topics about this but none of these could help me with my problem, i want to set the focus on a new created winform window after it started.

    I'm starting the form in a own new thread with:

    application.Run(new InvisibleForm());
    

    and the form appears, but the focus is still set on the last selected window from windows. this form has no titlebar and is not in the taskpanel to see, it also has a TransparencyKey set:

    this.AutoScaleDimensions = new SizeF(6F, 13F);
    this.AutoScaleMode = AutoScaleMode.Font;
    this.BackColor = SystemColors.AppWorkspace;
    this.ClientSize = new Size(992, 992);
    this.ControlBox = false;
    this.MaximizeBox = false;
    this.MinimizeBox = false;
    this.Name = "InvisibleForm";
    this.Opacity = 0.5;
    this.ShowInTaskbar = false;
    this.TransparencyKey = SystemColors.AppWorkspace;
    this.Load += new EventHandler(this.InvisibleForm_Load);
    

    now i tried a few methods but none of these got me focus on the form or could set the form on the foreground / of top of all other windows:

    this.TopMost = true;
    this.Focus();
    this.BringToFront();
    this.Activate();
    

    is there a way to fire programmly a click / focus event to the form so that it sets himself with this event on focus ?