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

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/kwwwvagaa/article/details/100586865

前提

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

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

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

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

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

目录

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

用处及效果

用处:就是一个进度条

效果:

准备工作

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

开始

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

private int _value = 0;

        public int Value
        {
            get { return this._value; }
            set
            {
                if (value < 0)
                    return;
                this._value = value;
                SetValue();
            }
        }

        private int maxValue = 100;

        public int MaxValue
        {
            get { return maxValue; }
            set
            {
                if (value <= 0)
                    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 = 0;

        public int Value
        {
            get { return this._value; }
            set
            {
                if (value < 0)
                    return;
                this._value = value;
                SetValue();
            }
        }

        private int maxValue = 100;

        public int MaxValue
        {
            get { return maxValue; }
            set
            {
                if (value <= 0)
                    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)(23)))), ((int)(((byte)(127)))), ((int)(((byte)(203)))));
            this.panel1.Dock = System.Windows.Forms.DockStyle.Left;
            this.panel1.Location = new System.Drawing.Point(0, 0);
            this.panel1.Name = "panel1";
            this.panel1.Size = new System.Drawing.Size(0, 22);
            this.panel1.TabIndex = 0;
            // 
            // ProcessExt
            // 
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None;
            this.BackColor = System.Drawing.Color.White;
            this.ConerRadius = 5;
            this.Controls.Add(this.panel1);
            this.IsRadius = true;
            this.Name = "ProcessExt";
            this.Size = new System.Drawing.Size(291, 22);
            this.SizeChanged += new System.EventHandler(this.ProcessExt_SizeChanged);
            this.ResumeLayout(false);

        }

        #endregion

        private System.Windows.Forms.Panel panel1;
    }
}

最后的话

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

猜你喜欢

转载自blog.csdn.net/kwwwvagaa/article/details/100586865