请问button怎么设置值
简单的功能 点击一个button 能够提取一个值输出
程序代码: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;
}
}
}

程序代码: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");
}
}
}
}
程序代码: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");
}
}
}
}
程序代码: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");
}
}
}
}