I am coding an app that can select another app's ComboBox
with a certain index.
For example, I want to select the second listed item "Adobe flash player" from my app with hooking.
The ComboBox
app is not mine so I cannot modify the target app.
Usually, putting a text or clicking a button can be done by using Sendmessage
API in VB.Net.
The handle value(hWnd) of that ComboBox
can be retrieved.
I want to know which function(api) to use and which value should be used.
Thank you.
You can send CB_SETCURSEL
message to the combo box. The wParam
parameter of SendMessage
should be zero-based index of the item which you want to set as selected index and the lParam
is useless here.
An application sends a
CB_SETCURSEL
message to select a string in the list of a combo box. If necessary, the list scrolls the string into view. The text in the edit control of the combo box changes to reflect the new selection, and any previous selection in the list is removed.
wParam
: Specifies the zero-based index of the string to select. If this parameter is –1, any current selection in the list is removed and the edit control is cleared.lParam
: This parameter is not used.
C# Example
[System.Runtime.InteropServices.DllImport("user32.dll")]
static extern IntPtr SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
const int CB_SETCURSEL = 0x014E;
void SetSelectedIndex(IntPtr comboBoxHandle, int index)
{
SendMessage(comboBoxHandle, CB_SETCURSEL, index, 0);
}
VB.NET Example
<System.Runtime.InteropServices.DllImport("user32.dll")> _
Shared Function SendMessage(ByVal hWnd As IntPtr, ByVal Msg As Integer, _
ByVal wParam As Integer, ByVal lParam As Integer) As IntPtr
End Function
Const CB_SETCURSEL As Integer = &H14E
Sub SetSelectedIndex(comboBoxHandle As IntPtr, index As Integer)
SendMessage(comboBoxHandle, CB_SETCURSEL, index, 0)
End Sub