进度条,c#

时间:2010-07-02 14:59:38

标签: c# progress-bar

我目前正在开发一个应用程序,它在选项卡控件上有多个Web浏览器,上面有进度条。为了节省我重复的代码,我想创建一个方法,我将进度条名称传递给一个函数。我在下面创建了以下内容,但是我收到了这个错误。

'string'不包含'Maximum'的定义,也没有扩展方法'Maximum'接受类型'string'的第一个参数(你是否缺少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);
        }

由于

4 个答案:

答案 0 :(得分:1)

如果发送进度条的名称,则需要使用FindControl方法之类的内容来查找控件。如果您改为发送对progressbar控件的引用,那就更好了:

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

并使用以下方式调用它:

PassPBReference(PBIntranet);

(你当然应该为这个方法做一个更好的名称,它反映了它的作用,而不仅仅是你如何将参数传递给它。)

答案 1 :(得分:1)

您有一个名为PBName的字符串,但您正在使用它,就好像它是一个进度条类。也许你打算通过课程?假设PBIntranet是实际的progess bar类,看起来你应该将它传递到PassPBName函数中。只是猜测,你还需要从你的e事件中传递WBIntranet_ProgressChange,而不是在PassPBName本地声明另一个,我认为这不会按你的意愿运行。

答案 2 :(得分:0)

您不能通过在变量中使用它的名称来引用对象。你必须通过Reflection访问它。像这样:

Using System.Reflection;

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

我对语法有点粗略,但也许这会对你有所帮助。获得实际对象后,即可访问Maxmimum / Minimum / etc.

答案 3 :(得分: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);
    }