2011-03-05 49 views
1

我在winform中使用windows進度條。它的最小值是20,最大值是120.我必須在進度條中設置value = 60的閾值,這樣如果值保持低於60,那麼進度條顏色將是紅色,如果值大於60,則進度條顏色應該是綠色。 最重要的是,我想要將此閾值顯示爲應該可見的任何可能是進度條綠色或紅色顏色的progessbar中的一行。 有沒有人有想法?您的幫助將不勝感激。如何在windows進度條控件中設置閾值?

+1

你的意思是財產以後像我們在我的電腦資源管理器中查看驅動器卷的 – 2011-03-05 09:36:12

+0

可能重複[如何改變進度條的顏色在C#.NET 3.5?](http://stackoverflow.com/questions/778678/how-to-change-the-color-of-progressbar-in-c-net-3-5) – 2011-03-05 09:46:31

回答

1

你可以試試這個(但是你不會得到動畫)。

它是添加了閾值功能的this answer的修改版本。

public class NewProgressBar : ProgressBar 
{ 
    //This property takes the Threshold. 
    public int Threshold{ get; set; } 

    public NewProgressBar() 
    { 
     this.SetStyle(ControlStyles.UserPaint, true); 
    } 

    protected override void OnPaint(PaintEventArgs e) 
    { 
     Rectangle rec = e.ClipRectangle; 

     rec.Width = (int)(rec.Width * ((double)Value/Maximum)) - 4; 
     if(ProgressBarRenderer.IsSupported) 
      ProgressBarRenderer.DrawHorizontalBar(e.Graphics, e.ClipRectangle); 
     rec.Height = rec.Height - 4; 
      //Check value is greater that Threshold 
      if(this.Value > Threshold) 
      { 
       e.Graphics.FillRectangle(Brushes.Green, 2, 2, rec.Width, rec.Height); 
      } 
      else 
      { 
       e.Graphics.FillRectangle(Brushes.Red, 2, 2, rec.Width, rec.Height); 
      } 
      //This line should do that 
      e.Graphics.DrawLine(Pens.Black,Threshold-1,2,Threshold-1,rec.Height); 
    } 
} 

現在你可以使用這樣的:

NewProgressBar p = new NewProgressBar(); 
//Set properties 
//Set threshold 
p.Threshold = 60;  
+0

謝謝...爲你的努力,但我需要的是在門檻上它也顯示了值60的行,應該是可見的任何b e進度條的顏色..紅色或綠色... – 2011-03-05 10:19:40

+0

@Sachin Patil:更新我的代碼:) – 2011-03-05 10:35:20

+0

@ Shekhar_Pro:不工作..在DrawLine方法中x和y協調值的一些問題..仍然感謝。 。 – 2011-03-05 11:08:57

0

使用默認的ProgressBar實現,你將無法完全實現你想要的。你需要創建一個自定義控件。

如果您只想更改顏色,可以通過在百分比值更改時更改ForeColor屬性來實現此目的。

+0

直到你有視覺效果才能工作啓用.. – 2011-03-05 09:47:16

+0

是的,我知道... – 2011-03-05 09:51:12