C#自定义控件

一、    开发环境和工具
1、    WIN10系统
2、    Visual Studio社区版 2019(C#)

二、    创建自定义控件窗体
①    添加一个自定义控件,右键解决方案,点击添加,然后点击用户控件

②    选择用户控件(Windows窗体)

 
③    得到如下图所示控件

 


④    从左侧工具箱拖曳控件并布局,如下图所示

 
⑤    查看工具箱就能够看见我们的自定义控件已经在工具箱生成了 
 

三、    添加相应函数

①    设置名称
 

public void SetName(string Name)
{
       this.lab_Name.Text = Name;
       return;
}


②    更新测试时间
 

     public void UpdateTimer(int Sec)
     {
         string StrHour;
         string StrMin;
         string StrSec;

         if ((Sec / 3600) < 10) 
             StrHour = "0" + (Sec / 3600).ToString(); 
         else 
             StrHour=(Sec / 3600).ToString();

         if (((Sec % 3600) / 60) < 10) 
             StrMin = "0" + ((Sec % 3600) / 60).ToString(); 
         else 
             StrMin= ((Sec % 3600) / 60).ToString();
             
         if (((Sec % 3600) % 60) < 10) 
            StrSec = "0" + ((Sec % 3600) % 60).ToString();
         else
            StrSec= ((Sec % 3600) % 60).ToString();
         this.textBox_TestTimer.Text = StrHour + ":" + StrMin + ":" + StrSec;
         return;
     }


③    更新测试状态

   public void UpdateTestState(bool LedRed,bool LedGreen, bool LedYellow)
    {
         if (!LedRed) 
            this.lab_LedRed.BackColor = System.Drawing.Color.Firebrick;
         else 
            this.lab_LedRed.BackColor = System.Drawing.Color.Red;

         if (!LedGreen)
            this.lab_LedGreen.BackColor = System.Drawing.Color.ForestGreen; 
         else
            this.lab_LedGreen.BackColor = System.Drawing.Color.Lime;

        if (!LedYellow) 
            this.lab_LedYellow.BackColor = System.Drawing.Color.ForestGreen; 
        else 
            this.lab_LedYellow.BackColor = System.Drawing.Color.Yellow;

        return;
    }

④设置ID

public void SetID(int IDIndex)
{
      if(IDIndex<10)
           this.lab_IDValue.Text ="00" + IDIndex.ToString();
       else if(IDIndex < 100)  
           this.lab_IDValue.Text = "0" + IDIndex.ToString();
       else if (IDIndex < 1000) 
           this.lab_IDValue.Text = IDIndex.ToString();

       return;
 }

四、    控件应用

using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        int Sec = 0;
        UserControl2[] userControl1 = new UserControl2[1024]; //创建控件数组
        private Layout layout = new Layout();
       
        /// <summary>
        /// 更新测试时间
        /// </summary>
        /// <param name="count"></param>
        /// <param name="Sec"></param>
        private void UpdataTimer(int count,int Sec)
        {
            for (int i = 0; i < count; i++)
            {
                 userControl1[i].UpdateTimer(Sec) ;
            }
        }
        /// <summary>
        /// 添加控件,count为添加控件数量
        /// </summary>
        /// <param name="count"></param>
        private void AddControl(int count)
        {
            int PanelWidth;
            int PanelHeight;
            int ColCount = 0;

           
            this.panel1.Controls.Clear();//清除面板控件
            PanelWidth = this.panel1.Width;
            PanelHeight = this.panel1.Height;
            layout.RowGap = Convert.ToInt32(textBox_RowGap.Text);    
            layout.ColGap = Convert.ToInt32(textBox_ColGap.Text);
            layout.LeftGap = Convert.ToInt32(textBox_LeftGap.Text);
            layout.TopGap = Convert.ToInt32(textBox_TopGap.Text);

            layout.Height = 134;
            layout.Width =218;
            //根据面板宽度,计算多少列
            ColCount = (PanelWidth - layout.LeftGap - layout.RightGap) / (layout.Width + layout.ColGap);

            for (int i = 0; i < count; i++)
            { 
                userControl1[i] = new UserControl2(); //实例化控件
                 //根据列距,行距,计算控件位置
                 userControl1[i].Location = new System.Drawing.Point((i % ColCount) * (layout.Width + layout.ColGap) +layout.LeftGap,(i / ColCount) * (layout.Height + layout.RowGap) + layout.TopGap);
                //更新测试状态
                userControl1[i].UpdateTestState(true, true,true);
               //设置名称
                userControl1[i].SetName("名称" + (i + 1).ToString());
                //设置ID
                userControl1[i].SetID(i + 1);
                //设置控件宽度和高度
                userControl1[i].Width = layout.Width;
                userControl1[i].Height = layout.Height;
                //添加相应控件
                this.panel1.Controls.Add(userControl1[i]); 
            }
            this.timer1.Start();

        }

        private void button1_Click_2(object sender, EventArgs e)
        {
            AddControl(32);
        }
       
        private void timer1_Tick(object sender, EventArgs e)
        {
            Sec++;
            UpdataTimer(32, Sec); //更新测试时间
        }
    }
} 

五、效果演示

C#自定义控件-CSDN直播C#自定义控件https://live.csdn.net/v/247427

 

  • 8
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值