Get the App
SLTechnology News&Howtos  ›  Development  › 

How to display the progress bar of C# listview

Shulou Source: shulou.com Published: 2022-06-02 08:39:39 10月25日 Update

This article mainly explains "how to display the C# listview progress bar". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to display the C# listview progress bar".

Take a look at the specific implementation process.

The program is relatively simple, is to overload the implementation of listview, but very practical!

Using System; using System.Collections.Generic; using System.Text; using System.Windows.Forms; using System.Drawing; / / C # listview progress bar shows namespace WindowsApplication1 {class ListViewEx:System.Windows.Forms.ListView {public ListViewEx () {InitializeComponent ();} / / C # listview progress bar shows 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;} / / the C# listview progress bar shows public int ProgressColumIndex {set {progressIndex = value;} get {return progressIndex;}} int progressIndex =-1 / / < summary > / / check whether it can be converted to a floating point number / < / summary > const string numberstring = "0123456789."; private bool CheckIsFloat (String s) {/ / C # listview progress bar displays 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);} / / the C# listview progress bar shows 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) {/ / C # listview progress bar shows e.DrawDefault = true; base.OnDrawSubItem (e);} else {if (CheckIsFloat (e.Item.SubItems [e.ColumnIndex] .text)) / / determines whether the current subitem text can be converted to a floating point number {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 progress bar shows / draws the subitem private void DrawProgress (Rectangle rect, float percent) of the progress bar column 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)) {/ / drawing progress 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 progress bar shows else {return } Thank you for your reading. The above is the content of "how to display the C# listview progress bar". After the study of this article, I believe you have a deeper understanding of how the C# listview progress bar is displayed, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Tags: Progress clocking learning content that is points practicality ideas situations text articles more knowledge knowledge points programs articles follow process problems practice Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple Huawei MariaDB vpn OPPO Reno