AutoComplete combobox

Ricky D

Well-Known Member
Licensed User
Longtime User
Hi all.

I am now going to put my auto complete combobox into a library.

So far it's looking good.

It doesn't need to use timers - I'm writing it in vb.net and the guts of the processing is in the KeyUp event of the textbox of the library.

I'll post it up when I'm done which will be shortly.

regards, Ricky
 

Ricky D

Well-Known Member
Licensed User
Longtime User
umm a slight challenge

I have created the autocomplete functionality just fine.

Now I need to be able to add the control to a tab page from the tabcontrol.

I'm not sure how to do this.

At present I can add it to a form easily.

here is the entire vb.net code for the dll

Imports System.Windows.Forms
Imports System.Drawing

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

FormName.Controls.Add(cboCombo)
FormName.Controls.Add(txtText)
txtText.BringToFront()

mAutoCompleteOn = True

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(eventSelectionIndexChangedObject, Nothing)
End Sub
End Class


any ideas?

regards, Ricky
 

agraham

Expert
Licensed User
Longtime User
Now I need to be able to add the control to a tab page from the tabcontrol.

I'm not sure how to do this.
Add it to a form then use :-

TabControl.AddControl(comboname.ControlRef, page, left, top)

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.
B4X:
Public Property ControlRef() As Control
  Get
    Return cboCombo 
  End Get
  Set(ByVal value As Control)
    cboCombo = value
  End Set
End Property
 

Ricky D

Well-Known Member
Licensed User
Longtime User
slight problem with it

thanks agraham but now the autoselect part of the combox doesn't work.

Do I also need to expose the textbox of the dll like I do the combobox?

regards, Ricky

EDIT: I did and it now works. Thanks a lot mate
I had to add a Method that brings the textbox to the front
 
Last edited:

Cableguy

Expert
Licensed User
Longtime User
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
 

Ricky D

Well-Known Member
Licensed User
Longtime User
ok, when i get home

no worries mate.

i'll post the C# version plus the .dll

i need to put together a .chm help file.

i haven't implemented the items collection.

it supports vga & qvga.

regards, Ricky
 

Cableguy

Expert
Licensed User
Longtime User
Thanks...Please PM after release to add you to the dll version listing...
 

Ricky D

Well-Known Member
Licensed User
Longtime User
I'll put it on hold

so I can rewrite it so it can do a ShowDropdown method.

This requires me to ditch the combobox in the dll and put in place a button and a listbox.

As this is in C# and I'm still learning it might take me a couple of weeks.

I haven't used a listbox in C# yet.

regards, Ricky
 
Top