Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sub routine to findstring in combobox

Status
Not open for further replies.

bnbunch

Programmer
Sep 29, 2015
3
US
I have following code to search combobox which I need to repeat in different forms and different comboboxes

I am using from key up event

Private Sub ComboBox1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles ComboBox1.KeyUp

' Store the actual text that has been typed.
actual = Me.ComboBox1.Text
Me.TextBox1.Text = Me.ComboBox1.Text

' Find the first match for the typed value.
index = Me.ComboBox1.FindString(actual)
TextBox2.Text = Me.ComboBox1.FindString(actual)

' Get the text of the first match.
If (index > -1) Then
found = Me.ComboBox1.Items(index).ToString()
Me.TextBox2.Text = found
' Select this item from the list.
Me.ComboBox1.SelectedIndex = index

' Select the portion of the text that was automatically
' added so that additional typing will replace it.
Me.ComboBox1.SelectionStart = actual.Length
Me.ComboBox1.SelectionLength = found.Length
End If

end sub

I would like to repeat by passing my combobox name to a subroutine so i don't have to repeat this code each time
Is this possible?
 
The VB NET forum: forum796, here is VBA. In VBA I would create a function returning True/False depending on the search success, with two arguments: object ComboBox and searched text. This function would be called by every KeyUp event procedure.

combo
 
I created single routine that can be used from any combobox
Then call this routine from any form to create searchable combobox


Public Sub highlighttext(ThisCombobox As ComboBox)

' Store the actual text that has been typed.
actual = ThisCombobox.Text

' Find the first match for the typed value.
index = ThisCombobox.FindString(actual)


' Get the text of the first match.
If (index > -1) Then
found = ThisCombobox.Items(index).ToString


' Select this item from the list.
ThisCombobox.SelectedIndex = index

' Select the portion of the text that was automatically
' added so that additional typing will replace it.
ThisCombobox.SelectionStart = actual.Length
ThisCombobox.SelectionLength = found.Length
End If


End Sub

'*********** Sub to Sub ******************


Private Sub ComboBox1_KeyUp(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles ComboBox1.KeyUp





If e.KeyCode = Keys.Home Then
'// Move any code you have in SelectedIndexChanged to here.
'//Otherwise search will stop and indexchange will trigger

End If
'// Call Search subroutine

highlighttext(ComboBox1)

'// If combobox is in another from you must call by formname as below
'// Form Name. Subroutine(Paramater Control Name of Combobox)

Updated_ADD_Order.highlighttext(ICComboBox)

 
I got this working finally, but I have another problem. I have an older computer without a keypad, so I added an external numpad. The top number keys work just fine, but searchbox does not recognize numbers from keypad. I used code below to see which keycode is being sent.

Select Case e.KeyCode
Case Keys.NumPad1
MsgBox("Pressed 1")
Case Keys.NumPad2
MsgBox("Pressed 2")
Case Keys.NumPad3
MsgBox("Pressed 3")
Case Keys.NumPad4
MsgBox("Pressed 4")
Case Keys.NumPad5
MsgBox("Pressed 5")
Case Keys.NumPad6
MsgBox("Pressed 6")
Case Keys.NumPad7
MsgBox("Pressed 7")
Case Keys.NumPad8
MsgBox("Pressed 8")
Case Keys.NumPad9
MsgBox("Pressed 9")
Case Keys.NumPad0
MsgBox("Pressed 0")


Case 13
MsgBox("Enter")
Case 33
MsgBox("PgUp")
Case 34
MsgBox("PgDown")
Case 35
MsgBox("End")
Case 36
MsgBox("Home")
Case 37
MsgBox("Left Arrow")
Case 38
MsgBox("Up Arrow")
Case 39
MsgBox("Right Arrow")
Case 40
MsgBox("Down Arrow")
Case 45
MsgBox("Ins")
Case 46
MsgBox("Del")

Case 106
MsgBox("* Multiply")
Case 109
MsgBox("- Minus")
Case 110
MsgBox(". Decimal")
Case 111
MsgBox("/ Divided")
Case 107
MsgBox("+ Add")
Case 144
MsgBox("Numlock")


End Select


With numlock on, no matter which numbers 0-9 I press, It returns keycode 144 which is numlock. with numlock turned off, all special keys are recognized. How can I get it to recognize numpads on Numpad?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top