0

enter image description here

In textchanged event Access Violation Exception occurs frequently.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343

2 Answers2

0

it's a security level problem may be. try this: Go to the Project Property page, and then go into the Security Section.

"Enable ClickOnce Security Settings" will be seen checked. uncheck it

reference: https://social.msdn.microsoft.com/Forums/windows/en-US/e599b2e2-3cda-43ad-b15f-a69b4fea1a75/dynamic-filling-of-textbox-autocomplete-not-working?forum=winforms

0

I too wanted to populate a TextBox AutoCompleteCustomSource using the users input from that same textbox as a filter, so I had the TextChanged event grabbing data to fill the dropdown suggestions list.

I tried so many different ways to fill the data, which all resulted in random or constant Access Violation. If I'm not wrong I finally have it nailed down I think.

It seems that it doesn't matter what method you're using to actually add the items to the AutoCompleteCustomSource. The problem arises when you go to replace those items.

Before changing any of the data in TheTextbox.AutoCompleteCustomSource, you need to first set:

TheTextBox.AutoCompleteSource = AutoCompleteSource.None    

Then you can use any method you like to clear and/or refill it:

TheTextBox.AutoCompleteCustomSource.Clear()
TheTextBox.AutoCompleteCustomSource.AddRange(AutoSuggestItems)

Set the AutoCompleteSource back to CustomSource when you're done:

TheTextBox.AutoCompleteSource = AutoCompleteSource.CustomSource

' prevent that annoying selection of the whole textbox text
TheTextBox.Select(Me.TheTextBox.Text.Length, 0)

*Note: If you also set TheTextBox.AutoCompleteMode while changing the data like I was originally, it causes the Access Violation error later. (SO FAR) the above method has worked correctly. I hope this is my last update to this solution :)

  • Another thread discussing this exact problem: https://stackoverflow.com/questions/8779557/dynamically-changing-textboxs-autocomplete-list-causes-accessviolationexception – user2101511 Jul 18 '20 at 23:13