VB.net Bring Window to Front
What code in VB.net 2010 do I need to set a window to come to the front of the screen.
What I am trying to achieve is to display an urgent alert type, its a form, for certain reasons I am not using message box.
Someone suggested the following code, but this does not work:
Private Sub frmMessage_Load(ByVal sender As Object, ByVal e As System开发者_C百科.EventArgs) Handles Me.Load
Me.BringToFront()
End Sub
It should be enough that you set property TopMost
of the window that you need to get on the top of the others.
Form.TopMost = True
try
me.Activate()
This outta do the trick
EDIT: I googled to find backup for my answer
My Case
EDIT2:
There seems to be a few things that work. the Above as well as
''depending on setup
Me.Show
Form2.Show()
also
Form2.ShowDialog()
also
Form2.Visible = True
<DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
Private Shared Function FindWindow( _
ByVal lpClassName As String, _
ByVal lpWindowName As String) As IntPtr
End Function
<DllImport("user32.dll")> _
Private Shared Function SetForegroundWindow(ByVal hWnd As IntPtr) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function
Now, take the name of the window you want to bring to the front, and do the following:
string name = "Untitled - Notepad";
IntPtr ptr = FindWindow(null, name);
SetForegroundWindow(ptr);
This will bring the window to the front of the screen.
Try using the .Shown event. Here is the code for a three form test. At the end of the button click event, Form3 should be on top of Form2, on top of Form1.
Public Class Form1
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Me.SendToBack()
Dim f2 As New Form2
f2.Show()
Dim f3 As New Form3
f3.Show()
End Sub
End Class
Public Class Form2
Private Sub Form2_Shown(sender As Object, e As System.EventArgs) Handles Me.Shown
Me.BringToFront()
End Sub
End Class
Public Class Form3
Private Sub Form3_Shown(sender As Object, e As System.EventArgs) Handles Me.Shown
Me.BringToFront()
End Sub
End Class
If what you want is bring to from, after the winfom has lost focus or it has been minimized. In my case, work when I open a winform at a button.
frmProducts.Show()
'Retorre the original State
frmProducts.BringToFront()
frmProducts.WindowState = FormWindowState.Normal
Bring window to front from where?
MDI
In MDI with multiple forms, form.BringToFront()
will be enough, this will move the form on top within your application.
You can also use form.ShowDialog()
method when presenting the warning/error.
Desktop
On your desktop you may have multiple application, you better go setting the Application as a TopMost.
If your application is behind another windows, the warning message may not be visible.
To bring the application
to the front you need some more extra work, this are 'extensions' to the 'form' class, so the use will be: form.MakeTopMost()
:
<Runtime.InteropServices.DllImport("user32.dll", SetLastError:=True)> _
Private Function SetWindowPos(ByVal hWnd As IntPtr, ByVal hWndInsertAfter As IntPtr, ByVal X As Integer, ByVal Y As Integer, ByVal cx As Integer, ByVal cy As Integer, ByVal uFlags As Integer) As Boolean
End Function
Private ReadOnly HWND_TOPMOST As New IntPtr(-1)
Private ReadOnly HWND_NOTOPMOST As New IntPtr(-2)
<System.Runtime.CompilerServices.Extension()> _
Public Sub MakeTopMost(frm As Form)
SetWindowPos(frm.Handle(), HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE)
End Sub
<System.Runtime.CompilerServices.Extension()> _
Public Sub MakeNormal(frm As Form)
SetWindowPos(frm.Handle(), HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE)
End Sub
as always, the extension code need to be in a separate module.
see folowing example when form load
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.TopMost = True
End Sub
My requirement was to pop up a possibly minimized application and have it maximized and active. after scouring the net a bit, i found part of the answer in a c++ forum.
WindowState = FormWindowState.Maximized
Me.Activate()
This took my application in any state (maxxed, mini'd, big, small, behind stuff). it brought it to the front and maximized it on the screen. i.e. popped it up on the screen so i could see it!
When nothing works try right click and select Bring to Front. If other images are covering the one that must be in the front, just right click on each one and select Send to Back as needed.
I use:
dim tempResult as dialogResult = frmName.showDialog()
and in the called form:
me.dialogResult = dialogResult.{OK, Abort, whatever}
The calling form code waits for the called forms result before continuing execution.
Just set the Owner
property of the form you want to appear on top:
Dim frmMessage As New Form()
frmMessage.Owner = frmMain 'frmMain is the form creating the message window
frmMessage.Show()
Now frmMessage
will always be on top of frmMain
, regardless of focus.
As another user posted, one of my favorite ways is to set the forms owner. By doing this, the child form will always sit on top of the parent form when either form is focused, activated, etc... What's nice about this is that you don't have to catch any special events and execute any special code. Suppose you have a main form frmMain and a popup form frmPopup you could use the following code to ensure the popup is always on top of the main form without using topmost (which works but can have some bad side affects).
frmPopup.show(frmMain)
or you could use the longer version (as posted above by someone
frmPopup.Owner = frmMain
frmPopup.show()
Another thing that is great about this is you can also use it with ShowDialog()
frmPopup.ShowDialog(frmMain)
I know this is an old post but perhaps people still looking for easy solutions to this will find this. It has really helped improve the functionality of my programs with a lot less code than I was using before.
Draw a visible top most form off of the screen and then make this form the owner of the ShowDialog() call.
I know this is a little old but I had a similar problem today this is how I solved it. It works as long as you don't mind closing the open form and creating a new one.
Dim MyRemoteForm As New Form
MyRemoteForm = Application.OpenForms("frmManualRemote")
If MyRemoteForm Is Nothing Then
frmManualRemote.Show()
Else
frmManualRemote.Close()
frmManualRemote.Show()
End If
A bit off the OP... I have a "dashboard" that I open from a menu. The user can switch to other windows and then "load" the dashboard again. If it is already loaded it is brought to the front.
Declare frmFISDash = frmFISDashboard "global"
If frmFISDash Is Nothing Then
frmFISDash = New frmFISDashboard
frmFISDash.Show()
Else
frmFISDash.WindowState = FormWindowState.Normal
frmFISDash.BringToFront()
End If
Note the setting of .WindowsState - if the form is minimized .bringToFront does not work.
I solved it this way (my be of use to somebody) - this way it brings the hidden form to front even in debug mode:
Private Sub Form1_SizeChanged(sender As Object, e As EventArgs) Handles MyBase.SizeChanged
If Me.WindowState = FormWindowState.Minimized Then
HideForm()
Else
BringFormToFront()
End If
End Sub
Private Sub NotifyIcon_Click(sender As Object, e As EventArgs) Handles NotifyIcon.Click
'Determine which mouse button was pressed, in order to differentiate between Left/Right Mouse Button
Dim MouseButton As System.Windows.Forms.MouseEventArgs = CType(e, MouseEventArgs)
If MouseButton.Button = MouseButtons.Left Then
BringFormToFront()
End If
End Sub
Private Sub NotifyIcon_DoubleClick(sender As Object, e As EventArgs) Handles NotifyIcon.DoubleClick
BringFormToFront()
End Sub
Private Sub HideForm()
Me.NotifyIcon.Visible = True
Me.ShowInTaskbar = False
'Windowstate controlled by user when minimizing Form
Msgbox("Minimized, click on Notify Icon to show")
End Sub
Private Sub BringFormToFront()
Me.NotifyIcon.Visible = False
Me.ShowInTaskbar = True
Me.WindowState = FormWindowState.Normal
End Sub
To take what Jim Nolan said to do based on his description. This is what the best way of handling to make sure the form is properly at the front of all other forms as well as addressing disposing the form, assigning ownership of the new form, and showing the form
Dim form As Form = new Form
form.TopMost = True
form.Owner = Me
form.ShowDialog()
form.Dispose()
The following seemed easiest to me to bring up a form called "Projects" after clicking on a menu choice. Form will be loaded if necessary, UN-minimized if necessary, and brought to front ('focus').
Private Sub ProjectsToolStripMenuItem1_Click(sender As Object, e As EventArgs) Handles ProjectsToolStripMenuItem1.Click
Me.Cursor = Cursors.WaitCursor ' if form is slow to load for any reason
Projects.Show()
Me.Cursor = Cursors.Default
Projects.WindowState = FormWindowState.Normal
Projects.Focus()
End Sub
Try this:
Me.ShowDialog()
should help.
a little trick:
me.hide()
me.visible = true
精彩评论