setfocus

leontager

Well-known member
Joined
Jun 17, 2003
Messages
89
how do I set the focus on a window from .net

Code:
    Private Sub Button1_Click(ByVal sender As System. _
Object, ByVal e As System.EventArgs) Handles Button1.Click          Dim wWindow As Long          
wWindow = FindWindow(vbNullString, "Untitled - Notepad") 
SetFocus(wWindow)   
End Sub

shoudnt this work?
 
Did you check to make sure it actually found that Window? Before you SetFocus(), look at the value of wWindow. Remember, if youre searching on window caption, the text has to be exactly the same. I would recommend that you leave the second parameter (the window caption) as Nothing and specify the first parameter (the window class) as Notepad, since that is the Notepad windows class.
 
well actually I was just testing the code. What class would I use for internet explorer? also, I did check the value of wWindow and I got some long number. I think it started with a 4.
 
You might try using the [api]SetForegroundWindow[/api] API instead, or maybe the [api]SetActiveWindow[/api] API to focus it, instead of the SetFocus API..

Use "IEFrame" for Internet Explorers class.
 
you could always do it without FindWindow , like this...
Code:
    Private Declare Function BringWindowToTop Lib "user32.dll" (ByVal hwnd As IntPtr) As Integer

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim proc() As Process = Process.GetProcessesByName("Notepad")
        Dim x As Integer
        For x = LBound(proc) To UBound(proc)
            If proc(x).MainWindowTitle = "Untitled - Notepad" Then
                BringWindowToTop(proc(x).MainWindowHandle)
            End If
        Next
    End Sub
 
Back
Top