Like the help interface, i want to use a textbox and a listbox/combo box.
which one is preferable list box or combo box ?
does anyone has a sample code for the same ....??
thanks
Printable View
Like the help interface, i want to use a textbox and a listbox/combo box.
which one is preferable list box or combo box ?
does anyone has a sample code for the same ....??
thanks
Code:'create a new project
'add a listbox, combobox, textbox
'run the app
Private Sub Combo1_Click()
Text1 = Combo1.Text & " Whatever else you want to add"
End Sub
Private Sub Form_Load()
Form1.Width = 7470
Form1.Height = 3600
Form1.WindowState = vbNormal
List1.Top = 120
List1.Left = 4440
List1.Width = 2415
List1.Height = 2010
Combo1.Top = 2520
Combo1.Left = 240
Combo1.Width = 5055
Text1.Text = ""
Text1.Left = 240
Text1.Top = 120
Text1.Width = 3975
Text1.Height = 2175
Text1.Text = "CLICK ME"
End Sub
Private Sub List1_Click()
Text1.Text = List1.Text & " Whatever else you want to add"
End Sub
Private Sub Text1_Click()
Dim i As Integer
For i = 1 To 20
List1.AddItem i & " List1"
Combo1.AddItem i & " Combo1"
Next i
List1.ListIndex = 0
Combo1.ListIndex = 0
End Sub
i wanted to know which is better, listbox or combo box ??
and i wanted a sample code, when i type in the text box , it should automatically scroll to the specific letter in the list box or combo box. any suggestions ....
thanx
The following code does what you are trying to do:
As far as which control is better, it's really up to your role as a software engineer to decide. If you want the user to be able to make multiple selections within the list then you should use a listbox otherwise a combobox would probably be better. (The previous code works with both combo boxes and listboxes just replace 'Combo1' with whatever the name of your listbox is)Code:
Private Sub Text1_Change()
Dim sTxt As String, sCurTxt As String
Dim iTxtLen As Integer
Dim iPos As Integer
sTxt = LCase(Text1.Text) ' Load current text into variable and make everything lowercase
If Not sTxt = "" Then
iTxtLen = Len(sTxt) ' get length of string
For iPos = 0 To (Combo1.ListCount - 1)
sCurTxt = LCase(Left(Combo1.List(iPos), iTxtLen))
If sCurTxt = sTxt Then ' do strings match
' if so show list value
Combo1.ListIndex = iPos
'exit loop
iPos = Combo1.ListCount - 1
End If
Next iPos
Else
'if no text displayed then don't show anything in the combo box
Combo1.ListIndex = -1
End If
End Sub
By the way as far as the previous code is concerned if you want your search of the list to be case specific (i.e. if you only want results to be returned if the user types exact matches in capitalization) then just remove the LCase function calls.
Good Luck. :D
Hi! kan_ana, is this you looking for?
The CB_FINDSTRING and LB_FINDSTRING only find those item in the combo/listbox lists which the fist character is match with those that you try to search.
You also can use the CB_FINDSTRINGEXACT and LB_FINDSTRINGEXACT to search the totally identical string in the combo/listbox listed item.
Code:Option Explicit
'NOTE:
'Please change the lParam Datatype from Long to Any
'Else the SendMessage API will not function correctly.
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
'For ComboBox
Private Const CB_FINDSTRING = &H14C
Private Const CB_FINDSTRINGEXACT = &H158
'For List View
Private Const LB_FINDSTRING = &H18F
Private Const LB_FINDSTRINGEXACT = &H1A2
Private Sub Command1_Click()
'Text1.text consits of the string that you try to search...
Combo1.ListIndex = SendMessage(Combo1.hwnd, CB_FINDSTRING, -1&, ByVal CStr(Text1.Text))
End Sub
Private Sub Command2_Click()
'Text1.text consits of the string that you try to search...
List1.ListIndex = SendMessage(List1.hwnd, LB_FINDSTRING, -1&, ByVal CStr(Text1.Text))
End Sub
Private Sub Form_Load()
With Combo1
.AddItem "Computer"
.AddItem "Screen"
.AddItem "Modem"
.AddItem "Printer"
.AddItem "Scanner"
.AddItem "Sound Blaster"
.AddItem "Keyboard"
.AddItem "CD-Rom"
.AddItem "Mouse"
.ListIndex = 0
End With
With List1
.AddItem "Computer"
.AddItem "Screen"
.AddItem "Modem"
.AddItem "Printer"
.AddItem "Scanner"
.AddItem "Sound Blaster"
.AddItem "Keyboard"
.AddItem "CD-Rom"
.AddItem "Mouse"
End With
End Sub
If you use a DBCombo box, style 2, and set it's matchedentry property to 1 then it will do it for you. As you enter letters in the text part of the combo the info will slide up to you.
thanx guys !! for the help