Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.EventArgs) Handles Me.Load
        Me.BringToFront()
    End Sub
like image 445
developer__c Avatar asked Jul 18 '11 18:07

developer__c


2 Answers

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
like image 70
ste.xin Avatar answered Sep 20 '22 13:09

ste.xin


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
like image 42
dbasnett Avatar answered Sep 18 '22 13:09

dbasnett