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

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

问题描述

我的客户希望在应用程序的 Customer 表单中有一个文本框,它为开头的街道名称提供适用的结尾.他开始输入街道名称,文本框提供了以他在文本框中输入的字符序列开头的街道列表.

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 列表的同时正在修改它,导致崩溃.

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 并重新创建它.如果底层控件被销毁而自动完成窗口仍在使用它,这将导致异常.

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.

我也尝试了 主题

那么,如果我坚持提供适用的街道名称 keypress-by-keypress,我怎么能做到这一点?

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 组合框,但我怀疑标准 Windows 组合框也能正常工作.

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天全站免登陆