troubleshooting Question click button in another window

Avatar of HaiFai
HaiFaiFlag for Finland asked on
Programming.NET ProgrammingVisual Basic.NET
4 Comments1 Solution2653 ViewsLast Modified:

I cant get this code to work .. i need to click yes button in another form called "Sample"
could anyone tell me what im doing wrong.

Imports System.Runtime.InteropServices

Public Class Form1
    <DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
    Private Shared Function FindWindow(ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr
    End Function
    <DllImport("user32.dll", CharSet:=CharSet.Auto)> _
    Public Shared Function SendMessage(ByVal hWnd As Integer, ByVal msg As Integer, ByVal wParam As Integer, ByVal lParam As IntPtr) As Integer
    End Function
    <DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
    Private Shared Function FindWindowEx(ByVal parentHandle As IntPtr, ByVal childAfter As IntPtr, ByVal lclassName As String, ByVal windowTitle As String) As IntPtr
    End Function
    Private Const WM_LBUTTONDOWN As Long = &H201
    Private Const WM_LBUTTONUP As Long = &H202
    Private Const BN_CLICKED As Integer = 245
    Sub CommunicateByClickButton(ByVal ApplicationTitle As String)
        Dim hwnd As IntPtr = IntPtr.Zero
        Dim hwndChild As IntPtr = IntPtr.Zero
        hwnd = FindWindow(Nothing, ApplicationTitle)
        If hwnd.Equals(IntPtr.Zero) Then
            MsgBox("Couldn't find the " & ApplicationTitle)
            Exit Sub
        End If
        'Get a handle for the button with caption
        hwndChild = FindWindowEx(hwnd, 0, "Button", "Yes")
        'send BN_CLICKED message 
        If Not hwndChild.Equals(IntPtr.Zero) Then
            SendMessage(CType(hwndChild, Integer), BN_CLICKED, 0, IntPtr.Zero)
        End If
    End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
end sub
End Class

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 4 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 4 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros