gpt4 book ai didi

vb.net - .NET 全文自动完成在组合框中。任何覆盖列表项的性能积极方式?

转载 作者:行者123 更新时间:2023-12-04 06:09:14 27 4
gpt4 key购买 nike

我正在努力满足上司的要求。真心希望有人能给点建议。
基本上在我们的项目中有很多地方可供选择。最具体的例子是选择世界上的一个城市。这些项目是数十万。
使用标准的 winforms 控件和属性,可以快速搜索列表。
问题是我们为所有项目使用城市和地区名称的串联。本质上 PREFIX autotomcomplete 可以工作,但不能按需要工作。任务是按项目任何部分中的任何给定字符串过滤和显示项目。本质上是组合框中的全文搜索。
有没有人知道在运行时相对快速地切换自动完成源并处理建议/建议附加事件?
该项目也在 VB.NET 中,尽管任何形式的 .NET 建议都会非常有帮助。

谢谢!

更新:使用 competent_tech 的最新尝试的建议稍作修改。

Imports System.Data
Public Class Form1
Private _ErrorText As String
Private _CommandExecuted As Boolean

Private m_fOkToUpdateAutoComplete As Boolean
Private m_sLastSearchedFor As String = ""

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Call Me.SetStatusText("Loading...")
Me._ErrorText = ""
Me.Cities.Clear()
Me.BackgroundWorker1.RunWorkerAsync()
End Sub

Private Sub BackgroundWorker1_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
Try
Me._CommandExecuted = True
Me.Ara_airportsTableAdapter.Fill(Me.Cities.ara_airports)
Catch ex As Exception
_ErrorText = ex.Message
End Try
End Sub

Private Sub BackgroundWorker1_RunWorkerCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
If Me._ErrorText = "" Then
Me.SetStatusText(Me.Cities.ara_airports.Count & " Records loaded")
Else
Me.SetStatusText(Me._ErrorText)
End If
Me.BindingSource.ResetBindings(False)
End Sub

Private Sub SetStatusText(ByVal sText As String)
Me.Text = sText
End Sub

Private Sub cboPort_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles cboPort.KeyDown
Try
If e.KeyCode = Keys.Up OrElse e.KeyCode = Keys.Down Then
m_fOkToUpdateAutoComplete = False
Else
m_fOkToUpdateAutoComplete = True
End If
Catch theException As Exception
' ...
End Try
End Sub

Private Sub cboPort_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cboPort.KeyUp
Try
If m_fOkToUpdateAutoComplete Then
With cboPort
If .Text.Length >= 2 Then
Dim cSuggestions As IList
Dim sError As String = ""

m_sLastSearchedFor = .Text

cSuggestions = GetSuggestions(m_sLastSearchedFor)
.DataSource = Nothing
If cSuggestions IsNot Nothing Then
.BindingContext = New BindingContext
.DisplayMember = "CName"
.ValueMember = "id"
.DataSource = New BindingSource(cSuggestions, Nothing)

System.Threading.Thread.Sleep(10)
System.Windows.Forms.Application.DoEvents()
.DroppedDown = True
.Text = m_sLastSearchedFor
If .Text.Length > 0 Then .SelectionStart = .Text.Length
End If
End If
End With
End If
Catch theException As Exception
' ...
End Try
End Sub

Private Function GetSuggestions(ByVal searchFor As String) As IList
BindingSource.Filter = "CName LIKE '%" & searchFor & "%'"
Return BindingSource.List
End Function

End Class

最佳答案

我们用非常大的数据集(全套药物信息)解决这个问题的方法是:

1) 处理组合的 TextChanged 事件

2) 在此事件中,从数据库中获取与用户当前输入相匹配的建议列表。我们利用数据库搜索的强大功能在字符串中的任何位置查找匹配项。

3)当检索到建议时,将它们绑定(bind)到组合框

4) 稍等片刻(500 毫秒)让 UI catch (我们使用 System.Threading.Thread.Sleep 和 System.Windows.Format.Application.DoEvents() 的组合)。

关于这种方法的几点说明:

1) 首次打开表单时没有任何内容绑定(bind)到列表

2)我们等到用户输入至少 4 个字符后才开始搜索,以减少对 DB 的命中并改善用户体验(例如,您不想显示 A 的所有匹配项)。

更新代码以显示完整的解决方案:

这里有一些额外的注释和代码来展示实际的过程。

ComboBox 的所有属性都应配置为默认值,但以下情况除外:

AutoCompleteMode = SuggestAppend
PreferredDropDownSize = 0, 0

这是我们用于特定情况(搜索前四个字符)的代码,其中包含用于检索和分配数据的占位符:
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 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 error
End Try
End Sub

Private Sub cboName_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cboName.TextChanged
Try
If m_fOkToUpdateAutoComplete Then
With 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 IEnumerable
Dim sError As String = ""

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

' And search for those
cSuggestions = GetSomeSuggestions(m_sLastSearchedFor) ' Your code here
.DataSource = Nothing
If cSuggestions IsNot Nothing Then
' Because this can use the same data source as the list, ensure that
' the bindingcontexts are different so that the lists are not tied to each other
.BindingContext = New BindingContext

.DataSource = cSuggestions

' Let the UI process the results
System.Threading.Thread.Sleep(10)
System.Windows.Forms.Application.DoEvents()
End If
End If
Else
If Not String.IsNullOrEmpty(m_sLastSearchedFor) Then
' Clear the last searched for text
m_sLastSearchedFor = ""
cboName.DataSource = Nothing
End If
End If
End With
End If
Catch theException As Exception
' Do something with the error
End Try
End Sub

关于vb.net - .NET 全文自动完成在组合框中。任何覆盖列表项的性能积极方式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7974533/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com