Solved

How can I implement autocompletion in a combobox that has a generic list as its datasource?

Posted on 2013-01-04
12
581 Views
Last Modified: 2013-01-25
I've created a generic list (a collection) and made it the dataSource for my combobox.  
Now I need to use autocompletion for when the user begins to type in the combobox while searching for something in the list.  There seems to be a good amount of help on the web for autocompletion where the combobox has a different kind of datasource (ie. datatable) but I'm still scratching my head trying to get this to work with a generic list.  

Suggestions?
0
Comment
Question by:David L. Hansen
  • 7
  • 3
12 Comments
 
LVL 74

Expert Comment

by:käµfm³d 👽
ID: 38745811
A generic list of what? Strings? Custom objects?
0
 
LVL 15

Author Comment

by:David L. Hansen
ID: 38745820
Custom objects.  Each item (object) has several properties and methods.  The collection object which holds these items uses the System.ComponentModel.BindingList(Of T) where T is my custom object.
0
 
LVL 15

Author Comment

by:David L. Hansen
ID: 38745821
The collection object is the combobox's datasource.
0
 
LVL 74

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 38745829
So something like this?

using System.Collections.Generic;
using System.Windows.Forms;

namespace WindowsFormsApplication5
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            List<CustomObject> originalList = new List<CustomObject>()
            {
                new CustomObject() { Text = "Hello World!" },
                new CustomObject() { Text = "Goodbye World!" },
            };
            System.ComponentModel.BindingList<CustomObject> bindingList = new System.ComponentModel.BindingList<CustomObject>(originalList);

            this.comboBox1.DataSource = bindingList;
            this.comboBox1.DisplayMember = "Text";
            this.comboBox1.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
            this.comboBox1.AutoCompleteSource = AutoCompleteSource.ListItems;
        }
    }

    public class CustomObject
    {
        public string Text { get; set; }
    }
}

Open in new window

0
 
LVL 15

Author Comment

by:David L. Hansen
ID: 38751812
Ahh Monday, back to work.  BTW, thanks for your help on this.

So, when I use:
this.comboBox1.AutoCompleteSource = AutoCompleteSource.ListItems;

Open in new window

I get the following error - "Value of type 'System.Windows.Forms.AutoCompleteSource' cannot be converted to 'System.Windows.Forms.AutoCompleteStringCollection'.
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 74

Expert Comment

by:käµfm³d 👽
ID: 38751951
Can you post your implementation? The code I posted was a working sample.
0
 
LVL 15

Author Comment

by:David L. Hansen
ID: 38755268
Sure, here it is:
    
'IN-FORM CODE:
Private Sub frmOfficiant_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Officiants = New Officiants
        Dim blankOfficiant As New Officiant 'this is provided for the user to add a new officiant if need be
        Officiants.RefreshFromDatabase()
        Officiants.Insert(0, blankofficiant)

        With Me.cboOfficiantLastName
            .DataSource = Officiants
            .DisplayMember = "LastName"
            .AutoCompleteMode = AutoCompleteMode.SuggestAppend
            .AutoCompleteCustomSource = AutoCompleteSource.ListItems
        End With

End Sub

'-----------------------------------------------

'COLLECTION CLASS OF CUSTOM CLASS
Public Class Officiants
    Inherits System.ComponentModel.BindingList(Of Officiant)

    'the following three subs allow this class to know when a 
    'new item is inserted, added, or the list is changed. 
    Protected Overrides Sub InsertItem(ByVal index As Integer, ByVal item As Officiant)
        MyBase.InsertItem(index, item)
    End Sub

    Protected Overrides Sub OnAddingNew(ByVal e As System.ComponentModel.AddingNewEventArgs)
        MyBase.OnAddingNew(e)
    End Sub

    Protected Overrides Sub OnListChanged(ByVal e As System.ComponentModel.ListChangedEventArgs)
        MyBase.OnListChanged(e)
    End Sub

    Public Sub RefreshFromDatabase()

        Dim strsql As String = _
            "SELECT officiant_id, title, first_name, " & _
            " last_name, address1, address2, " & _
            " city, state, zip, " & _
            " phone " & _
            "FROM Officiant " & _
            "--where --ADD WHERE STATEMENT HERE "

        Dim drItems As SqlClient.SqlDataReader
        drItems = DataSrc.GetDataReader(strsql)

        'clear current list 
        MyBase.Clear()

        'cycle through all items and add to list 
        While drItems.Read
            Dim x As New Officiant
            x.OfficiantId = CInt(NullToZero(drItems("officiant_id")))
            x.Title = CStr(NullToBlank(drItems("title")))
            x.FirstName = CStr(NullToBlank(drItems("first_name")))
            x.LastName = CStr(NullToBlank(drItems("last_name")))
            x.Address1 = CStr(NullToBlank(drItems("address1")))
            x.Address2 = CStr(NullToBlank(drItems("address2")))
            x.City = CStr(NullToBlank(drItems("city")))
            x.State = CStr(NullToBlank(drItems("state")))
            x.Zip = CStr(NullToBlank(drItems("zip")))
            x.Phone = CStr(NullToBlank(drItems("phone")))
            MyBase.Add(x)
        End While

        'close the datareader 
        drItems.Close()

    End Sub

.
.
.

Open in new window

0
 
LVL 15

Author Comment

by:David L. Hansen
ID: 38769048
Was that helpful?
0
 
LVL 15

Author Comment

by:David L. Hansen
ID: 38776136
Any ideas?
0
 
LVL 15

Author Comment

by:David L. Hansen
ID: 38819532
Finally found the issue!  I had:
 .AutoCompleteCustomSource = AutoCompleteSource.ListItems

instead of:
.AutoCompleteSource = AutoCompleteSource.ListItems

I just couldn't see the difference even though it was right there in front of me.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now