zoukankan      html  css  js  c++  java
  • .NET winform 在listview中添加progressbar

    找了好长时间没找到,后来索性自己写了一个:

    首先,在往listview加载数据的事件里添加progressbar:

                foreach (string d in arr)
                {
                    int index = lv.Items.Count + 1;
                    item = new ListViewItem(new string[] { index.ToString(), d, "", "", "", "" });
                    lv.Items.Add(item);
    
                    float progress = 0;
    
                    Rectangle SizeR = default(Rectangle);
                    System.Windows.Forms.ProgressBar ProgBar = new System.Windows.Forms.ProgressBar();
                    SizeR = item.SubItems[2].Bounds;
                    SizeR.Width = lv.Columns[2].Width;
                    ProgBar.Parent = lv;
                    ProgBar.SetBounds(SizeR.X, SizeR.Y, SizeR.Width, SizeR.Height);
                    ProgBar.Value = (int)progress;
                    ProgBar.Visible = true;
                    //取一个唯一的名字,以后好找
                    ProgBar.Name = d + "progressbar";
                }

    然后在需要修改progressbar的值的地方设置它的值:

    //循环listview上的所有控件,按名字找到progressbar
    foreach (Control item in lv.Controls)
    {
           if (item.Name == d.Name + "progressbar")
           {
                ProgressBar bar = (ProgressBar)item;
                bar.Value = (int)((d.Progress) * 100);
           }
    }

    其实我们只是把progressbar根据长宽高固定在了listview指定的格子里,如果我们拖动listview中的列,格子的位置会发生改变,这时候需要修改对应proressbar的位置,我们需要添加ColumnWidthChanging事件,在拖动column的时候,progressbar会随着改变位置:

            private void lvt_ColumnWidthChanging(object sender, ColumnWidthChangingEventArgs e)
            {
    
                Rectangle SizeR = default(Rectangle);
    
                int width = e.NewWidth;
                
                foreach (Control item in lv.Controls)
                {
                    //根据名字找到所有的progressbar
                    if (item.Name.IndexOf("progressbar") >= 0)
                    {
                        ProgressBar bar = (ProgressBar)item;
                        
                        //Rectangle size=bar.Bounds;
                        SizeR=bar.Bounds;
                        //lv.Columns[2]是放置progressbar的地方
                        SizeR.Width=lv.Columns[2].Width;
                        bar.SetBounds(lv.Items[0].SubItems[2].Bounds.X, SizeR.Y, SizeR.Width, SizeR.Height);
                        //bar.Width = width;
                    }
                }
            }
  • 相关阅读:
    网站漏洞扫描工具Uniscan
    iOS 11开发教程(八)定制iOS11应用程序图标
    iOS 11开发教程(七)编写第一个iOS11代码Hello,World
    UDP转TCP隧道工具udptunnel
    iOS 11开发教程(六)iOS11Main.storyboard文件编辑界面
    Web服务评估工具Nikto
    iOS 11开发教程(五)iOS11模拟器介绍二
    网络数据修改工具netsed
    iOS 11开发教程(四)iOS11模拟器介绍一
    兼容IE getElementsByClassName取标签
  • 原文地址:https://www.cnblogs.com/mrhyher/p/5369837.html
Copyright © 2011-2022 走看看