gpt4 book ai didi

.net - 如何异步获取文本框的自动完成数据?

转载 作者:行者123 更新时间:2023-12-02 00:33:36 24 4
gpt4 key购买 nike

我们的WinForms应用程序会延迟加载数据以自动完成文本框。其伪代码如下;

  1. 用户在文本框中输入内容
  2. 在输入暂停时,确定我们是否需要获取自动完成数据
  3. 在工作线程中,联系服务器并获取数据
  4. 调用回 UI 线程
  5. 设置textBox.AutoCompleteCustomSource = fetchedAutoCompleteStringCollection;
  6. 强制文本框下拉其自动完成下拉列表。

我目前遇到了#6 的问题。作为黑客,我执行以下操作来模拟有效的按键,但并非在所有情况下都有效。

     // This is a hack, but the only way that I have found to get the autocomplete
// to drop down once the data is returned.
textBox.SelectionStart = textBox.Text.Length;
textBox.SelectionLength = 0;
SendKeys.Send( " {BACKSPACE}" );

一定有更好的方法。我不敢相信我是唯一一个异步获取自动完成数据的人。我应该怎么做?

编辑: 用于导致自动完成下拉列表的 Win32 调用是可以接受的。如果有必要,我不介意 PInvoking。

最佳答案

我仅使用托管代码为 TextBox 编写了一个异步自动完成类。希望对您有所帮助。

using System;
using System.Windows.Forms;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;

namespace TextboxAutocomplete
{
public abstract class AutoCompleteSource
{
private TextBox mTextBox;
private AutoCompleteMode mAutoCompleteMode;

public AutoCompleteSource(TextBox textbox) :
this(textbox, AutoCompleteMode.Suggest) { }

public AutoCompleteSource(TextBox textbox, AutoCompleteMode mode)
{
if (textbox == null)
throw new ArgumentNullException("textbox");

if (textbox.IsDisposed)
throw new ArgumentException("textbox");

mTextBox = textbox;
mAutoCompleteMode = mode;

mTextBox.AutoCompleteSource = System.Windows.Forms.AutoCompleteSource.None;

BackgroundWorker autoCompleteLoader = new BackgroundWorker();
autoCompleteLoader.DoWork += new DoWorkEventHandler(autoCompleteLoader_DoWork);
autoCompleteLoader.RunWorkerCompleted += new RunWorkerCompletedEventHandler(autoCompleteLoader_RunWorkerCompleted);
autoCompleteLoader.RunWorkerAsync();
}

void autoCompleteLoader_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
AutoCompleteStringCollection collection = e.Result as AutoCompleteStringCollection;
if (collection == null) return;

if (mTextBox.InvokeRequired)
{
mTextBox.Invoke(new SetAutocompleteSource(DoSetAutoCompleteSource), new object[] { collection });
}
else
{
DoSetAutoCompleteSource(collection);
}
}

protected void DoSetAutoCompleteSource(AutoCompleteStringCollection collection)
{
if (mTextBox.IsDisposed) return;

mTextBox.AutoCompleteMode = mAutoCompleteMode;
mTextBox.AutoCompleteSource = System.Windows.Forms.AutoCompleteSource.CustomSource;
mTextBox.AutoCompleteCustomSource = collection;
}

void autoCompleteLoader_DoWork(object sender, DoWorkEventArgs e)
{
List<string> autoCompleteItems = GetAutocompleteItems();
if (autoCompleteItems == null) return;
AutoCompleteStringCollection collection = new AutoCompleteStringCollection();
collection.AddRange(GetAutocompleteItems().ToArray());
e.Result = collection;
}

protected abstract List<string> GetAutocompleteItems();
}

internal delegate void SetAutocompleteSource(AutoCompleteStringCollection collection);
}

示例实现:

using System;
using System.Windows.Forms;
using System.Collections.Generic;
using System.Text;

namespace TextboxAutocomplete
{
class MockAutoCompleteSource : AutoCompleteSource
{
public MockAutoCompleteSource(TextBox textbox)
: base(textbox)
{

}

protected override List<string> GetAutocompleteItems()
{
List<string> result = new List<string>();
for (int i = 0; i < 2500; i++)
{
result.Add(Guid.NewGuid().ToString());
}

return result;
}
}
}

如何使用:

 ...
TextBox myTextbox = new TextBox();
MockAutoCompleteSource autoComplete =
new MockAutoCompleteSource(myTextbox);
...

关于.net - 如何异步获取文本框的自动完成数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/440277/

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