C#实现带进度条的ListView
来源:本站原创|时间:2020-01-10|栏目:C#教程|点击: 次
推荐阅读: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 ,希望对大家有所帮助。
您可能感兴趣的文章
- 01-10C#实现txt定位指定行完整实例
- 01-10WinForm实现仿视频 器左下角滚动新闻效果的方法
- 01-10C#实现清空回收站的方法
- 01-10C#实现读取注册表监控当前操作系统已安装软件变化的方法
- 01-10C#实现多线程下载文件的方法
- 01-10C#实现Winform中打开网页页面的方法
- 01-10C#实现远程关闭计算机或重启计算机的方法
- 01-10C#自定义签名章实现方法
- 01-10C#文件断点续传实现方法
- 01-10winform实现创建最前端窗体的方法
阅读排行
本栏相关
- 01-10C#通过反射获取当前工程中所有窗体并
- 01-10关于ASP网页无法打开的解决方案
- 01-10WinForm限制窗体不能移到屏幕外的方法
- 01-10WinForm绘制圆角的方法
- 01-10C#实现txt定位指定行完整实例
- 01-10WinForm实现仿视频 器左下角滚动新
- 01-10C#停止线程的方法
- 01-10C#实现清空回收站的方法
- 01-10C#通过重写Panel改变边框颜色与宽度的
- 01-10C#实现读取注册表监控当前操作系统已
随机阅读
- 08-05织梦dedecms什么时候用栏目交叉功能?
- 08-05dedecms(织梦)副栏目数量限制代码修改
- 01-10SublimeText编译C开发环境设置
- 01-10C#中split用法实例总结
- 01-11ajax实现页面的局部加载
- 08-05DEDE织梦data目录下的sessions文件夹有什
- 01-11Mac OSX 打开原生自带读写NTFS功能(图文
- 01-10delphi制作wav文件的方法
- 01-10使用C语言求解扑克牌的顺子及n个骰子
- 04-02jquery与jsp,用jquery