Solved

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

Posted on 2013-01-04
12
590 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 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
How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

730 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