您可以發送CB_SETCURSEL
消息到組合框。的SendMessage
的wParam
參數應該是從零開始,你要設置爲選定的索引項的指數和lParam
是沒用在這裏。
應用程序發送一消息CB_SETCURSEL
選擇在組合框中的 列表的字符串。如有必要,該列表將字符串滾動到 視圖。組合框的編輯控件中的文本更改爲反映 新選擇,並且刪除列表中的任何先前選擇。
wParam
:指定要選擇的字符串從零開始的索引。如果此 參數爲-1,則刪除列表中的任何當前選擇,並清除編輯控件 。
lParam
:此參數未使用。
C#示例
[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例
<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
正是我需要的。謝謝。另外,是否有任何方法來獲取ComboBox控件的Count? – klados
不客氣:) –
'CB_GETCOUNT'用於獲取物品數量。隨時通知我,如果你問了一個關於它的問題,我會爲你發佈一個例子。 –