Results 1 to 5 of 5

Thread: [RESOLVED] How do I check if a form has focus or is active?

  1. #1
    Addicted Member lecfox's Avatar
    Join Date
    Dec 11
    Location
    Jamaica
    Posts
    174

    Resolved [RESOLVED] How do I check if a form has focus or is active?

    I just want to blink the taskbar icon if the app has an alert occur while the user is working with another application (I already know how to blink the taskbar icon).

    I have tried Me.focused

    code:
    Code:
     Private Sub Textbox1_TextChanged(sender As Object, e As System.EventArgs) Handles Textbox1.TextChanged
            'Textbox1.SelectionStart = Len(Textbox1.Text)
           ' Textbox1.SelectionLength = 0
    
            If Me.Focused = False Then
                My.Computer.Audio.Play(My.Resources.Alert, AudioPlayMode.WaitToComplete)
                FlashWindow(Me.Handle, 1)
            End If
        End Sub
    HELP please..
    FOX Designs

  2. #2
    New Member
    Join Date
    Aug 12
    Posts
    12

    Re: How do I check if a form has focus or is active?

    Well your code logicaly should work, because in my application I used something similar
    Try this though, it might not response to me.focused=false
    Code:
     Private Sub Textbox1_TextChanged(sender As Object, e As System.EventArgs) Handles Textbox1.TextChanged
            'Textbox1.SelectionStart = Len(Textbox1.Text)
           ' Textbox1.SelectionLength = 0
    
            If Me.Focused = True Then
    'do nothing
    Else
                My.Computer.Audio.Play(My.Resources.Alert, AudioPlayMode.WaitToComplete)
                FlashWindow(Me.Handle, 1)
            End If
        End Sub

  3. #3
    Addicted Member lecfox's Avatar
    Join Date
    Dec 11
    Location
    Jamaica
    Posts
    174

    Re: How do I check if a form has focus or is active?

    i tried it. didn't work though.

    Any other suggestions?
    FOX Designs

  4. #4
    vb Coda .paul.'s Avatar
    Join Date
    May 07
    Location
    Chelmsford UK
    Posts
    16,471

    Re: How do I check if a form has focus or is active?

    try this:

    vb.net Code:
    1. Public Class Form1
    2.  
    3.     Private Declare Function GetActiveWindow Lib "user32" Alias "GetActiveWindow" () As IntPtr
    4.  
    5.     Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
    6.         If GetActiveWindow = Me.Handle Then
    7.             My.Computer.Audio.Play(My.Resources.Alert, AudioPlayMode.WaitToComplete)
    8.             FlashWindow(Me.Handle, 1)
    9.         End If
    10.     End Sub
    11.  
    12. End Class

    edit: that should be <> not =

  5. #5
    Addicted Member lecfox's Avatar
    Join Date
    Dec 11
    Location
    Jamaica
    Posts
    174

    Re: How do I check if a form has focus or is active?

    Since i wanted it to notify me when the txtbox is update while form is inactive i changed the = to <>

    Worked great!.. Thank you .paul.
    FOX Designs

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •