程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> C#完成帶進度條的ListView

C#完成帶進度條的ListView

編輯:C#入門知識

C#完成帶進度條的ListView。本站提示廣大學習愛好者:(C#完成帶進度條的ListView)文章只能為提供參考,不一定能成為您想要的結果。以下是C#完成帶進度條的ListView正文


推舉浏覽:ListView 百分比進度條(delphi版)

關於曾經有的組件,可以直接添加出去,添加後要先運轉一下,然後會在對象箱內找到響應控件。

1、起首編寫組件,然後將組件添加到對象箱內


編寫代碼以下:

public partial class ListViewEx : System.Windows.Forms.ListView
{
public ListViewEx()
{
InitializeComponent();
}
//C# listview進度條顯示
private Color mProgressColor = Color.Red;
public Color ProgressColor
{
get
{
return this.mProgressColor;
}
set
{
this.mProgressColor = value;
}
}
private Color mProgressTextColor = Color.Black;
public Color ProgressTextColor
{
get
{
return mProgressTextColor;
}
set
{
mProgressTextColor = value;
}
}
public int ProgressColumIndex
{
set
{
progressIndex = value;
}
get
{
return progressIndex;
}
}
int progressIndex = -1;
const string numberstring = "0123456789.";
private bool CheckIsFloat(String s)
{
//C# listview進度條顯示
foreach (char c in s)
{
if (numberstring.IndexOf(c) > -1)
{ continue; }
else return false;
}
return true;
}
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
}
//C# listview進度條顯示
private void InitializeComponent()
{
this.OwnerDraw = true;
this.View = View.Details;
}
protected override void OnDrawColumnHeader(DrawListViewColumnHeaderEventArgs e)
{
e.DrawDefault = true;
base.OnDrawColumnHeader(e);
}
protected override void OnDrawSubItem(DrawListViewSubItemEventArgs e)
{
if (e.ColumnIndex != this.progressIndex)
{
e.DrawDefault = true; base.OnDrawSubItem(e);
}
else
{
if (CheckIsFloat(e.Item.SubItems[e.ColumnIndex].Text))
//斷定以後subitem文本能否可以轉為浮點數
{
float per = float.Parse(e.Item.SubItems[e.ColumnIndex].Text);
if (per >= 1.0f) { per = per / 100.0f; }
Rectangle rect = new Rectangle(e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height);
DrawProgress(rect, per, e.Graphics);
}
}
}
//C# listview進度條顯示 ///繪制進度條列的subitem 
private void DrawProgress(Rectangle rect, float percent, Graphics g)
{
if (rect.Height > 2 && rect.Width > 2)
{
if ((rect.Top > 0 && rect.Top < this.Height) && (rect.Left > this.Left && rect.Left < this.Width))
{
//繪制進度 
int width = (int)(rect.Width * percent);
Rectangle newRect = new Rectangle(rect.Left + 1, rect.Top + 1, width - 2, rect.Height - 2);
using (Brush tmpb = new SolidBrush(this.mProgressColor))
{ g.FillRectangle(tmpb, newRect); }
newRect = new Rectangle(rect.Left + 1, rect.Top + 1, rect.Width - 2, rect.Height - 2);
g.DrawRectangle(Pens.RoyalBlue, newRect);
StringFormat sf = new StringFormat();
sf.Alignment = StringAlignment.Center;
sf.LineAlignment = StringAlignment.Center;
sf.Trimming = StringTrimming.EllipsisCharacter;
newRect = new Rectangle(rect.Left + 1, rect.Top + 1, rect.Width - 2, rect.Height - 2);
using (Brush b = new SolidBrush(mProgressTextColor))
{
g.DrawString(percent.ToString("p1"), this.Font, b, newRect, sf);
}
}
}
//C# listview進度條顯示
else
{
return;
}
} 
}

2、挪用辦法:

private void Form1_Load(object sender, EventArgs e)
{
ListViewItem lviUserName = new ListViewItem();
ListViewItem.ListViewSubItem lvsinc = new ListViewItem.ListViewSubItem();
ListViewItem.ListViewSubItem lvsihostname = new ListViewItem.ListViewSubItem();
ListViewItem.ListViewSubItem lvsiip = new ListViewItem.ListViewSubItem();
lviUserName.Text = "5";
lvsinc.Text = "4";
lvsihostname.Text = "3";
lvsiip.Text = "100";
lviUserName.SubItems.Add(lvsinc);
lviUserName.SubItems.Add(lvsihostname);
lviUserName.SubItems.Add(lvsiip);
this.listView1.Items.Add(lviUserName);
this.listView1.ProgressTextColor = Color.Red;
this.listView1.ProgressColor = Color.YellowGreen;
}
private void listView1_DrawSubItem(object sender, DrawListViewSubItemEventArgs e)
{
//設置進度條的ColunIndex
this.listView1.ProgressColumIndex = 1;
}
private void timer1_Tick(object sender, EventArgs e)
{
if (Convert.ToInt32(listView1.Items[0].SubItems[1].Text.ToString()) <= 100)
{
//進度條數字更新
listView1.Items[0].SubItems[1].Text = (Convert.ToInt32(listView1.Items[0].SubItems[1].Text.ToString()) + 1).ToString();
}
}

3、留意要添加Timer控件

響應屬性設置以下:


4、運轉成果以下所示


以上所述是基於C#完成帶進度條的ListView ,願望對年夜家有所贊助。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved