Set focus on winform after start

dontcare picture dontcare · Apr 19, 2012 · Viewed 10.4k times · Source

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 ?

Answer

dontcare picture dontcare · Apr 22, 2012

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