Introduction

During work on a project, I needed to use a control having auto complete facility in it as user types. I realized that we can make use of the textbox control or combobox control for this purpose. Both controls can be used to filter records as a drop down list to show the best matches. I will demonstrate it using VB.NET. If someone needs a C# version, an online converter can be used. I will be discussing the addition and removal of item from the drop down list.

Prerequisites

Before reading this article, we need to be aware of two properties and an enumeration introduced by Microsoft in the .NET Framework 2.0 which is AutoCompleteCustomSource property, AutoCompleteMode property and AutoCompleteSource enumeration.

We should also have a look at AutoCompleteStringCollection class.

Design

An auto complete source is binded to the textbox’s AutoCompleteCustomSource property. It will help to filter the best fit records in the suggestion list.

'Item is filled either manually or from database
Dim lst AsNew List(OfString)
'AutoComplete collection that will help to filter keep the records.
Dim MySource AsNew AutoCompleteStringCollection()

Implementation

I have filled the source from the list named ‘lst’ at Form1_Load event. This list can be populated by the database as well.

sorry for my english, I'm newbie in .Net programming I am using vb. net 2010 framework 4. I want to make the autocomplete textbox but get error message "AccessViolationException". provisions:2 pieces that use autocomplete textbox, textbox each data retrieved from a database type (sql server) but different databases. the code that I know:

At First, Thank you for reading this article. In response to the issue you have reported, need further clarifications on the following:

1. Can you provide more details about the exact line having this error ?2. Are you sure you can access the database used for second text box ? Have you tried to use first text box with code written for second text box ? 3. Have you tried to set TBOwner.AutoCompleteMode = AutoCompleteMode.None before setting TBOwner.AutoCompleteSource ?4. Are you sure you are not the part of this problem ?

1) First step, if I run one by one (start the application and then enter the word on TBArtist, I get the result, then I close the application. and vice versa). I also have tried :I typed in the TBArtist, a result I managed to get the Artist's data after that I typed again in the TBOwner. when I enter the first character of the word "MR" there is no error message, when I enter the second character is "R" I get the error message :"AccessViolationException was unhandled"Attempted to read or write protected memory. This is often an indication that other memory is corrupt.System.AccessViolationException was unhandled Message=Attempted to read or write protected memory. This is often an indication that other memory is corrupt. Source=System.Windows.Forms StackTrace: at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg) at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData) at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context) at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context) at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun() at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel() at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine) at Inventory.My.MyApplication.Main(String[] Args) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 81 at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args) at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args) at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() at System.Threading.ThreadHelper.ThreadStart_Context(Object state) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Threading.ThreadHelper.ThreadStart() InnerException:

2) Of course, I also tried to use first textbox with code written for second textbox, I put first texbox code to the second texbox the result get error message "AccessViolationException was unhandled". I close the application with curiosity and then I run the application again by entering a word into the first textbox, the result is the same error message.then I clean up the solution & my project by going to menu Build -> CLEAN SOLUTION and BUILD -> CLEAN PROJECT. then I tried again and the result is the same error message, even when I return to the code structure as the original condition and I am trying to give it back but the result is the same error message "AccessViolationException was unhandled".

3) I've tried it, below is the code I useSTEP 1 => The result is the same error message==============================================