How to give your application the focus.

  • 17 years ago

    I'm puzzled,

    I have a application (with invisible form) that runs at startup.
    Trigered by some event it can display a message box.
    The first time this message box will be behind the active application.
    All next times the messagebox will go in front of the active application.
    I tried lots of things to get the messagebox the first time in front of the application.
    See the code below. Nothing worked. Does anyone have an idea?

    Const SWPSHOWWINDOW = &H40
    Const HWND
    TOPMOST = -1
    Const SW_SHOWNORMAL = 1

    Public Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
    Public Declare Function BringWindowToTop Lib "user32" (ByVal hwnd As Long) As Long
    Public Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long
    Public Declare Function SetFocus Lib "user32" (ByVal hwnd As Long) As Long
    Public Declare Function SetActiveWindow Lib "user32.dll" (ByVal hwnd As Long) As Long
    Public Declare Function SetWindowPos Lib "user32" (ByVal hwnd As Long, ByVal hWndInsertAfter As Long, ByVal X As Long, ByVal Y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long

    Public Sub activateWindow()
    ' Atempt 1      
         frmStartup.Visible = True
    ' Atempt 2      
         ShowWindow frmStartup.hwnd, SWSHOWNORMAL
    ' Atempt 3      
         SetWindowPos frmStartup.hwnd, HWND
    TOPMOST, 9999, 1, 1, 1, SWP_SHOWWINDOW
    ' Atempt 4      
         BringWindowToTop frmStartup.hwnd
    ' Atempt 5      
         SetActiveWindow frmStartup.hwnd
    ' Atempt 6      
         SetForegroundWindow frmStartup.hwnd
    ' Atempt 7      
         SetFocus frmStartup.hwnd

    End Sub

  • 17 years ago

    try to develop u one msg box...
    i mean i u can using the form to replace the msgbox to display the msg
    n set it as alway on top

  • 17 years ago

    I am guessing your problems are because your form is invisible. You might try another approach rather than trying to make your hidden form the active window or set it on top. The MessageBox API function is very straightforward and you can then specify the MB_SYSTEMMODAL flag to make the message box system modal. You can also try using the vbSystemModal constant with the intrinsic VB message box function but I am not sure how well that works with hidden forms either.

  • 17 years ago

    Thanks Jon, that did the trick

  • 17 years ago

    Great! I'm glad it helped....

Post a reply

Enter your message below

Sign in or Join us (it's free).


Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“There are only 3 numbers of interest to a computer scientist: 1, 0 and infinity”