gpt4 book ai didi

winforms - 使用 ProgressBar 和 ComboBox

转载 作者:行者123 更新时间:2023-12-02 03:51:50 25 4
gpt4 key购买 nike

我遇到了 Marquee 的麻烦ProgressBar .我需要执行一个方法( refreshList() )来获得 List<string> .然后我分配这个 ListComboBox , 所以 ComboBox用新的 Items 刷新.作为refreshList()花 3 或 4 秒,我想运行 Marquee ProgressBar .但我做不到。 ProgressBar没关系,但是ComboBox不加载新的 Items .

我的 refreshList()方法:

private void refreshList(List<string> list)
{
albumList.DataSource = null;
albumList.DataSource = list;
}

我有以下代码,它工作正常:

private void changeDirectoryToolStripMenuItem_Click(object sender, EventArgs e)
{
fbd.RootFolder = Environment.SpecialFolder.MyComputer;
folderPath = "";
if (fbd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
folderPath = fbd.SelectedPath;
refreshList(N.getList(folderPath));

}
}

但是我加了一个ProgressBar并写下这段代码:

private void changeDirectoryToolStripMenuItem_Click(object sender, EventArgs e)
{
fbd.RootFolder = Environment.SpecialFolder.MyComputer;
folderPath = "";
if (fbd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
folderPath = fbd.SelectedPath;
bgWorker.WorkerReportsProgress = true;
bgWorker.RunWorkerAsync();

}
}

然后我放置了refreshList()doWork()方法:

private void bgWorker_DoWork(object sender, DoWorkEventArgs e)
{
refreshList(N.getList(folderPath));
}

但不幸的是,这是行不通的。谁能帮我解决这个问题?提前致谢。

最佳答案

您可以使用 MarqueeAnimationSpeedValue ProgressBar 控件的属性来停止和启动选取框。无需使用 WorkerReportsProgress * 因为您没有增加正常的进度条 - 您只想“旋转”选取框。

您可以执行以下操作:

    public Form1()
{
InitializeComponent();
//Stop the progress bar to begin with
progressBar1.MarqueeAnimationSpeed = 0;
//If you wire up the event handler in the Designer, then you don't need
//the following line of code (the designer adds it to InitializeComponent)
//backgroundWorker1.RunWorkerCompleted += backgroundWorker1_RunWorkerCompleted;
}

private void changeDirectoryToolStripMenuItem_Click(object sender, EventArgs e)
{
fbd.RootFolder = Environment.SpecialFolder.MyComputer;
folderPath = "";
if (fbd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
folderPath = fbd.SelectedPath;
//This line effectively starts the progress bar
progressBar1.MarqueeAnimationSpeed = 10;
bgWorker.RunWorkerAsync(); //Calls the DoWork event

}
}

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
e.Result = N.getList(folderPath); //Technically this is the only work you need to do in the background
}

private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
//these two lines effectively stop the progress bar
progressBar1.Value = 0;
progressBar1.MarqueeAnimationSpeed = 0;
//Now update the list with the result from the work done on the background thread
RefreshList(e.Result as List<String>);
}


private void RefreshList(List<String> results)
{
albumList.DataSource = null; //You don't need this line but there is no real harm.
albumList.DataSource = list;
}

请记住通过设计器中事件部分的属性栏将 RunWorkerCompleted 事件连接到 backgroundWorker1_RunWorkerCompleted。

首先,作为您成功选择文件夹的一部分,我们通过将 MarqueeAnimationSpeed 属性设置为非零正数来启动 ProgressBar 的动画。

然后,在调用 RunWorkerAsync 之后,代码在 DoWork 方法中构建您的列表,然后将结果分配给 DoWorkEventArgs,后者传递给 RunWorkerCompleted 事件(在 DoWork 完成时触发)。

backgroundWorker1_RunWorkerCompleted 方法中,我们停止进度条(并将其值设置为零以有效地将其返回到原始状态),然后我们将列表传递给 refreshList 方法以对其进行数据绑定(bind)并填充组合框。

使用 VS2012、Windows 窗体、.Net 4.0 进行测试(使用 Thread.Sleep 来模拟 N.getList 所花费的时间)

*WorkerReportsProgress 和关联的 ReportProgress 方法/事件在您想要增加进度条时使用 - 您可以告诉 GUI 您已完成 10%、20%、50% 等等。

关于winforms - 使用 ProgressBar 和 ComboBox,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14110863/

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