如何在.Net2.0中用后台工作者填充ListBox?

发布于 2024-10-18 16:21:02 字数 701 浏览 2 评论 0原文

在我的应用程序中,当用户将两个以上的字符放入文本框中后,列表框就会被填充。

如果列表框有很多内容需要填写,则用户必须等到列表框完全填满后才能继续输入。

现在我需要一个解决方案来填充ListBox而不阻塞UI,所以我想使用后台工作者来解决这个问题。

---- 原始版本 -----

在我的应用程序中,当用户在文本框中输入两个以上的字符时,我会填充一个列表框。 列表框填充结果,之后用户可以输入更多字符。

我的问题是以下行为:用户只能在整个列表框填满所有条目后放入下一个字符。如果有很多结果,用户必须等待才能继续。

我希望有以下行为:如果用户输入一个字符,则列表框的填充应该停止并为新输入构建列表。

我想到了某事。就像:

[PSEUDO-CODE]
loop over results {
    if (UserInput) {    // <-- GetKeyState()?
        break
    }

    AddOneItemToListBox
}
[/PSEUDO-CODE]

我想到了某事。就像 GetKeyState() 但我认为应该有一种更优雅的方式(也许是一种控件或另一种我不了解 ATM 的技术)。

PS:目前我正在寻找.Net2.0的解决方案,但如果.Net的更高版本中有一项技术,我也会很高兴得到一个简短的提示(仅供我参考)。

In my application a ListBox is filled after a user puts more than two characters into a textBox.

If there is a lot to fill into the ListBox, the user has to wait until the ListBox is completely filled before is able to continue his input.

Now I need a solution to fill the ListBox without blocking the UI, so I would like to use a background worker to solve this.

---- Original Version -----

In my application I fill a ListBox when the user puts in more then two characters into a textBox.
The ListBox fills with results and after this, the user is able to put in more characters.

My problem is the following behaviour: The user is only able to put in the next char after the whole ListBox is filled with all entries. If there are a lot of results the user has to wait until he is able to continue.

I would like to have the following behaviour: If the user puts in a char, the filling of the ListBox should stop and build the list for the new input.

I thought about sth. like:

[PSEUDO-CODE]
loop over results {
    if (UserInput) {    // <-- GetKeyState()?
        break
    }

    AddOneItemToListBox
}
[/PSEUDO-CODE]

I thought about sth. like GetKeyState() but I think there should be a more elegant way (perhaps a control or another technology I don't know about ATM).

P.S.: Currently I'm looking for a solution with .Net2.0, but if there is a technology inside a later version of .Net I would also be glad for a short hint (just for my information).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蔚蓝源自深海 2024-10-25 16:21:02

您可以填充 BackgroundWorker 中的 ListBox(不要忘记使用 BeginInvoke() 调用 Add() 方法)。在这种情况下,当框被填充时,用户已经可以与 GUI 交互。

如果用户在自己的框中添加了一些内容,您可以通过调用worker.CancelAsnyc()来停止自动填充。

更新(与 VS 2005 兼容)

不幸的是我没有 VS2005 来尝试,但我删除了所有花哨的 lambda 等东西。

只是一个简单的开始示例:

public partial class FormMain : Form
{
    BackgroundWorker _Worker;

    public FormMain()
    {
        InitializeComponent();

        _Worker = new BackgroundWorker();

        _Worker.DoWork += OnWorkerDoWork;
        _Worker.WorkerSupportsCancellation = true;

        this.Shown += OnFormMainShown;
    }

    void OnFormMainShown(object sender, EventArgs e)
    {
        _Worker.RunWorkerAsync();
    }

    void OnWorkerDoWork(object sender, DoWorkEventArgs e)
    {
        for (int i = 0; i < 100; i++)
        {
            if (_Worker.CancellationPending)
            {
                return;
            }

            Thread.Sleep(100);
            listBox1.Invoke((Action<int>)AddItem, i);
        }
    }

    private void AddItem(int i)
    {
        listBox1.Items.Add(i);
    }

    private void button1_Click(object sender, EventArgs e)
    {
        _Worker.CancelAsync();
    }
}

You can fill the the ListBox within a BackgroundWorker (don't forget to call the Add() method with BeginInvoke()). In this case the user can already interact with the GUI while the box will be filled.

If the user adds something to the box on himself you can stop the autofill by calling worker.CancelAsnyc().

Update (compatible with VS 2005)

Unfortunately i don't have VS2005 to try it out, but i removed all the fancy lambda, etc. stuff.

Just a simple sample for starting:

public partial class FormMain : Form
{
    BackgroundWorker _Worker;

    public FormMain()
    {
        InitializeComponent();

        _Worker = new BackgroundWorker();

        _Worker.DoWork += OnWorkerDoWork;
        _Worker.WorkerSupportsCancellation = true;

        this.Shown += OnFormMainShown;
    }

    void OnFormMainShown(object sender, EventArgs e)
    {
        _Worker.RunWorkerAsync();
    }

    void OnWorkerDoWork(object sender, DoWorkEventArgs e)
    {
        for (int i = 0; i < 100; i++)
        {
            if (_Worker.CancellationPending)
            {
                return;
            }

            Thread.Sleep(100);
            listBox1.Invoke((Action<int>)AddItem, i);
        }
    }

    private void AddItem(int i)
    {
        listBox1.Items.Add(i);
    }

    private void button1_Click(object sender, EventArgs e)
    {
        _Worker.CancelAsync();
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文