none
System.AccessViolationException On Textbox autocomplete suggestion RRS feed

  • Question

  • I am new on vb.net

    My Problem is whenever i want to show the typed result on autocomplete suggestion the project breaks and showing the   

    Exception thrown: 'System.AccessViolationException' in System.Windows.Forms.dll
    An unhandled exception of type 'System.AccessViolationException' occurred in System.Windows.Forms.dll
    Additional information: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

    My code are on changed event for Textbox

    Private Sub txtTextProductChanged(sender As Object, e As EventArgs)
            Dim txt = DirectCast(sender, TextBox)
            'MsgBox(txt.Text)
            txt.Enabled = False
            Dim Accs As AutoCompleteStringCollection
            txt.AutoCompleteMode = AutoCompleteMode.None
            txt.AutoCompleteSource = AutoCompleteSource.None
            txt.AutoCompleteCustomSource.Clear()

            txt.AutoCompleteMode = AutoCompleteMode.Suggest
            txt.AutoCompleteSource = AutoCompleteSource.CustomSource
            If (Len(txt.Text) > 1) Then
                Accs = getProductName(txt.Text)
                If Accs IsNot Nothing And Accs.Count > 0 Then
                    txt.AutoCompleteCustomSource = Accs
                    Console.WriteLine(Accs.ToString)
                End If
            End If
            txt.Enabled = True
            txt.Focus()
        End Sub

    This are the Errors


    Tried every thing but Unable to solve it

    Please Help me as soon as possible for the ongoing project




    Friday, August 4, 2017 10:06 AM

All replies

  • First, the definition for the AutoComplete behavior of the TextBox should not be in the Changed event handler.

    Second, you do not provide the GetProductName function, is it where the error is thrown?

    Third, where are your Try...Catch?

    As far as the code:

    -build up your AutoCompleteStringCollection in the Load for exemple:

     Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            'Load into MySource all the words for the AutoComplete
            Dim MySource As New AutoCompleteStringCollection()
    
            MySource.AddRange(New String() _
                                {"January","February","March","April","May",
    "June","July","August","September","October", "November","December"
                                })
            
            ' Initialize the text box.
            With TextBox1
                .AutoCompleteCustomSource = MySource
                .AutoCompleteMode = AutoCompleteMode.SuggestAppend
                .AutoCompleteSource = AutoCompleteSource.CustomSource
                .Location = New Point(20, 20)
                .Width = (Me.ClientRectangle.Width - 40) \ 2
                .Visible = True
            End With
        End Sub
    and that's it you have autosuggest on the list provided.

    • Proposed as answer by Frank L. Smith Friday, August 4, 2017 11:19 AM
    Friday, August 4, 2017 10:53 AM
  • Hello,

    If you would like a fully functional example, see my MSDN code sample for both VB.NET and C#.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Friday, August 4, 2017 5:39 PM
    Moderator