C#实现带进度条的ListView

C#实现带进度条的ListView,第1张

概述推荐阅读:ListView百分比进度条(delphi版)对于已经有的组件,可以直接添加进来,添加后要先运行一下,然后会在工具箱内找到相应控件。

推荐阅读: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.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.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){//设置进度条的ColunIndexthis.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 ,希望对大家有所帮助。

总结

以上是内存溢出为你收集整理的C#实现带进度条的ListView全部内容,希望文章能够帮你解决C#实现带进度条的ListView所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1260834.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-08
下一篇 2022-06-08

发表评论

登录后才能评论

评论列表(0条)

保存