8

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 ?

dontcare
  • 935
  • 3
  • 16
  • 34
  • Have you tried to give it the focus in the Load-Event? – Tomtom Apr 19 '12 at 08:47
  • 1
    And where/when do you set all those properties? Try `this.Focus()` in the Shown event. – H H Apr 19 '12 at 08:48
  • 5
    Aside: running a Form on its own thread is possible but rarely a good idea. – H H Apr 19 '12 at 08:49
  • Are you clicking on a part of the form that has 0% opacity? If you are your input will fall through to whatever is underneath. – David Anderson Apr 19 '12 at 08:50
  • i'm setting this in the InitializeComponent() method, i know that the clicks are going through the window ( thats what i want ), thats is why i want to set the focus programmly and not with user clicks – dontcare Apr 19 '12 at 09:29
  • is there a way to simulate a mouse click on this frame programmly ? – dontcare Apr 19 '12 at 14:05
  • 1
    Funny, it works for me. What are you doing in this.InvisibleForm_Load? – Philip Wallace Apr 19 '12 at 14:43
  • because i'm from the java world and this is the first time for my doing something in C#, where are you using the commands ? how do i create a eventlistener for the "show" state ? found only "load" for this. btw: i dont create the form anymore in a own thread – dontcare Apr 19 '12 at 15:00
  • 1
    http://msdn.microsoft.com/en-us/library/system.windows.forms.form.shown.aspx – Philip Wallace Apr 19 '12 at 15:09
  • when exactly are you calling the Activate() method? – cre8or Apr 19 '12 at 17:37
  • 1
    i only saw this.show and didnt notice that we also have this.shown ... so yet its working ;) thanks dood – dontcare Apr 20 '12 at 07:09

2 Answers2

8

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

bluish
  • 26,356
  • 27
  • 122
  • 180
dontcare
  • 935
  • 3
  • 16
  • 34
1

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.