Solved

Visual C#, Autosuggest Combobox

Posted on 2009-05-14
9
2,312 Views
Last Modified: 2013-12-17
Hi Friends,

I am developing a windows application using VS 2008 with C#, I need a combobox with autosuggest feature e.g. when user enter any key in combobox using that text application fetches related records from database and show as items in combobox and user could select from the given items.

say user enters "com" and application query a specific database to fetch records starting with "Com%" and user could select from it.

thanks,
0
Comment
Question by:ArunVashist
  • 5
  • 3
9 Comments
 
LVL 16

Expert Comment

by:CuteBug
ID: 24384588
Set the AutoCompleteMode property of the ComboBox to true.

And set the AutoCompleteCustomSource property as detailed in the following link

http://social.msdn.microsoft.com/Forums/en-US/winforms/thread/d7672f50-a620-4b5c-acc8-7ce7c61b04e9
0
 
LVL 21

Expert Comment

by:Tapan Pattanaik
ID: 24384740
0
 

Author Comment

by:ArunVashist
ID: 24384915
Hi Cute Bug,

As suggested I am using following code in "onTextChanged" event of combox box but I am getting the  error "Attempted to read or write protected memory. This is often an indication that other memory is corrupt".

following is the code.

 private void cbMedicineName_TextChanged(object sender, EventArgs e)
        {
            string QText;
            if (cbMedicineName.Text == "" || cbMedicineName.Text.Length < 2)
            {
                QText = "Do not load";
            }
            else
            {
                QText = cbMedicineName.Text;
            }
           
            OleDbDataReader Drugs = objPatient.GetDrugsList(QText);
            DataSet temp = new DataSet();
            temp.Load(Drugs, LoadOption.OverwriteChanges, "DrugsList");
               
           
            cbMedicineName.AutoCompleteMode = AutoCompleteMode.Suggest;
            cbMedicineName.AutoCompleteSource = AutoCompleteSource.CustomSource;
            cbMedicineName.AutoCompleteCustomSource.Clear();          
               
            foreach (DataRow dr in temp.Tables["DrugsList"].Rows)
            {
               cbMedicineName.AutoCompleteCustomSource.Add(dr["DrugName"].ToString());
            }
           
           
        }
0
 

Author Comment

by:ArunVashist
ID: 24385289
HI Tapan,

I need a combobox with autosuggest feature which updated the items in combobox from database according to text input. The examples you sent are for autocomplete which just search the existing items in combobox.

Thanks,
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 16

Expert Comment

by:CuteBug
ID: 24386248
You can do it this way

AutoCompleteStringCollection acsc = new AutoCompleteStringCollection();
 

foreach (DataRow dr in temp.Tables["DrugsList"].Rows)

{

   acsc.Add(dr["DrugName"].ToString());

}
 

cbMedicineName.AutoCompleteCustomSource = acsc;

cbMedicineName.AutoCompleteMode = AutoCompleteMode.Suggest;

cbMedicineName.AutoCompleteSource = AutoCompleteSource.CustomSource;

Open in new window

0
 

Author Comment

by:ArunVashist
ID: 24386611
Hi Cute bug,

Actually the suggested method works for the first time but when on "KeyPress"  or "TextChanged" event I requery the database and try to refresh items of AutoCompleteSoure.CustomSource I get the error "Attempted to read or write protected memory. This is often an indication that other memory is corrupt".

it would be better if you send me some working sample code.

thanks,
0
 
LVL 16

Expert Comment

by:CuteBug
ID: 24400893
I think you do not have to requery in the database each time you do a key press.

Instead during the creation of the combobox, set the autocompletecustom source like the code given previously.

AutoComplete will handle the rest when you do a keypress...
0
 

Author Comment

by:ArunVashist
ID: 24404076
Hi Cute Bug,

Actually the item count is more then 200k, and it take hell lot of time loading these values into list, so I want only to add required items in combobox. so requery and rebinding become necessary, kindly guide if any other way out is there.

thanks,
0
 

Accepted Solution

by:
ArunVashist earned 0 total points
ID: 24435628
Hi as it was not feasible to load 200k records into Combobox Items now I am switched to Textbox, on Keydown event I requery the database according to text of Textbox and load the result into Contextmenu.

its really fast and easy to use contextmenu.
0

Featured Post

3 Use Cases for Connected Systems

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

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Error on Add method 1 38
SQL Exceptions 3 39
Page navigation in windows phone 8.1 application not working. 5 19
Video Player 2017 5 14
There is an easy way, in .NET, to centralize the treatment of all unexpected errors. First of all, instead of launching the application directly in a Form, you need first to write a Sub called Main, in a module. Then, set the Startup Object to th…
Having just graduated from college and entered the workforce, I don’t find myself always using the tools and programs I grew accustomed to over the past four years. However, there is one program I continually find myself reverting back to…R.   So …
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

867 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

15 Experts available now in Live!

Get 1:1 Help Now