Bringing a child form to the front

  • Thread starter Richard L Rosenheim
  • Start date
R

Richard L Rosenheim

I have the application's form (MainForm) along with a 2nd form (Form2). If
the user minimizes MainForm, I also minimize Form2. And when the user
restores the application, I also restore Form2. But, Form2 is ending up
behind MainForm. Anyone have an idea of how can I set/keep Form2 to be in
front of the MainForm?

And before someone suggests it, no -- I don't consider TopMost to be the
solution. That will make the form the topmost window for everything. I'm
trying to make Form2 the top most form only in relationship to MainForm.
Nor do I want to have a MDI application.

Here's the basic code I'm working with:

Private Sub MainForm_Resize(ByVal sender as Object, ByVal e as
System.EventArgs) Handles MyBase.Resize
If Me.WindowState = FormWindowState.Minimized Then
If Not (Form2 Is Nothing) Then
Form2.Visible = False
End If
Else
If Not (Form2 Is Nothing) Then
Form2.Visible = True
' code to bring Form2 to front goes here
End If
End If
End Sub

I did find the thread "Bring To Front of a Process/Other application"
http://groups.google.com/groups?hl=...ups?q=%22Bring+to+front+of+a+process%22&hl=en
but it's not working for me.

Based upon the above thread, I inserted the following code in the above code
fragment:

Dim handle As IntPtr = Form2.Handle
If Not IntPtr.Zero.Equals(handle) Then
Win32Helper.ShowWindow(handle, 1)
Win32Helper.SetForegroundwindow(handle)
End If

All I'm getting is Form2's windows briefly flashing in front of the main
form. BTW, I'm using Visual Studio 2004 on a Windows 2000 Pro machine.

Anyone have any suggestions?

Thanks in advance,

Richard Rosenheim
 
K

Ken Tucker [MVP]

Hi,

Form2.BringToFront
http://msdn.microsoft.com/library/d...windowsformscontrolclassbringtofronttopic.asp

Ken
-------------------
I have the application's form (MainForm) along with a 2nd form (Form2). If
the user minimizes MainForm, I also minimize Form2. And when the user
restores the application, I also restore Form2. But, Form2 is ending up
behind MainForm. Anyone have an idea of how can I set/keep Form2 to be in
front of the MainForm?

And before someone suggests it, no -- I don't consider TopMost to be the
solution. That will make the form the topmost window for everything. I'm
trying to make Form2 the top most form only in relationship to MainForm.
Nor do I want to have a MDI application.

Here's the basic code I'm working with:

Private Sub MainForm_Resize(ByVal sender as Object, ByVal e as
System.EventArgs) Handles MyBase.Resize
If Me.WindowState = FormWindowState.Minimized Then
If Not (Form2 Is Nothing) Then
Form2.Visible = False
End If
Else
If Not (Form2 Is Nothing) Then
Form2.Visible = True
' code to bring Form2 to front goes here
End If
End If
End Sub

I did find the thread "Bring To Front of a Process/Other application"
http://groups.google.com/groups?hl=...ups?q=%22Bring+to+front+of+a+process%22&hl=en
but it's not working for me.

Based upon the above thread, I inserted the following code in the above code
fragment:

Dim handle As IntPtr = Form2.Handle
If Not IntPtr.Zero.Equals(handle) Then
Win32Helper.ShowWindow(handle, 1)
Win32Helper.SetForegroundwindow(handle)
End If

All I'm getting is Form2's windows briefly flashing in front of the main
form. BTW, I'm using Visual Studio 2004 on a Windows 2000 Pro machine.

Anyone have any suggestions?

Thanks in advance,

Richard Rosenheim
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top