2013-01-01 71 views
3

我遇到了MarqueeProgressBar問題。我需要執行一個方法(refreshList())以獲得List<string>。然後我將這List分配到ComboBox,所以ComboBox刷新與新的Items。由於refreshList()需要3或4秒,我想運行一個MarqueeProgressBar。但我不能。 ProgressBar是好的,但是ComboBox不會加載新的Items使用ProgressBar和組合框

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)); 
} 

但不幸的是,這是行不通的。任何人都可以幫我解決這個問題嗎?提前致謝。

+1

我可以總結一下你的問題的TL版本嗎?設置數據源'albumList.DataSource = list;'需要3-4秒,如何在分配時顯示選取框進度條?這是對的嗎? – Neolisk

+0

您確定BackgroundWorker類已連接到DoWork事件(即您的代碼中是否有'bgWorker.DoWork + = bgWorker_DoWork;')。我也看不到你在哪裏停止並啓動'ProgressBar' – dash

+0

在我看來,這段代碼無法編譯?在'bgWorker_DoWork'中'folderPath'是什麼。 –

回答

1

您可以使用ProgressBar控件的MarqueeAnimationSpeedValue屬性來停止和啓動選取框。沒有必要使用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,DoWorkEventArgs將傳遞給RunWorkerCompleted事件(DoWork完成時觸發)。

backgroundWorker1_RunWorkerCompleted方法中,我們停止進度條(並將其值設置爲零以有效地將其返回到原始狀態),然後我們將該列表傳遞給refreshList方法以對其進行數據綁定並填充組合框。

使用VS2012,Windows窗體,.NET 4.0(用了Thread.Sleep效仿採取N.getList時間)

* WorkerReportsProgress,以及相關的ReportProgress方法/事件進行測試時,要使用增加進度條 - 你可以告訴GUI你完成10%,完成20%,完成50%等等。

+0

您的負擔得起。非常感謝。 –