2010-07-02 88 views
1

我目前正在開發一個應用程序,它在帶有進度條的選項卡控件上有多個web瀏覽器。爲了節省我重複的代碼,我想創建一個方法,將進度條名稱傳遞給一個函數。我在下面創建了以下內容,但是我收到了此錯誤。進度條,c#

「字符串」不包含「最高」和沒有擴展方法「最高」接受類型「字符串」的第一個參數的定義可以找到(是否缺少using指令或程序集引用?)

private void PassPBName(string PBName) 
     { 

      // Event for the browser 
      AxSHDocVw.DWebBrowserEvents2_ProgressChangeEvent e; 

      /* The CurrentProgress variable from the raised event 
        * gives you the current number of bytes already downloaded 
        * while the MaximumProgress is the total number of bytes 
        * to be downloaded */ 
      if (e.progress < e.progressMax) 
      { 
       // Check if the current progress in the progress bar 
       // is >= to the maximum if yes reset it with the min 
       if (PBName.Value >= PBName.Maximum) 
        PBName.Value = PBName.Minimum; 
       else 
        // Just increase the progress bar 
        PBName.PerformStep(); 
      } 
      else 
       // When the document is fully downloaded 
       // reset the progress bar to the min (0) 
       PBName.Value = PBName.Minimum; 
     } 
     private void WBIntranet_ProgressChange(object sender, AxSHDocVw.DWebBrowserEvents2_ProgressChangeEvent e) 
     { 

      string progressBar = PBIntranet.Value.ToString(); 
      PassPBName(progressBar); 
     } 

感謝

回答

0

我找到了解決方案,如果有人想知道如何做到這一點。

private void PassPBName(ToolStripProgressBar PBName, AxSHDocVw.DWebBrowserEvents2_ProgressChangeEvent e) 
    { 
       /* The CurrentProgress variable from the raised event 
       * gives you the current number of bytes already downloaded 
       * while the MaximumProgress is the total number of bytes 
       * to be downloaded */ 
     if (e.progress < e.progressMax) 
     { 
      // Check if the current progress in the progress bar 
      // is >= to the maximum if yes reset it with the min 
      if (PBName.Value >= PBName.Maximum) 
       PBName.Value = PBName.Minimum; 
      else 
       // Just increase the progress bar 
       PBName.PerformStep(); 
     } 
     else 
      // When the document is fully downloaded 
      // reset the progress bar to the min (0) 
      PBName.Value = PBName.Minimum; 
    } 

    private void WBIntranet_ProgressChange(object sender, AxSHDocVw.DWebBrowserEvents2_ProgressChangeEvent e) 
    { 

     //Pass the PB bar name to PassPBName function to show current progress. 
     PassPBName (PBIntranet, e); 
    } 
1

如果您發送的進度的名字,你需要使用類似FindControl方法找到了控制。

private void PassPBReference(ProgressBar PBName) { 
    ... 
} 

,並使用叫它:

PassPBReference(PBIntranet); 

(你當然應該彌補方法一個更好的名字,這反映了,如果你發送一個參考進度控制,而不是它的更好它的確如此,而不僅僅是如何將參數傳遞給它。)

1

您有一個名爲PBName的字符串,但是您將它用作進度欄類。也許你打算通過這門課?假設PBIntranet是實際的progess bar類,看起來你應該把它傳遞給你的PassPBName函數。只是猜測,您還需要從WBIntranet_ProgressChange事件中通過e,而不是在PassPBName的本地聲明另一個,我認爲這不會按照您的意圖工作。

0

您不能通過在變量中使用名稱來引用對象。你將不得不通過Reflection來訪問它。類似這樣的:

Using System.Reflection; 

ProgressBar myProgress = (ProgressBar)this.GetType().GetField(PBName).GetValue(this); 

我對語法有點粗略,但也許這會幫助你進一步推動。一旦你有實際的對象,那麼你可以訪問Maxmimum/Minimum /等。