为什么我的进度条百分比会消失.. c# [英] Why does my Progressbar percentage disappear .. c#

查看:94
本文介绍了为什么我的进度条百分比会消失.. c#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



当进度条更新为什么时,我的进度条perecentage消失了?



代码:



  public   void  PBPercentage()
{

int percent =( int )((< span class =code-keyword> double )(progressBar1.Value - progressBar1.Minimum)/( double )(progressBar1.Maximum - progressBar1.Minimum ))* 100 );
使用(Graphics gr = progressBar1.CreateGraphics())
{
gr.DrawString(percent.ToString()+ ,SystemFonts.DefaultFont,Brushes.Black,
new PointF(progressBar1.Width / 2 - (gr.MeasureString(percent.ToString()+
SystemFonts.DefaultFont).Width / 2 .0F ),
progressBar1.Height / 2 - (gr.MeasureString(percent.ToString()+
SystemFonts.DefaultFont).Height / 2 .0F))) ;


}
}

解决方案

因为图形是易变的。您不应该使用您创建的 System.Drawing.Graphics 的实例进行绘制。相反,您应该在重写的方法 System.Windows.Forms.Control.OnDraw 或事件的处理程序 System.Windows.Forms中执行此操作。 Control.Draw 并使用event arguments参数中传递的 Graphics 实例。请查看我过去的答案:

什么样的俏皮方法是Paint? (DataGridViewImageCell.Paint(...)) [ ^ ],

在面板上捕获绘图 [ ^ ],

mdi子表单之间的绘制线 [ ^ ],

如何加快我的vb.net应用程序? [ ^ ]。



-SA

Hi
My progressbar perecentage disappears when the progressbar updates why is this?

CODE:

public void PBPercentage()
      {

          int percent = (int)(((double)(progressBar1.Value - progressBar1.Minimum) / (double)(progressBar1.Maximum - progressBar1.Minimum)) * 100);
          using (Graphics gr = progressBar1.CreateGraphics())
          {
              gr.DrawString(percent.ToString() + "%", SystemFonts.DefaultFont, Brushes.Black,
              new PointF(progressBar1.Width / 2 - (gr.MeasureString(percent.ToString() + "%",
              SystemFonts.DefaultFont).Width / 2.0F),
              progressBar1.Height / 2 - (gr.MeasureString(percent.ToString() + "%",
               SystemFonts.DefaultFont).Height / 2.0F)));


          }
      }

解决方案

Because you graphics is volatile. You should not draw using the instance of System.Drawing.Graphics you create. Instead, you should do it in an overridden method System.Windows.Forms.Control.OnDraw or your handler of the event System.Windows.Forms.Control.Draw and use the Graphics instance passed in the event arguments parameter. Please see my past answers:
What kind of playful method is Paint? (DataGridViewImageCell.Paint(...))[^],
capture the drawing on a panel[^],
Drawing Lines between mdi child forms[^],
How to speed up my vb.net application?[^].

—SA


这篇关于为什么我的进度条百分比会消失.. c#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆