Pulkit Gaur

dream - do - deserve

Combobox and TreeView handy tricks

Filed under: Visual Basic at 09:21:19 On 2006-09-27

While working on a small project i used these frequently,thought should share it !


1)To Select First Item as default in Dropdown Combobox use :


ComboBoxName.ListIndex = 0

2)To remove a particular nodes children from tree view use this fragment :

While treeControl.SelectedItem.Children > 0
treeControl.Nodes.Remove treeControl.SelectedItem.Child.Key

3)This code fragment facilitates ajax type autosuggest/autocomplete feature in combobox.This code is not mine !


'Api declaration
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Integer, ByVal lParam As Long) As Long

'Cont declaration
Private Const CB_FINDSTRING As Long = &H14C

'Procedure declaration
Public Sub ComboAutoComplete(ByRef SourceCtl As VB.ComboBox, _
ByRef KeyAscii As Integer, ByRef LeftOffPos As Long)
Dim iStart As Long
Dim sSearchKey As String

With SourceCtl
'If text entered so far matches item(s) in the list, use autocomplete
Select Case Chr$(KeyAscii)
Case vbBack
'Let backspace characters process as usual; otherwise try to match text
Case Else
If Chr$(KeyAscii) <> vbBack Then
.SelText = Chr$(KeyAscii)

iStart = .SelStart

If LeftOffPos <> 0 Then
.SelStart = LeftOffPos
iStart = LeftOffPos
End If

sSearchKey = CStr(Left$(.Text, iStart))
.ListIndex = SendMessage(.hwnd, CB_FINDSTRING, -1, _
ByVal CStr(Left$(.Text, iStart)))

If .ListIndex = -1 Then
LeftOffPos = Len(sSearchKey)
End If

.SelStart = iStart
.SelLength = Len(.Text)
LeftOffPos = 0

KeyAscii = 0
End If
End Select
End With
End Sub

Use this code as follows:

Private Sub Combo1_KeyPress(KeyAscii As Integer)
Static lWhere As Long
ComboAutoComplete Combo1, KeyAscii, lWhere
End Sub

1 Comment(s) | Add your own

Leave a Comment


Required, hidden