• c# winForm 简单的按钮用户控件示例


    一般情况下 按钮是不用做成用户控件的
    因为Button本身已经可以满足我们的常规需要
    将按钮做成用户控件
    是为了我们开发程序时 可以对用到的一批按钮进行同类操作
    如验证某一权限 更换某一皮肤等

    本次示例只实现最简单的按钮效果

    1.打开VS 新建Windows应用程序项目 WinFormStudy
      在新增的解决方案下 再新增Windows控件库项目 UserControlStudy
    2.在UserControl1的设计视图
      添加一个按钮button1
    3.修改button1的Dock属性为Fill
      使其可随控件的大小而改变
    4.公开对按钮的Text的设置
    public string UCButtonText
    {
        set
        {
            this.button1.Text = value;
        }
        get
        {
            return this.button1.Text;
        }
    }

    5.公开对按钮的Click的相应
      使之调用在使用控件时 所触发的控件的Click事件
    双击button1进入代码编写
    private void button1_Click(object sender, EventArgs e)
    {
        //MessageBox.Show("button1");
        base.OnClick(e);
    }

    6.编译及在WinFormStudy的Form1中使用该按钮用户控件
      进入Form1的设计视图
      在此时的工具箱中将发现多出来的UserControlStudy选项卡
      以及其下的UserControl1 也就是刚才我们编写的按钮用户控件
     
      拖入控件 修改UCButtonText属性
            以及进行控件的Click事件的编写
    private void userControl11_Click(object sender, EventArgs e)
    {
        MessageBox.Show("winFrom");
    }

    7.生成及运行WinFormStudy 查看Form1的按钮效果

  • 相关阅读:
    解决Cannot delete or update a parent row: a foreign key constraint fails的mysql报错
    zabbix4.2绘制网络拓扑图-添加链路速率
    zabbix 添加宏变量
    238_Product of Array Except Self
    122_Best Time to Buy and Sell Stock II
    260_Single Number III
    C# 比较时间问题
    226_Invert Binary Tree
    100_Same Tree
    283_Move Zeroes
  • 原文地址:https://www.cnblogs.com/freeliver54/p/1301431.html
Copyright © 2020-2023  润新知