System.AccessViolationException关于文本框自动完成建议 [英] System.AccessViolationException On Textbox autocomplete suggestion
问题描述
我是vb.net上的新手
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
抛出异常:System.Windows.Forms.dll中的'System.AccessViolationException'
$
System.Windows.Forms中出现未处理的类型'System.AccessViolationException'异常.dll
附加信息:尝试读取或写入受保护的内存。这通常表明其他内存已损坏。
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作为EventArgs)
      Dim txt = DirectCast(发件人,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
     如果(Len(txt.Text)> 1)则为
          Accs = getProductName(txt.Text)
         如果Accs IsNot Nothing和Accs.Count> 0然后
              txt.AutoCompleteCustomSource = Accs
              Console.WriteLine(Accs.ToString)
         结束如果
     结束如果
      txt.Enabled = True
      txt.Focus()
 结束子
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
这是错误
尝试了所有但无法解决的问题
Tried every thing but Unable to solve it
请尽快帮助我项目
推荐答案
首先,TextBox的AutoComplete行为的定义不应该在Changed事件处理程序中。
First, the definition for the AutoComplete behavior of the TextBox should not be in the Changed event handler.
其次,你没有提供GetProductName函数,它是抛出错误的地方吗?
Second, you do not provide the GetProductName function, is it where the error is thrown?
第三,你的Try ... Catch在哪里?
Third, where are your Try...Catch?
至于代码:
- 在Load for example中构建你的AutoCompleteStringCollection:
-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.
这篇关于System.AccessViolationException关于文本框自动完成建议的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!