动态更改文本框的自动完成列表导致AccessViolationException,任何建议? [英] Dynamically changing Textbox's AutoComplete List causes AccessViolationException, any advice?

查看:215
本文介绍了动态更改文本框的自动完成列表导致AccessViolationException,任何建议?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的客户希望在应用程序的客户窗体中设置一个文本框,其中提供了适用于开始的街道名称的结尾。他开始输入一个街道名称,并且文本框提供一个街道列表,首先是他输入到文本框中的字符序列。

My client wanted to have a textbox in the Customer form of the application, which offers the applicable endings to a started street name. He starts to type a street name and the textbox offers a list of streets which start with the char sequence he typed into the textbox.

我对自己说:文本框具有AutoCompleteCustomSource属性,即使一般的街道名称列表将比启动时预先填写的更长一些,我只需点击一个查询的数据库,填入一个AutoCompleteStringCollection并显示给用户。

I said to myself: thats okay, textboxes have the AutoCompleteCustomSource property and even though a list of common street names will be longer than it could be pre-filled on start, i could just hit a database with a query, populate an AutoCompleteStringCollection and show that to the user.

现在这里是:如果我使列表填充在每个按键/键下,任何,程序崩溃并抛出一个AccessViolationException。

Now here's the thing: if I make the list populate on every keypress/keydown whatever, the program crashes and throws an AccessViolationException.

我发现这是因为:
当同时修改AutoComplete列表时,该控件正在显示AutoComplete列表的中间,导致崩溃。

I've found out that that's because: The control is in the middle of showing the AutoComplete list when at the same time it is being modified, resulting in the crash.

刷新自动填充列表时,将使用新的指针重新创建控件。键盘和鼠标事件(KeyPress,MouseOver,MouseLeave,MouseHover)尝试引用旧控件的指针,这些指针现在在内存中无效,导致内存访问冲突发生。

When you refresh the Autocomplete List, the control is recreated with new pointers. Keyboard and mouse events (KeyPress, MouseOver, MouseLeave, MouseHover) attempt to reference the old control's pointers which are now invalid in memory causing a memory access violation to occur.

一旦在窗口上设置了AutoComplete候选列表对象,底层AutoComplete实现就不允许更改。为了允许更改列表,WinForms会破坏Edit控件或ComboBox并重新创建它。如果底层控件在AutoComplete窗口仍然使用时被破坏,则会导致异常。

The underlying AutoComplete implementation does not allow for changing the AutoComplete candidate list object once it has been set on a window. To allow changing the list, WinForms destroys the Edit control or ComboBox and recreates it. This causes an exception if the underlying control is destroyed while the AutoComplete window is still use it.

我在 MSDN ,他们的解决方案:

I read about this on MSDN, their resolution:


不要修改自动完成候选人列表在关键事件期间动态执行。

Do not modify the AutoComplete candidate list dynamically during key events.

我也尝试过从这个线程

I've also tried everything from this thread

那么,如果我坚持提供适用的街道名称按键击键,那么我该怎么做呢?

So how could I make this work, if I insist on offering the applicable street names keypress-by-keypress?

注意:我知道你可以通过创建一个自定义控件来实现这一点,而只能使用纯编码精灵来实现?

Note: I know that you can do this by creating a custom control and such, but can it be done with just pure coding wizardry?

推荐答案

我们在应用程序中解决了这个问题(我们需要选择的方式)从可能的100,000个项目)是保释自动完成功能,并使用组合框。

The way that we solved this issue in our application (where we need to select from possibly 100,000 items) was to bail on the auto-complete functionality and use a combobox instead.

我们使用Infragistics组合框,但我怀疑标准的窗口将

We use the Infragistics combobox, but I suspect that the standard windows one would work as well.

这里的诀窍是使用DropDown模式中的组合框本身作为自动填充列表,并将其填充为用户类型。

The trick here is to use the combobox itself, in DropDown mode, as the autocomplete list and populate it as the user types.

这是我们使用的逻辑:

Private m_fOkToUpdateAutoComplete As Boolean
Private m_sLastSearchedFor As String = ""

Private Sub cboName_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles m_cboName.KeyDown
    Try
        ' Catch up and down arrows, and don't change text box if these keys are pressed.
        If e.KeyCode = Keys.Up OrElse e.KeyCode = Keys.Down Then
            m_fOkToUpdateAutoComplete = False
        Else
            m_fOkToUpdateAutoComplete = True
        End If
    Catch theException As Exception
        ' Do something with the exception
    End Try
End Sub


Private Sub cboName_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles m_cboName.TextChanged
    Try
        If m_fOkToUpdateAutoComplete Then
            With m_cboName
                If .Text.Length >= 4 Then
                    ' Only do a search when the first 4 characters have changed
                    If Not .Text.Substring(0, 4).Equals(m_sLastSearchedFor, StringComparison.InvariantCultureIgnoreCase) Then
                        Dim cSuggestions As StringCollection
                        Dim sError As String = ""

                        ' Record the last 4 characters we searched for
                        m_sLastSearchedFor = .Text.Substring(0, 4)

                        ' And search for those
                        ' Retrieve the suggestions from the database using like statements
                        cSuggestions = GetSuggestions(m_sLastSearchedFor, sError)
                        If cSuggestions IsNot Nothing Then
                            m_cboName.DataSource = cSuggestions
                            ' Let the list catch up. May need to do Thread.Idle here too
                            Application.DoEvents()
                        End If
                    End If
                Else
                    If Not String.IsNullOrEmpty(m_sLastSearchedFor) Then
                        ' Clear the last searched for text
                        m_sLastSearchedFor = ""
                        m_cboName.DataSource = Nothing
                    End If
                End If
            End With
        End If
    Catch theException As Exception
        ' Do something with the exception
    End Try
End Sub

由于大量的项目,我们不会开始搜索,直到用户输入4个字符,但这只是我们的实现。

Due to the large number of items, we don't start searching until the user has entered 4 characters, but that is just our implementation.

这篇关于动态更改文本框的自动完成列表导致AccessViolationException,任何建议?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆