i am trying to make a program that in the even that a message is received when the program does not have focus a message box will be displayed, however, using:

if me.focused = false then
TrayIcon.BalloonTipTitle = "New Message From " & array(0)
TrayIcon.BalloonTipText = array(1)
TrayIcon.ShowBalloonTip(5000)
end if

always return the message even if the form is not focused. i also tried this under message received:

messagebox.show(me.focused)

and it always receives the value false

can someone please tell me what i am doing wrong. and is there another command that i am supposed to be using?
Thank
~Matt

Dani AI

Generated

As observed, the form-level Focused check returned false and produced the wrong behavior. Focused only reports whether that specific control currently has the input cursor and is not a reliable indicator of whether the application window is the active/foreground window. Also, 's BalloonTipShown event fires after a balloon is shown and does not control whether it should be shown based on window activation.

A simple, robust pattern is to track activation with the form's Activated and Deactivate events and consult that flag when a message arrives (marshal to the UI thread first). Example:

Private isActive As Boolean = False

Private Sub Form1_Activated(sender As Object, e As EventArgs) Handles Me.Activated
    isActive = True
End Sub

Private Sub Form1_Deactivate(sender As Object, e As EventArgs) Handles Me.Deactivate
    isActive = False
End Sub

' On message arrival (ensure this runs on the UI thread):
If Not isActive Then
    ShowTrayNotification()
Else
    ShowInWindowNotification()
End If

For a system-level check, use Win32 to see which window is foreground (or which process owns it). This works well when multiple top-level windows exist:

Imports System.Runtime.InteropServices

<DllImport("user32.dll")>
Private Shared Function GetForegroundWindow() As IntPtr
End Function

<DllImport("user32.dll")>
Private Shared Function GetWindowThreadProcessId(hWnd As IntPtr, ByRef lpdwProcessId As Integer) As Integer
End Function

Private Function IsAppForeground() As Boolean
    Dim pid As Integer = 0
    Dim h As IntPtr = GetForegroundWindow()
    GetWindowThreadProcessId(h, pid)
    Return pid = Process.GetCurrentProcess().Id
End Function

Notes: marshal message handling to the UI thread before checking activation; avoid using modal MessageBox calls for debugging focus (they steal focus and skew results); and test balloon/notification behavior on the target OS since notification handling can vary.

Recommended Answers

All 2 Replies

You have to handle BallonTipShown event.

? i have no problem with the balloon text. i just want that to only show when the windows does not have focus.

windows has focus - string appears in window
windows has no focus - string appears in tray pop up

the focusing function is what is not working for me.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.