Public Class AutoCompleteCombo
Implements IDisposable 'Inherit from IDisposable if you need Basic4ppc to free resources when the application is closed.
Private WithEvents txtText As System.Windows.Forms.TextBox
Private WithEvents cboCombo As System.Windows.Forms.ComboBox
Public Event TextChanged As EventHandler
Public Event SelectionIndexChanged As EventHandler
Public Event GotFocus As EventHandler
Public Event LostFocus As EventHandler
Private eventTextChangedObject() As Object 'One object[] for each event (one event here).
Private eventSelectionIndexChangedObject As Object
Private eventGotFocusObject As Object
Private eventLostFocusObject As Object
Private mAutoCompleteOn As Boolean = True
Sub New(ByVal FormName As Control, ByVal Left As Int32, ByVal Top As Int32, ByVal Width As Int32, ByVal Height As Int32)
cboCombo = New ComboBox
cboCombo.Top = Top
cboCombo.Left = Left
cboCombo.Width = Width
cboCombo.Height = Height
txtText = New TextBox
txtText.Top = Top
txtText.Left = Left
txtText.Width = Width - 15
txtText.Height = Height
eventTextChangedObject = New Object() {Me, "TextChanged"}
eventSelectionIndexChangedObject = New Object() {Me, "SelectionIndexChanged"}
eventGotFocusObject = New Object() {Me, "GotFocus"}
eventLostFocusObject = New Object() {Me, "LostFocus"}
End Sub
Public Property AutoCompleteOn() As Boolean
Get
Return mAutoCompleteOn
End Get
Set(ByVal value As Boolean)
mAutoCompleteOn = value
End Set
End Property
Private Sub txtText_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtText.GotFocus
RaiseEvent GotFocus(eventGotFocusObject, Nothing)
End Sub
Private Sub txtText_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtText.KeyUp
Dim strg As String = txtText.Text.ToUpper
Dim item As String
Dim i As Integer
Dim found As Boolean = False
Dim obj As Object
If e.KeyData = Keys.Back Then
e.Handled = True
Exit Sub
End If
If mAutoCompleteOn = False Then
e.Handled = True
Exit Sub
End If
For i = 0 To cboCombo.Items.Count - 1
obj = cboCombo.Items.Item(i)
'If TypeOf obj Is Data.DataRowView Then
'Dim drv As Data.DataRowView = obj
'get the field from the displaymember
'Dim dsp As String = DisplayMember
'Dim tmp As Integer = dsp.IndexOf(".") + 1
'If tmp > 0 Then
'item = dsp.Substring(tmp)
'item = drv.Row.Item(item)
'Else
'item = drv.Row.Item(dsp)
'End If
'ElseIf TypeOf obj Is String Then
item = obj
'End If
'item = item.ToUpper()
If item.ToUpper.StartsWith(strg) Then
found = True
Exit For
End If
Next
If found Then
txtText.Text = item
txtText.SelectionStart = strg.Length
txtText.SelectionLength = txtText.Text.Length - strg.Length
cboCombo.SelectedIndex = i
Else
'RaiseEvent NotInList(txtText.Text)
Me.Height = txtText.Height
End If
End Sub
Private Sub txtText_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtText.LostFocus
RaiseEvent LostFocus(eventLostFocusObject, Nothing)
End Sub
Private Sub txtText_TextChanged(ByVal sender As Object, ByVal e As EventArgs) Handles txtText.TextChanged
RaiseEvent TextChanged(eventTextChangedObject, Nothing)
End Sub
Public Property Text() As String
Get
Return txtText.Text
End Get
Set(ByVal value As String)
txtText.Text = value
End Set
End Property
Public Property Height() As Int32
Get
Return cboCombo.Height
End Get
Set(ByVal Value As Int32)
cboCombo.Height = Value
txtText.Height = Value
End Set
End Property
Public Property Width() As Int32
Get
Return cboCombo.Width
End Get
Set(ByVal value As Int32)
cboCombo.Width = value
txtText.Width = value - 15
End Set
End Property
Public Sub Add(ByVal strToAdd As String)
cboCombo.Items.Add(strToAdd)
End Sub
Public Sub Dispose() Implements System.IDisposable.Dispose
If Not txtText Is Nothing Then
txtText.Dispose()
End If
If Not cboCombo Is Nothing Then
cboCombo.Dispose()
End If
End Sub
Private Sub cboCombo_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cboCombo.SelectedIndexChanged
txtText.Text = cboCombo.Items(cboCombo.SelectedIndex)
RaiseEvent SelectionIndexChanged(eventSelectionIndexChangedOb ject, Nothing)
End Sub
End Class
For this to work you need a property to return a reference to your underlying control. It is good practice to always provide this reference as it can also be used with things like AddEvent and the Door library. You probably don't need the Set but I normally include it.
Code:
Public Property ControlRef() As Control Get Return cboCombo End Get Set(ByVal value As Control) cboCombo = value End Set End Property
Ricky, I've been trying to make sense of you code in the dll, as you posted, and I'm not able to...
I've learned how to make dlls in c#, and although the two are not that diferent, some syntax just gets me off...
Could you release your dll in the Aditional libraries?
I'm sure that, others like me, would benefit from it...
Thanks in advance
__________________
Paulo Gomes - Porto, Portugal - Living/Working in France
Mobile Device: Samsung Galaxy S, Android 2.3.4 CUstom ROM
Laptop: Toshiba NB100-130 (running on Win7Ultimate)
My Posts helped you? Consider Buying me a Porto Glass!
Thanks...Please PM after release to add you to the dll version listing...
__________________
Paulo Gomes - Porto, Portugal - Living/Working in France
Mobile Device: Samsung Galaxy S, Android 2.3.4 CUstom ROM
Laptop: Toshiba NB100-130 (running on Win7Ultimate)
My Posts helped you? Consider Buying me a Porto Glass!