(七)c#Winform自定义控件-进度条

时间:2020-12-07 08:37:56

官网

http://www.hzhcontrols.com

前提

入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章。

GitHub:https://github.com/kwwwvagaa/NetWinformControl

码云:https://gitee.com/kwwwvagaa/net_winform_custom_control.git

如果觉得写的还行,请点个 star 支持一下吧

欢迎前来交流探讨: 企鹅群568015492 

idkey=6e08741ef16fe53bf0314c1c9e336c4f626047943a8b76bac062361bab6b4f8d">(七)c#Winform自定义控件-进度条

目录

https://www.cnblogs.com/bfyx/p/11364884.html

准备工作

该控件将继承基类控件UCControlBase,如果你还对UCControlBase不了解,请移步 (一)c#Winform自定义控件-基类控件 查看

开始

这个控件比较简单,没有多少东西,看下关键代码吧

  private int _value = ;

         public int Value
{
get { return this._value; }
set
{
if (value < )
return;
this._value = value;
SetValue();
}
} private int maxValue = ; public int MaxValue
{
get { return maxValue; }
set
{
if (value <= )
return;
maxValue = value;
SetValue();
}
} private void SetValue()
{
double dbl = (double)_value / (double)maxValue;
this.panel1.Width = (int)(this.Width * dbl);
} public ProcessExt()
{
InitializeComponent();
} private void ProcessExt_SizeChanged(object sender, EventArgs e)
{
SetValue();
} public void Step()
{
Value++;
}

然后看下完成代码吧

 // 版权所有  黄正辉  交流群:568015492   QQ:623128629
// 文件名称:ProcessExt.cs
// 创建日期:2019-08-15 16:02:44
// 功能描述:Process
// 项目地址:https://gitee.com/kwwwvagaa/net_winform_custom_control
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms; namespace HZH_Controls.Controls
{
public partial class ProcessExt : UCControlBase
{
private int _value = ; public int Value
{
get { return this._value; }
set
{
if (value < )
return;
this._value = value;
SetValue();
}
} private int maxValue = ; public int MaxValue
{
get { return maxValue; }
set
{
if (value <= )
return;
maxValue = value;
SetValue();
}
} private void SetValue()
{
double dbl = (double)_value / (double)maxValue;
this.panel1.Width = (int)(this.Width * dbl);
} public ProcessExt()
{
InitializeComponent();
} private void ProcessExt_SizeChanged(object sender, EventArgs e)
{
SetValue();
} public void Step()
{
Value++;
}
}
}
 namespace HZH_Controls.Controls
{
partial class ProcessExt
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null; /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
} #region 组件设计器生成的代码 /// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.panel1 = new System.Windows.Forms.Panel();
this.SuspendLayout();
//
// panel1
//
this.panel1.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)()))), ((int)(((byte)()))), ((int)(((byte)()))));
this.panel1.Dock = System.Windows.Forms.DockStyle.Left;
this.panel1.Location = new System.Drawing.Point(, );
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(, );
this.panel1.TabIndex = ;
//
// ProcessExt
//
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None;
this.BackColor = System.Drawing.Color.White;
this.ConerRadius = ;
this.Controls.Add(this.panel1);
this.IsRadius = true;
this.Name = "ProcessExt";
this.Size = new System.Drawing.Size(, );
this.SizeChanged += new System.EventHandler(this.ProcessExt_SizeChanged);
this.ResumeLayout(false); } #endregion private System.Windows.Forms.Panel panel1;
}
}

用处及效果

用处:就是一个进度条

效果:

(七)c#Winform自定义控件-进度条

最后的话

如果你喜欢的话,请到 https://gitee.com/kwwwvagaa/net_winform_custom_control 点个星 星吧