|
-
Dec 27th, 2012, 10:28 PM
#14
Re: How to set Combo box Item index
 Originally Posted by Edgemeal
I don't think insert would work if the combo is set to be sorted.
I'd just use a cuebanner instead, see remarks on using EM_SETCUEBANNER message.
example,..
Code:
Imports System.Runtime.InteropServices
Public Class Form1
#Region "SETCUEBANNER"
Private Const EM_SETCUEBANNER As Integer = &H1501
<DllImport("user32.dll", EntryPoint:="SendMessageW")>
Private Shared Function SendMessageW(hWnd As IntPtr, Msg As UInt32, wParam As Boolean, <MarshalAs(UnmanagedType.LPWStr)> lParam As String) As IntPtr
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 Sub SetCueBanner(ByVal control As Control, ByVal text As String)
If TypeOf control Is ComboBox Then
' get handle to the "edit" control in combobox control
Dim edit_hWnd As IntPtr = FindWindowEx(control.Handle, IntPtr.Zero, "Edit", Nothing)
If Not edit_hWnd.Equals(IntPtr.Zero) Then
SendMessageW(edit_hWnd, EM_SETCUEBANNER, True, text)
End If
ElseIf TypeOf control Is TextBox Then
' get handle to textbox
SendMessageW(control.Handle, EM_SETCUEBANNER, True, text)
End If
End Sub
#End Region
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
' set combo to sorted
ComboBox1.Sorted = True
'set combo cuebanner text
SetCueBanner(ComboBox1, "Please Select")
' add some items to combo
ComboBox1.Items.Add("Moses")
ComboBox1.Items.Add("Able")
ComboBox1.Items.Add("Dan")
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
' add more items to combo
ComboBox1.Items.Add("Zebra")
ComboBox1.Items.Add("Mustang")
' Optional: reset combo index to display the cuebanner.
ComboBox1.SelectedIndex = -1
End Sub
End Class
That also only works if the DropDownStyle is set to DropDown, which means that the user can type anything into the control. Maybe there's a way to do it when the DropDownStyle is DropDownList but the is no edit window to send that message to.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|