Lm th? no ? t?o thanh tien tr?nh m?n trong Visual C#

D?ch tiu ? D?ch tiu ?
ID c?a bi: 323116 - Xem s?n ph?m m bi ny p d?ng vo.
?i v?i m?t Microsoft Visual Basic.NET Phin b?n c?a bi vi?t ny, xem 323088.
Bung t?t c? | Thu g?n t?t c?

? Trang ny

TM T?T

Bi vi?t ny ch?ng t? lm th? no ? t?o m?t n gi?n, tu? ch?nh UserControl ? t?o ra m?t m?n, di chuy?n ProgressBar ki?m sot.

Trong phin b?n tr?c c?a cc ProgressBar ki?m sot, ch?ng h?n nh cc phin b?n cung c?p v?i Microsoft Windows ph? bi?n i?u khi?n ActiveX control, b?n c th? xem s? ti?n b? trong hai quan i?m khc nhau. ? ki?m sot nh?ng quan i?m, b?n s? d?ng cc Di chuy?n b?t ?ng s?n, bao g?m cc ci ?t tiu chu?n v m?n. Uy?n s?n xu?t m?t kh?i r?n mu s?c ?i di?n cho s? ti?n b?, v di chuy?n tiu chu?n xu?t hi?n phn o?n v bao g?m m?t lo?t cc nh? kh?i ho?c h?nh ch? nh?t.

Cc ProgressBar i?u khi?n ?c bao g?m v?i Microsoft Visual C# h? tr? ch? l cc thi?t l?p tiu chu?n.

M?u m? trong bi vi?t ny minh ho? cch ? t?o ra m?t i?u khi?n h? tr? cc tnh ch?t sau:
  • T?i thi?u. ?c tnh ny l?y ?c ho?c ?t gi tr? th?p hn cho ph?m vi gi tr? h?p l? cho ti?n b?. Gi tr? m?c ?nh c?a ti s?n ny l zero (0); b?n khng th? ?t thu?c tnh ?n m?t gi tr? tiu c?c.
  • T?i a. ?c tnh ny l?y ?c ho?c ?t gi tr? mid cho ph?m vi gi tr? h?p l? cho ti?n b?. Gi tr? m?c ?nh c?a ti s?n ny l 100.
  • Gia tri. ?c tnh ny l?y ?c ho?c b? c?p ? hi?n t?i c?a s? ti?n b?. Gi tr? ph?i n?m trong ph?m vi m cc T?i thi?u v cc T?i a thu?c tnh xc ?nh.
  • ProgressBarColor. ?c tnh ny l?y ?c ho?c ?t mu s?c c?a thanh tien tr?nh.

T?o m?t tu? ch?nh ProgressBar control

  1. Th?c hi?n theo cc b?c sau ? t?o ra m?t d? n m?i th vi?n i?u khi?n c?a Windows trong Visual C#:
    1. B?t ?u Microsoft Visual Studio.
    2. Trn cc Tp tr?nh n, i?m ?n M?i, sau b?m D an.
    3. Trong cc D? n m?i h?p tho?i h?p, b?m vo Visual C# d?i Cc lo?i d? n, sau b?m Th vi?n i?u khi?n c?a Windows Forms d?i Khun mu.

      Chu y Trong ph?ng thu tr?c quan.NET 2003, h?y nh?p vo Visual C# cc d? n Thay v? Visual C#.
    4. Trong cc Tn h?p, lo?i SmoothProgressBar, sau b?m Ok.
    5. Trong d? n Explorer, ?i tn m?c ?nh l?p module t? UserControl1.cs ? SmoothProgressBar.cs.
    6. Trong c?a s? thu?c tnh cho cc UserControl ?i t?ng, thay ?i cc Tn b?t ?ng s?n t? UserControl1 ? SmoothProgressBar.
  2. T?i th?i i?m ny, b?n th?ng k? th?a kha ki?m sot v sau thm cc ch?c nng b? sung ? m? r?ng m?t i?u khi?n hi?n c. Tuy nhin, cc ProgressBar l?p l nim phong v khng th? ?c th?a h?ng. V? v?y, b?n ph?i xy d?ng s? ki?m sot t? ?u.

    Thm m? sau vo t?p tin SmoothProgressBar.cs trong l?p c ngu?n g?c t? UserControl.

    Chu y Trong ph?ng thu tr?c quan.NET 2003, thm m? ? module l?p l?p UserControl b?t ngu?n, ch? sau khi ph?n "Windows m?u thi?t k? t?o ra m?".
    int min = 0;	// Minimum value for progress range
    int max = 100;	// Maximum value for progress range
    int val = 0;		// Current progress
    Color BarColor = Color.Blue;		// Color of progress meter
    
    protected override void OnResize(EventArgs e)
    {
    	// Invalidate the control to get a repaint.
    	this.Invalidate();
    }
    
    protected override void OnPaint(PaintEventArgs e)
    {
    	Graphics g = e.Graphics;
    	SolidBrush brush = new SolidBrush(BarColor);
    	float percent = (float)(val - min) / (float)(max - min);
    	Rectangle rect = this.ClientRectangle;
    
    	// Calculate area for drawing the progress.
    	rect.Width = (int)((float)rect.Width * percent);
    
    	// Draw the progress meter.
    	g.FillRectangle(brush, rect);
    
    	// Draw a three-dimensional border around the control.
    	Draw3DBorder(g);
    
    	// Clean up.
    	brush.Dispose();
    	g.Dispose();		
    }
    
    public int Minimum
    {
    	get
    	{
    		return min;
    	}
    
    	set
    	{
    		// Prevent a negative value.
    		if (value < 0)
    		{
    			min = 0;
    		}
    		
    		// Make sure that the minimum value is never set higher than the maximum value.
    		if (value > max)
    		{
    			min = value;
    			min = value;
    		}
    		
    		// Ensure value is still in range
    		if (val < min)
    		{
    			val = min;
    		}
    
    		// Invalidate the control to get a repaint.
    		this.Invalidate();
    	}
    }
    
    public int Maximum
    {
    	get
    	{
    		return max;
    	}
    
    	set
    	{
    		// Make sure that the maximum value is never set lower than the minimum value.
    		if (value < min)
    		{
    			min = value;
    		}
    
    		max = value;
    
    		// Make sure that value is still in range.
    		if (val > max)
    		{
    			val = max;
    		}
    
    		// Invalidate the control to get a repaint.
    		this.Invalidate();
    	}
    }
    
    public int Value
    {
    	get
    	{
    		return val;
    	}
    
    	set
    	{
    		int oldValue = val;
    
    		// Make sure that the value does not stray outside the valid range.
    		if (value < min)
    		{
    			val = min;
    		}
    		else if (value > max)
    		{
    			val = max;
    		}
    		else
    		{
    			val = value;
    		}
    
    		// Invalidate only the changed area.
    		float percent;
    
    		Rectangle newValueRect = this.ClientRectangle;
    		Rectangle oldValueRect = this.ClientRectangle;
    
    		// Use a new value to calculate the rectangle for progress.
    		percent = (float)(val - min) / (float)(max - min);
    		newValueRect.Width = (int)((float)newValueRect.Width * percent);
    
    		// Use an old value to calculate the rectangle for progress.
    		percent = (float)(oldValue - min) / (float)(max - min);
    		oldValueRect.Width = (int)((float)oldValueRect.Width * percent);
    
    		Rectangle updateRect = new Rectangle();
    		
    		// Find only the part of the screen that must be updated.
    		if (newValueRect.Width > oldValueRect.Width)
    		{
    			updateRect.X = oldValueRect.Size.Width;
    			updateRect.Width = newValueRect.Width - oldValueRect.Width;
    		}
    		else
    		{
    			updateRect.X = newValueRect.Size.Width;
    			updateRect.Width = oldValueRect.Width - newValueRect.Width;
    		}
    
    		updateRect.Height = this.Height;
    
    		// Invalidate the intersection region only.
    		this.Invalidate(updateRect);
    	}
    }
    
    public Color ProgressBarColor
    {
    	get
    	{
    		return BarColor;
    	}
    
    	set
    	{
    		BarColor = value;
    
    		// Invalidate the control to get a repaint.
    		this.Invalidate();
    	}
    }
    
    private void Draw3DBorder(Graphics g)
    {
    	int PenWidth = (int)Pens.White.Width;
    
    	g.DrawLine(Pens.DarkGray, 
    		new Point(this.ClientRectangle.Left, this.ClientRectangle.Top),
    		new Point(this.ClientRectangle.Width - PenWidth, this.ClientRectangle.Top));
    	g.DrawLine(Pens.DarkGray,
    		new Point(this.ClientRectangle.Left, this.ClientRectangle.Top), 
    		new Point(this.ClientRectangle.Left, this.ClientRectangle.Height - PenWidth));
    	g.DrawLine(Pens.White,
    		new Point(this.ClientRectangle.Left, this.ClientRectangle.Height - PenWidth), 
    		new Point(this.ClientRectangle.Width - PenWidth, this.ClientRectangle.Height - PenWidth));
    	g.DrawLine(Pens.White,
    		new Point(this.ClientRectangle.Width - PenWidth, this.ClientRectangle.Top), 
    		new Point(this.ClientRectangle.Width - PenWidth, this.ClientRectangle.Height - PenWidth));
    } 
    					
  3. Trn cc Xy d?ng tr?nh n, nh?p vo Xy d?ng gi?i php ? bin d?ch cc d? n.

T?o m?t ?ng d?ng khch hng m?u

  1. Trn cc Tp tr?nh n, i?m ?n M?i, sau b?m D an.
  2. Trong cc Thm d? n m?i h?p tho?i h?p, b?m vo Visual C# d?i Cc lo?i d? n, b?m ?ng d?ng Windows Forms d?i Khun mu, sau b?m Ok.

    Chu y Trong ph?ng thu tr?c quan.NET 2003, h?y nh?p vo Visual C# cc d? n Thay v? Visual C#.
  3. Lm theo cc b?c sau ? thm hai th? hi?n c?a cc SmoothProgressBar ki?m sot ? cc h?nh th?c:
    1. Trn cc Cng cu tr?nh n, nh?p vo Ch?n kho?n m?c h?p cng c?.

      Chu y Trong ph?ng thu tr?c quan.NET 2003, h?y nh?p vo Tu? ch?nh h?p cng c? Thay v? Ch?n kho?n m?c h?p cng c?.
    2. B?m vo cc .Cc thnh ph?n NET Framework tab.
    3. Nh?p vo Tr?nh duy?tv sau xc ?nh v? tr cc t?p tin SmoothProgressBar.dll, m b?n ? t?o ? cc "T?o m?t tu? ch?nh ProgressBar control"ph?n.
    4. Nh?p vo Ok. Nh?n th?y r?ng cc SmoothProgressBar i?u khi?n s? ?c thm vo h?p cng c?.
    5. Ko hai th? hi?n c?a cc SmoothProgressBar i?u khi?n t? h?p cng c? m?c ?nh m?u c?a d? n ?ng d?ng Windows.
  4. Ko m?t B? ?m th?i gian i?u khi?n t? h?p cng c? m?u.
  5. Thm m? sau y ? cc nh d?u s? ki?n c?a cc B? ?m th?i gian i?u khi?n:
    if (this.smoothProgressBar1.Value > 0)
    {
    	this.smoothProgressBar1.Value--;
    	this.smoothProgressBar2.Value++;
    }
    else
    {
    	this.timer1.Enabled = false;
    } 
    					
  6. Ko m?t Nt i?u khi?n t? h?p cng c? m?u.
  7. Thm m? sau y ? cc Nh?p vo s? ki?n c?a cc Nt i?u khi?n:
    this.smoothProgressBar1.Value = 100;
    this.smoothProgressBar2.Value = 0;
    			
    this.timer1.Interval = 1;
    this.timer1.Enabled = true; 
    					
  8. Trn cc G? l?i tr?nh n, nh?p vo B?t ?u ? ch?y cc d? n m?u.
  9. Nh?p vo nt. Ch ? r?ng cc ch? s? hai ti?n b? hi?n th? vn b?n "ti?n b?". M?t trong nh?ng ti?n b? ch? bo hi?n th? s? ti?n b? trong m?t cch th?c ngy cng tng, v cc ch? s? khc c?a ti?n b? hi?n th? s? ti?n b? trong gi?m m?t ho?c m?t cch ?m ng?c.

Thu?c tnh

ID c?a bi: 323116 - L?n xem xt sau cng: 27 Thang Tam 2011 - Xem xt l?i: 2.0
p d?ng
  • Microsoft Visual C# 2008 Express Edition
  • Microsoft Visual C# 2005 Express Edition
T? kha:
kbsweptvs2008 kbhowtomaster kbmt KB323116 KbMtvi
My d?ch
QUAN TRONG: Bi vi?t ny ?c d?ch b?ng ph?n m?m d?ch my c?a Microsoft ch? khng ph?i do con ng?i d?ch. Microsoft cung c?p cc bi vi?t do con ng?i d?ch v c? cc bi vi?t do my d?ch ? b?n c th? truy c?p vo t?t c? cc bi vi?t trong C s? Ki?n th?c c?a chng ti b?ng ngn ng? c?a b?n. Tuy nhin, bi vi?t do my d?ch khng ph?i lc no c?ng hon h?o. Lo?i bi vi?t ny c th? ch?a cc sai st v? t? v?ng, c php ho?c ng? php, gi?ng nh m?t ng?i n?c ngoi c th? m?c sai st khi ni ngn ng? c?a b?n. Microsoft khng ch?u trch nhi?m v? b?t k? s? thi?u chnh xc, sai st ho?c thi?t h?i no do vi?c d?ch sai n?i dung ho?c do ho?t ?ng s? d?ng c?a khch hng gy ra. Microsoft c?ng th?ng xuyn c?p nh?t ph?n m?m d?ch my ny.
Nh?p chu?t vo y ? xem b?n ti?ng Anh c?a bi vi?t ny:323116

Cung cp Phan hi

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com