Windows Develop Bookmark and Share   
 index > Windows Forms General > application losing focus when modal form is hidden.
 

application losing focus when modal form is hidden.

Form1 is opened as a modal window with a button in it. If i click on the button form1 is hidden and and form2 is opened as modal window. But when the form2 opens, my application looses focus and the form2 appears above the window that I visited last.(eg. my application is running. I open a word document. Then when i click on button in Form1, form2 appears above the word document instead of my own application).

nitinveer  21 hours 51 minutes ago
Well your application and Word are seperate applications, which ever you click will get focus, what is so strange in this behaviour for you?
VB.NET to C# http://www.developerfusion.com/tools/convert/vb-to-csharp/
Se3ker385  19 hours 58 minutes ago
Could you post the relevant code? I'm interested in how each of the two Forms are being shown/hidden, etc.
~~Bonnie Berent [C# MVP]

(new blog --- not many posts yet --- be patient)
geek-goddess-bonnie.blogspot.com
BonnieB  9 hours 49 minutes ago

Once i come back to my application and then click on the button in form1(which obviously means that my application is in focus now), my application looses focus and goes back to the application that i last visited.

nitinveer  6 hours 13 minutes ago
Both forms are displayed using showdialog:

 

public DialogResult DisplayDialog()

{

return this.ShowDialog(this.Parent);

}

On click of button in form1, form1 is hidden and then form2 is displayed. On button Click this is done:

 

 

 

 this.Hide();
Form2 SampleChild= new Form2();
SampleChild.DisplayDialog();

 

nitinveer  6 hours 0 minutes ago

Forgot to mention that my application has got a main window. Form1 is opened from the main window. So when form2 is opened, it is supposed come over this main window. But my problem is that form2 opens over the window that i last visited.

nitinveer  5 hours 35 minutes ago
I remember this question coming up before ... you're right, that behavior happens when you hide the first modal form. I know what you're talking about. Now, if i could only remember *where* I saw this question before and what the answer to it was to solve the problem. Heck, I may even have been the one to answer it ... but I can't recall it right now ... and I can't recall if it was in this forum or another one that I frequent.

I'll do a search for it in the morning and get back to you.
~~Bonnie Berent [C# MVP]

(new blog --- not many posts yet --- be patient)
geek-goddess-bonnie.blogspot.com
BonnieB  5 hours 31 minutes ago

Ok, the main form indeed looses focus, but why would you want to hide a modal dialog?

Common practice is to gather input from Form1 close the dialog and then open Form2 and gather its input, and then close it to return to modal-less MainForm.

Edit: anyway in your form1 button click event just switch owner to your main form


 this.Hide();
Form2 SampleChild= new Form2
();
SampleChild.DisplayDialog(MainForm);




VB.NET to C# http://www.developerfusion.com/tools/convert/vb-to-csharp/
Se3ker385  2 hours 58 minutes ago

You can use google to search for other answers

Custom Search

More Threads

• Image Text wrapping in RichTextBox control???
• is it possible to restrict qty of app instances that client can open
• Screen Updating Issues
• Programmatically creating files for use by separate application
• Looks OK in Windows XP - looks a mess in Vista!
• Using Exception Management Application Block with CAD program add-in...
• change a cell value in ListView
• Delete Key does not respond smart client app
• Image Box problem
• Question about cloning menus