?
Solved

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

Posted on 2013-01-04
12
Medium Priority
?
592 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
Application Discovery Service in AWS

In the era of the cloud, customers migrating away from their existing on-premise infrastructure. This requires lots of planning, strategies, and effort to identify their existing resources and determine how best to migrate.  Datacenter migrations happen in four phases -

 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 2000 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

What do responsible coders do? They don't take detrimental shortcuts. They do take reasonable security precautions, create important automation, implement sufficient logging, fix things they break, and care about users.
The SignAloud Glove is capable of translating American Sign Language signs into text and audio.
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.
Progress
Suggested Courses

765 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