| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 4057 人关注过本帖
标题:请问button怎么设置值
只看楼主 加入收藏
hh373231690
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:67
专家分:138
注 册:2012-10-13
收藏
得分:0 
回复 9楼 yhlvht
多谢!
2016-04-27 21:33
yhlvht
Rank: 13Rank: 13Rank: 13Rank: 13
等 级:贵宾
威 望:36
帖 子:707
专家分:4405
注 册:2011-9-30
收藏
得分:0 
重写button有些复杂,就这个例子看有点画蛇添足了,代码也给出来参考吧,需要修改设计器代码,把原有的System.Windows.Forms.Button换成自己的Button
程序代码:
namespace WindowsFormsApplication1
{
    partial class Form1
    {
        /// <summary>
        /// 必需的设计器变量。
        /// </summary>
        private  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 Windows 窗体设计器生成的代码

        /// <summary>
        /// 设计器支持所需的方法 - 不要
        /// 使用代码编辑器修改此方法的内容。
        /// </summary>
        private void InitializeComponent()
        {
            this.components = new ();
            this.serialPort1 = new (this.components);
            this.button1 = new ButtonEx();
            this.button2 = new ButtonEx();
            this.SuspendLayout();
            // 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point(69, 52);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(75, 23);
            this.button1.TabIndex = 0;
            this.button1.Text = "button1";
            this.button1.UseVisualStyleBackColor = true;
            // 
            // button2
            // 
            this.button2.Location = new System.Drawing.Point(69, 103);
            this.button2.Name = "button2";
            this.button2.Size = new System.Drawing.Size(75, 23);
            this.button2.TabIndex = 1;
            this.button2.Text = "button2";
            this.button2.UseVisualStyleBackColor = true;
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(284, 262);
            this.Controls.Add(this.button2);
            this.Controls.Add(this.button1);
            this.Name = "Form1";
            this.Text = "Form1";
            this.Load += new System.EventHandler(this.Form1_Load);
            this.ResumeLayout(false);

        }
        #endregion

        private  serialPort1;
        private ButtonEx button1;
        private ButtonEx button2;
    }
}




程序代码:
namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.button1.Click += new EventHandler(button1_Click);
            this.button2.Click += new EventHandler(button1_Click);
        }

        void button1_Click(object sender, EventArgs e)
        {
            Button b = sender as Button;
            EventArgsEx ex = (EventArgsEx)e;
            if (ex.I == 1)
            {
                MessageBox.Show("111111");
            }
            else if (ex.I == 2)
            {
                MessageBox.Show("222222");
            }
        }
    }

    /// <summary>
    /// 继承EventArgs 加一个属性
    /// </summary>
    public class EventArgsEx : EventArgs
    {
        public int I { get; set; }
    }

    /// <summary>
    /// 继承Button 重写OnClick方法
    /// 传入被修改过的EventArgsEx参数
    /// </summary>
    public class ButtonEx : Button
    {
        protected override void OnClick(EventArgs e)
        {
            EventArgsEx ex = new EventArgsEx();
            if (this.Name == "button1")
            {
                ex.I = 1;
            }
            else if (this.Name == "button2")
            {
                ex.I = 2;
            }
            base.OnClick(ex);
        }
    }
}
2016-04-27 21:58
快速回复:请问button怎么设置值
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.047898 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved