Form losing focus when opening a new form (c#)

I am having a problem where my main form loses focus when opening a new form. I know I can revert the focus back by using mainForm.focus()... but how do I handle things if I want the main form to never give up its focus when new windows are opened?
Pretzel_JesusAsked:
Who is Participating?
 
SameerJagdaleConnect With a Mentor Commented:
you need to handle showwindow API with no activate option..check the code below.
 

private const int SW_SHOWNA = 8;
 
[System.Runtime.InteropServices.DllImport("user32.dll")]
private static extern int ShowWindow(IntPtr handle, int nCmdShow);
----
 private void button1_Click(object sender, EventArgs e)
        {
            //Form4 is a test form..you give your formname
            Form4 frm = new Form4();
            ShowWindow(frm.Handle, SW_SHOWNA);
        }

Open in new window

0
 
aherpsCommented:
You can have the form on top all the time by setting it as a modal form if you want it on top all the time when the program is running.
0
 
aherpsCommented:
There's also a good method here which I suspect is what you're actually looking for as opposed to above:

http://stackoverflow.com/questions/156046/show-a-form-without-stealing-focus-in-c
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
Pretzel_JesusAuthor Commented:
I already have the mainform set to topmost and the second form is not. While this does keep the window on top it does NOT prevent the second form from gaining focus when it first opens.
0
 
Pretzel_JesusAuthor Commented:
Hrm I will try that out in a bit. I was hoping there was just a simple wndproc message I could override or something.
0
 
aherpsCommented:
Yeah, I think you're stuck with PInvoke ShowWindow method as described. Not too onerous.
0
 
Pretzel_JesusAuthor Commented:
Hrm, this doesnt seem to work. When I am browsing through a combobox on the mainform, and the second form opens it still closes the comobobox, I think its because the new form is still gaining focus...
0
 
Pretzel_JesusAuthor Commented:
Ok actually I see what is causing my problems... your code actually did the job, however it creates a seperate problem when used. I have a lot of code checking for which windows are currently open by iterating through the Application.OpenForms and I guess since these windows are not being activated they are not showing up here. How would I use your solution and still be able to solve this?
0
 
Pretzel_JesusAuthor Commented:
I figured out my secondary problem on my own. You solved my originally asked question. Thanks.
0
All Courses

From novice to tech pro — start learning today.