Cannot trigger the click event of a list box remotely

Asked By 260 points N/A Posted on -
qa-featured

I have a .Net application that I am developing using VB.Net. I am attempting to control a remote application. I found a similar post in this forum upon reading the values of a list box of a remote program. I am using that as reference and have made progress. Presently, I am stuck in attempting to trigger the click event of the list box. I am also new to this Windows Messaging API system. How can I trigger the click event on an item for a remote List box?

Please help.
SHARE
Best Answer by Edvard
Answered By 0 points N/A #101382

Cannot trigger the click event of a list box remotely

qa-featured

Using the Windows Application Programming Interface you can achieve greater sophistication in interaction with other programs. It allows you to send messages to the remote program from your program, simulating a human user input, either the keyboard, mouse or other human interface devices.

In order to fire the click event of the List box, you need to select them. You can use the Send Message function to notify the List Box that these items need to be selected. Assuming you have multiple selection enabled, you can use the SETSEL message to select the required list items.
 
LB_SETSEL = &H185
 
SendMessage(targetControl, LB_SETSEL, 1, Integer.Parse(i))
 
The targetControl specifies the handle to the window of the List box. The LB_SETSEL is defined as:
 
The next parameter, 1 , informs the List Box to "select". The last parameter is the List Index of the required item. Using this method you can select the desired items on the List Box.
Answered By 260 points N/A #101383

Cannot trigger the click event of a list box remotely

qa-featured

Using the Send Message with LB_SETSEL message I was able to select multiple items on the List box; thank you Ingrid.

But the problem still remains – I cannot seem to trigger the click event on the List box on selecting a List box Item. The remote program populates a second List box, whenever you select an item from the first List box. The selection works only if I use the keyboard or the mouse. When I use the Send Message API to select the items, I can see that the selection happens, but the second List box does not get populated.
 
Please help!
Answered By 0 points N/A #101384

Cannot trigger the click event of a list box remotely

qa-featured

You can use the VB.Net "SendKeys" function instead of using the Windows API natively. The SendKeys function is actually a wrapper for the Send Message function. You can pass a predefined set of strings as commands to this function and it will do the needful. For example, if you want to simulate the enter key, you pass "{ENTER}" as the command; it is much easier to deal with than the Windows Messaging API.

SendKeys.SendWait("{ENTER}")
 
SendKeys.SendWait(" ") 'to send a space
 
In order for the SendKeys function to work, you need to have the required application in the foreground. This is because there is no provision to send the target application or title as a parameter to the SendKeys class.
Answered By 0 points N/A #101385

Cannot trigger the click event of a list box remotely

qa-featured

You can try sending a mouse click event to the destination list box using the Windows Application Programming Interface. The mouse click event consists of a mouse down event and a mouse up event. You need to execute these together in order to complete the mouse click event. Try sending the click event to the list box. Sample code for it is attached:

Public Const MOUSEEVENTF_LEFTDOWN = &H2
Public Const MOUSEEVENTF_LEFTUP = &H4
Public Const MOUSEEVENTF_RIGHTDOWN = &H8
Public Const MOUSEEVENTF_RIGHTUP = &H10
 
Declare Sub mouse_event Lib "user32" (ByVal dwFlags As Integer, ByVal dx As Integer, ByVal dy As Integer, ByVal cButtons As Integer, ByVal dwExtraInfo As Integer).
 
The above code uses the mouse_event API call. You will need to know the X,Y coordinate location of the destination control in order to use this message. To complete a left click the following need to be executed.
 
mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0)
 
mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)
 
Try sending the click events by getting the coordinates of the destination list box and passing it to the dx and dy parameters.
Answered By 260 points N/A #101386

Cannot trigger the click event of a list box remotely

qa-featured

I tried the SendKeys function. It sometimes works and sometimes does not. I think there is a timing issue and the commands get lost. I tried the Windows API for mouse click as outline; it works only if the target application is stationary and in full screen. I am able to get the exact co-ordinates and send a mouse click provided the window is stationary and opens in the same location. The problem is the destination window opens up at a random location dependent on the screen size of the user desktop.

Is it possible to send the mouse events via targeting the window handle?
Best Answer
Best Answer
Answered By 0 points N/A #101387

Cannot trigger the click event of a list box remotely

qa-featured

Well, you can send a mouse click via the Windows Application Programming Interface. You must be careful and you need to ensure that the mouse down and the mouse up events must be sent to the target application. If you miss to send the mouse up event, the target application will perceive that the mouse button is forever pressed!

Const WM_RBUTTONDOWN = &H204
Const WM_RBUTTONUP = &H205
 
Private Declare Function SendMessage Lib "USER32.DLL" Alias "SendMessageA" (ByVal hwnd As IntPtr, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal lParam As IntPtr) As Integer 'sending a right button click updates the selected indexes.
 
SendMessage(targetControl, WM_RBUTTONDOWN, 0, 0)
SendMessage(targetControl, WM_RBUTTONUP, 0, 0)
 
The above code allows you to send a mouse event direct to the windows handle. A right click is recommended as the left click will de-select the options in the list box.
Answered By 260 points N/A #101388

Cannot trigger the click event of a list box remotely

qa-featured

Sending the mouse event via the Windows Application Programming Interface worked! Thank you Edvard for the advise! Now I am able to continue with my application! Thank you experts for the valuable advice!

Login/Register to Answer

Related Questions