注册 登录
编程论坛 C# 论坛

请问button怎么设置值

hh373231690 发布于 2016-04-26 23:12, 4287 次点击
简单的功能 点击一个button 能够提取一个值输出
11 回复
#2
qq10235692232016-04-27 08:01
button_click事件里面处理。如果不清楚,你再说详细点。
#3
hh3732316902016-04-27 14:04
回复 2楼 qq1023569223
比如我有2个button,我想让button1代表数字1,button2代表数字2,然后他们的事件是同一个方法,按button1的时候得到的值是1,button2得到的值是数字2,像这样的 有什么解决方法吗 谢谢
#4
qq10235692232016-04-27 14:16
有点不明白你的意思。
程序代码:
namespace WindowsFormTest
{
    public partial class Form1 : Form
    {
        private int n1;
        private int n2;

        public Form1()
        {
            InitializeComponent();

            this.n1 = 0;
            this.n2 = 0;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.n1=1;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.n2=2;
        }
    }
}
#5
hh3732316902016-04-27 15:07
回复 4楼 qq1023569223
你这样是把button1和2的click独立了,每个button都写一个click事件的方法,如果有更多的按键重复一样的代码 就显得很多余,我想简化 对应到一个方法,比如按下button1,2,乃至3,4等都是调用一个input,但是我现在不知道怎么区分不同按键对应的值 比如按1就是1 按2就是2 明白了吗 ORZ
#6
yhlvht2016-04-27 21:03
Button不是都有名字的嘛,每个button名字不一样哇
还有Tag属性也可以用哇
还可以把button的引用放到字典里嘛,取出来比一比就知道是谁了哇
还有重写button的点击事件,继承EventArgs对象参数,增加属性,也可以区分哇
只有想不到,没有做不到
#7
hh3732316902016-04-27 21:14
回复 6楼 yhlvht
好吧 我就想看看有没有比较简便的方式。。刚学不久 懂得不多
#8
yhlvht2016-04-27 21:22
估计你还是需要一点代码作为参考
名字判断
程序代码:
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;
            if (b.Name == "button1")
            {
                MessageBox.Show("111111");
            }
            else if (b.Name == "button2")
            {
                MessageBox.Show("222222");
            }
        }
    }
}
#9
yhlvht2016-04-27 21:24
Tag属性判断
程序代码:
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);
            this.button1.Tag = "1111";
            this.button2.Tag = "2222";
        }

        void button1_Click(object sender, EventArgs e)
        {
            Button b = sender as Button;
            if (b.Tag.ToString() == "1111")
            {
                MessageBox.Show("111111");
            }
            else if (b.Tag.ToString() == "2222")
            {
                MessageBox.Show("222222");
            }
        }
    }
}
#10
yhlvht2016-04-27 21:28
字典判断
程序代码:
namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        Dictionary<Button, int> dict = new Dictionary<Button, int>();

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

        void button1_Click(object sender, EventArgs e)
        {
            Button b = sender as Button;
            int i = dict[b];
            if (i == 1)
            {
                MessageBox.Show("111111");
            }
            else if (i == 2)
            {
                MessageBox.Show("222222");
            }
        }
    }
}
#11
hh3732316902016-04-27 21:33
回复 9楼 yhlvht
多谢!
#12
yhlvht2016-04-27 21:58
重写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);
        }
    }
}
1