[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
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
?
597 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
10 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

Question has a verified solution.

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

Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
Without even knowing it, most of us are using web applications on a daily basis.  In fact, Gmail and Yahoo email, Twitter, Facebook, and eBay are used by most of us daily—and they are web applications. We generally confuse these web applications to…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Suggested Courses

873 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