更新 ToolStripProgressBar 和 ToolStripStatusLabel 以及操作

发布于 2024-09-05 15:34:51 字数 672 浏览 9 评论 0原文

在 Windows 窗体中,我有一个搜索框,它会触发一个事件来搜索远程数据库并显示一些结果。查询速度非常快,通常只需几分之一秒,但如果延迟很明显,表单的状态栏中会有一个进度条和标签。当用户单击“搜索”时,应出现状态标签,并且进度条显示一些进度。然后,当结果返回时,标签应该消失并且进度条应该已满。非常基本的回应。

问题是,我无法让这些操作按顺序发生。使用下面的代码,我单击“搜索”,直到显示结果为止没有任何反应,然后进度条从 0 填满到 100。标签永远不会出现。我什至在事件发生后立即添加了一个睡眠命令,以确保我不仅仅是错过了它,而且就好像前两条语句没有被执行一样。我在这里做错了什么?

 private void searchButton_Click(object sender, EventArgs e)
    {
        toolStripStatusLabel1.Visible = true;
        toolStripProgressBar1.Value = 20;
        m_changeRequestedEvents.Fire<String>("SearchTerm", searchTextBox.Text);
        toolStripProgressBar1.Value = 100;
        toolStripStatusLabel1.Visible = false;
    }

In a Windows Form, I have a search box that fires an event to search a remote database and display some results. The query is pretty fast, usually just a fraction of a second, but in case the delay is noticeable there is a progress bar and label in the form's status bar. When the user clicks "Search" the status label should appear and the progress bar show some progress. Then when the result comes back the label should disappear and the progress bar should be full. Pretty basic response.

The problem is, I can't get those actions to happen in that order. Using the code below, I click "Search", nothing happens until the results are displayed, and then the progress bar fills up from 0 to 100. The label never appears. I even threw in a sleep command immediately after the event to be sure I wasn't just missing it, but it's as if the first 2 statements are not being executed. What am I doing wrong here?

 private void searchButton_Click(object sender, EventArgs e)
    {
        toolStripStatusLabel1.Visible = true;
        toolStripProgressBar1.Value = 20;
        m_changeRequestedEvents.Fire<String>("SearchTerm", searchTextBox.Text);
        toolStripProgressBar1.Value = 100;
        toolStripStatusLabel1.Visible = false;
    }

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

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

发布评论

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

评论(1

橙味迷妹 2024-09-12 15:34:52

提供的代码更改了 UI 属性,但线程在 searchButton_Click 返回之前无法重新绘制 UI。因此,在事件之前所做的更改永远不会应用,因为它们会被事件之后所做的更改覆盖,然后在方法返回时应用这些更改。

相反,在触发事件之前更新 UI 属性:

searchButton.Enabled = false;
toolStripProgressBar1.Value = 0;
toolStripStatusLabel1.Visible = true;
m_changeRequestedEvents.Fire<String>("SearchTerm", searchTextBox.Text);

并在事件处理程序中,在单独的线程 (BackgroundWorker) 中运行查询,以便 UI 可以同时更新:

private void View_OnSearchTermChangeRequest(Object sender, PropertyChangeRequestEventArgs<String> args)
{
    m_search_bgw = new BackgroundWorker();
    ...
    m_DBHandler.current_worker = m_search_bgw;
    m_search_bgw.RunWorkerAsync(args.RequestedValue);
}

然后在调用的方法中再次更新 UI BackgroundWorker 线程完成:

void UpdateView(DataView projects)
{
    dataGridView1.DataSource = projects;
    ...
    toolStripProgressBar1.Value = 100;
    toolStripStatusLabel1.Visible = false;
}

The code provided changes the UI attributes, but the thread can't repaint the UI until after searchButton_Click returns. So the changes made before the event are never applied, because they're overridden by the changes made after, which are then applied when the method returns.

Instead, update the UI attributes before firing the event:

searchButton.Enabled = false;
toolStripProgressBar1.Value = 0;
toolStripStatusLabel1.Visible = true;
m_changeRequestedEvents.Fire<String>("SearchTerm", searchTextBox.Text);

and from the event handler, run the query in a separate thread (BackgroundWorker), so that the UI can update in the meantime:

private void View_OnSearchTermChangeRequest(Object sender, PropertyChangeRequestEventArgs<String> args)
{
    m_search_bgw = new BackgroundWorker();
    ...
    m_DBHandler.current_worker = m_search_bgw;
    m_search_bgw.RunWorkerAsync(args.RequestedValue);
}

then update the UI again in the method that is called when the BackgroundWorker thread completes:

void UpdateView(DataView projects)
{
    dataGridView1.DataSource = projects;
    ...
    toolStripProgressBar1.Value = 100;
    toolStripStatusLabel1.Visible = false;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文