2015-04-22 70 views
-1

我做了一個閃屏形式:如何計算百分比並更新splashscreen窗體中的progressBar?

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 
using System.IO; 
using System.Threading; 

namespace GetHardwareInfo 
{ 
    public partial class SplashScreen : Form 
    { 
     public SplashScreen() 
     { 
      InitializeComponent(); 
     } 

     private Mutex mutex = new Mutex(); 
     public void SyncedClose() 
     { 
      mutex.WaitOne(); 
      this.Close(); 
      mutex.ReleaseMutex(); 
     } 
     public void UpdateProgressBar(int percentage) 
     { 

      if (this.InvokeRequired) 
      { 
       mutex.WaitOne(); 
       if (!IsDisposed) 
        this.BeginInvoke(new Action<int>(UpdateProgresPRV), percentage); 
       mutex.ReleaseMutex(); 
      } 
      else 
      { 
       UpdateProgresPRV(percentage); 
      } 
     } 

     private void UpdateProgresPRV(int per) 
     { 
      if (progressBar1.IsDisposed) return; 
      progressBar1.Value = per; 
     } 

     private void SplashScreen_Load(object sender, EventArgs e) 
     { 

     } 
    } 
} 

濺射屏幕的形式在設計一個背景圖像和圖像的progressBar1上。

然後在Form1我沒有在頂部:

List<string> WmiClassesKeys = new List<string>(); 
IEnumerable<Control> controls; 
string comboBoxesNames; 
SplashScreen splash = new SplashScreen(); 

然後在構造:

controls = LoopOverControls.GetAll(this, typeof(ComboBox)); 
      string[] lines = File.ReadAllLines(@"c:\wmiclasses\wmiclasses1.txt"); 

       foreach (string line in lines) 
       { 
        foreach (ComboBox comboBox in controls.OfType<ComboBox>()) 
        { 
         if (line.StartsWith("ComboBox")) 
         { 

          comboBoxesNames = line.Substring(14); 
         } 
         else 
         { 
          if (line.StartsWith("Classes")) 
          { 

           if (comboBox.Name == comboBoxesNames) 
           { 
            comboBox.Items.Add(line.Substring(14)); 

           } 

          } 
         } 
        } 
       } 

       foreach (ComboBox comboBox in controls.OfType<ComboBox>()) 
       { 
        comboBox.SelectedIndex = 0; 
       } 

方法GETALL是遍歷特定控件:

public static IEnumerable<Control> GetAll(Control control, Type type) 
     { 
      var controls = control.Controls.Cast<Control>(); 

      return controls.SelectMany(ctrl => GetAll(ctrl, type)) 
             .Concat(controls) 
             .Where(c => c.GetType() == type); 
     } 

當我正在運行該程序,這需要一些時間來使foreach循環,我需要顯示SplashScreen和約會progressBar直到foreach循環結束。

這是怎麼看起來像現在:

SplashScreen

+5

您正在發佈的方式太多的信息 - 坦率地說,我懷疑很多人會費心閱讀所有這些信息。我注意到的一件事是你有一個Thread.Sleep()調用。這絕不應該在WinForms程序的UI線程上使用。相反,您應該重寫程序的這一部分以使用WinForms Timer(而不是Thread Timer),並在Timer的Tick事件處理程序中完成需要完成的工作。 – RenniePet

回答

1

你可以指望你需要多少控件加載並投入Maximum屬性。 (當然,如果它不是一個固定值,你可以改變它使用例如progressBar.Maximum = x;代碼隱藏)

然後不斷刷新它,使用progressBar.PerformStep()(它會根據Step添加屬性值),或只是簡單地使用老progressBar.Value++

在你的情況我想這會是這樣的構造函數中:

ProgressBar progressBar = splash.Controls["myProgressBar"]; 

foreach (string line in lines) 
        { 
         foreach (ComboBox comboBox in controls.OfType<ComboBox>()) 
         { 
          if (line.StartsWith("ComboBox")) 
          { 

           comboBoxesNames = line.Substring(14); 
          } 
          else 
          { 
           if (line.StartsWith("Classes")) 
           { 

            if (comboBox.Name == comboBoxesNames) 
            { 
             comboBox.Items.Add(line.Substring(14)); 

            } 
           } 
          } 
          progressBar.PerformStep(); 
         } 
        } 

當你告訴它需要一些時間foreach循環,我想它的存在,你要添加的「進步」如果沒有,每次調用Combo時都需要刷新它,唯一的區別。