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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

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
SameerJagdaleCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.