Solved

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

Posted on 2013-01-04
12
591 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 3
12 Comments
 
LVL 75

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
Increase Agility with Enabled Toolchains

Connect your existing build, deployment, management, monitoring, and collaboration platforms. From Puppet to Chef, HipChat to Slack, ServiceNow to JIRA, Splunk to New Relic and beyond, hand off data between systems to engage the right people.

Connect with xMatters.

 
LVL 75

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
 
LVL 75

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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
Simple Linear Regression

695 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