Set focus on winform after launch

I found several topics about this, but none of them helped me with my problem, I want to adjust the focus on the newly created winform window after it started.

I run the form in my new thread with:

application.Run(new InvisibleForm()); 

and a form will appear, but the focus is still set in the last selected window from the windows. this form has no title and is not in the taskbar to view, it also has a set of TransparencyKey:

 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 several methods, but none of them made me focus on the form or could set the form in the foreground / top of all other windows:

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

Is there a way to programmatically trigger the click / focus event on the form so that it sets itself with this event in focus?

+7
source share
2 answers

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

+7
source

you can try this code

 this.ShowDialog(); 

The code is higher than MessageBox, with attention. Thus, the last form should close and access another form again.

+1
source

All Articles