Visual Basic Development Bookmark and Share   
 Home > Visual Basic Language > How to find handle of a textbox on a form using handles.
 

How to find handle of a textbox on a form using handles.

I am coding in VB.NET to control an external application.

I used the following code to launch a Form2 from a theoretical Form1 using the shortcut ALT-F1 on Form 1.


Private Sub Button2_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
'Get handle by window title. You can specify the class name too.
Dim hwnd As Int32 = apiFindWindowEx(HWND_DESKTOP, 0, Nothing, "Form1")
SetForegroundWindow(hwnd)
SendKeys.SendWait("%{F1}")

End Sub

Now, there is a TextBox on Form2. How do I find and place the cursor on that TextBox so I can send some text to that textbox.

I know the classname of the TextBox. How can I accomplish this?



  • Edited bytrekkie25 Sunday, June 22, 2008 12:09 AMClarification
  •  
trekkie25  Sunday, June 22, 2008 12:08 AM
Hi Trekkie25,

First use FindWindow to find the windows' handle. Then use FindWindowEx to find its child control's handle.

ImportsSystem.Runtime.InteropServices
ImportsMicrosoft.Win32
PublicClassForm1
PrivateConstWM_LBUTTONDOWN=&H201
PrivateConstWM_LBUTTONDBLCLK=&H203
PrivateConstWM_LBUTTONUP=&H202
<DllImport("user32.dll",CharSet:=CharSet.Auto,SetLastError:=False)>_
PrivateSharedFunctionSendMessage(ByValhWndAsIntPtr,ByValMsgAsUInteger,ByValwParamAsIntPtr,_
ByVallParamAsIntPtr)AsIntPtr
EndFunction
<System.Runtime.InteropServices.DllImport("USER32.DLL",CharSet:=System.Runtime.InteropServices.CharSet.Auto)>_
PrivateSharedFunctionFindWindow(ByVallpClassNameAsString,ByVallpWindowNameAsString)AsIntPtr
EndFunction
<DllImport("user32.dll",SetLastError:=True,CharSet:=CharSet.Auto)>_
PrivateSharedFunctionFindWindowEx(ByValparentHandleAsIntPtr,ByValchildAfterAsIntPtr,_
ByVallclassNameAsString,ByValwindowTitleAsString)AsIntPtr
EndFunction
PrivateSubButton1_Click(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)HandlesButton1.Click
DimFHandleAsIntPtr
FHandle=FindWindow(Nothing,"Form1")
DimchildhandleAsIntPtr=FindWindowEx(FHandle,IntPtr.Zero,Nothing,"Button1")
IfFHandle=IntPtr.ZeroThen
MessageBox.Show("notepadisnotrunning.")
Else
MessageBox.Show(childhandle.ToString)
SendMessage(childhandle,WM_LBUTTONDBLCLK,IntPtr.Zero,IntPtr.Zero)
SendMessage(childhandle,WM_LBUTTONUP,IntPtr.Zero,IntPtr.Zero)
EndIf
EndSub
PrivateSubForm1_Load(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)HandlesMyBase.Load
Me.Text="FormX"
EndSub
EndClass


Best regards,
Riquel

Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Riquel_Dong  Wednesday, June 25, 2008 2:08 PM
Hi Trekkie25,

First use FindWindow to find the windows' handle. Then use FindWindowEx to find its child control's handle.

ImportsSystem.Runtime.InteropServices
ImportsMicrosoft.Win32
PublicClassForm1
PrivateConstWM_LBUTTONDOWN=&H201
PrivateConstWM_LBUTTONDBLCLK=&H203
PrivateConstWM_LBUTTONUP=&H202
<DllImport("user32.dll",CharSet:=CharSet.Auto,SetLastError:=False)>_
PrivateSharedFunctionSendMessage(ByValhWndAsIntPtr,ByValMsgAsUInteger,ByValwParamAsIntPtr,_
ByVallParamAsIntPtr)AsIntPtr
EndFunction
<System.Runtime.InteropServices.DllImport("USER32.DLL",CharSet:=System.Runtime.InteropServices.CharSet.Auto)>_
PrivateSharedFunctionFindWindow(ByVallpClassNameAsString,ByVallpWindowNameAsString)AsIntPtr
EndFunction
<DllImport("user32.dll",SetLastError:=True,CharSet:=CharSet.Auto)>_
PrivateSharedFunctionFindWindowEx(ByValparentHandleAsIntPtr,ByValchildAfterAsIntPtr,_
ByVallclassNameAsString,ByValwindowTitleAsString)AsIntPtr
EndFunction
PrivateSubButton1_Click(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)HandlesButton1.Click
DimFHandleAsIntPtr
FHandle=FindWindow(Nothing,"Form1")
DimchildhandleAsIntPtr=FindWindowEx(FHandle,IntPtr.Zero,Nothing,"Button1")
IfFHandle=IntPtr.ZeroThen
MessageBox.Show("notepadisnotrunning.")
Else
MessageBox.Show(childhandle.ToString)
SendMessage(childhandle,WM_LBUTTONDBLCLK,IntPtr.Zero,IntPtr.Zero)
SendMessage(childhandle,WM_LBUTTONUP,IntPtr.Zero,IntPtr.Zero)
EndIf
EndSub
PrivateSubForm1_Load(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)HandlesMyBase.Load
Me.Text="FormX"
EndSub
EndClass


Best regards,
Riquel

Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Riquel_Dong  Wednesday, June 25, 2008 2:08 PM

You can use google to search for other answers

Custom Search

More Threads

• Using Enter key to click a button
• Datagrid with results form sql query
• ComboBox Error: "Cannot bind to the new value member. Parameter name: value"
• Unable to create file...
• Query Windows Services in TreeView structure
• How to process Large Binary Files
• A list with key-value pairs sorted by adding sequence?
• Inserting text into a textbox
• How To Create Dockable Forms Like Visual Studio.Net 2005 has
• Opening txt files in Unicode?????