日期:2014-05-18 浏览次数:21060 次
这个很容易啊,,,
buttonClick事件中写代码
private void button_Click(object sender, EventArgs e)
{
Button btn = (Button)sender;
btn.Text = Convert.ToInt32(btn.Text.ToString()) + 1+"";
MessageBox.Show("投票成功\r\n谢谢参与!");
}
如果要更新到数据,用button的tag属性存储相关的信息,再更新到数据库
------解决方案--------------------
下面代码替换 Vote.cs 的内容,然后启动 Vote 界面,看看效果吧
using System;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Vote : Form
{
private int[][] array = new int[][] { new int[6], new int[6] };
public Vote()
{
InitializeComponent();
this.btn1.Tag = "00";
this.btn2.Tag = "01";
this.button3.Tag = "02";
this.button4.Tag = "03";
this.button5.Tag = "04";
this.button6.Tag = "05";
this.button7.Tag = "10";
this.button8.Tag = "11";
this.button9.Tag = "12";
this.button10.Tag = "13";
this.button11.Tag = "14";
this.button12.Tag = "15";
this.Register(this.groupBox1.Controls);
this.Register(this.groupBox2.Controls);
}
private void Register(Control.ControlCollection collection)
{
foreach (Control item in collection)
{
if (item is Button && item.Tag != null)
{
item.Click += this.ButtonClick;
item.DoubleClick += this.ButtonClick;
}
}
}
private void Vote_Load(object sender, EventArgs e)
{
}
private void ButtonClick(object sender, EventArgs e)
{
Button btn = (Button)sender;
char[] arr = ((string)btn.Tag).ToCharArray();
btn.Text = (++this.array[arr[0] - '0'][arr[1] - '0']).ToString();
}
}
}
------解决方案--------------------
重置事件
将2个groupbox放在一个panel中
private void button_Click(object sender, EventArgs e)
{
foreach (Control item in this.panel1.Controls)
{
if (item is Button)
item.Text = "0";
}
}
------解决方案--------------------
using System;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Vote : Form
{
private int[][] array = new int[][] { new int[6], new int[6] };
public Vote()
{
InitializeComponent();
this.btn1.Tag = "00";
this.btn2.Tag = "01";
this.button3.Tag = "02";
this.button4.Tag = "03";
this.button5.Tag = "04";
this.button6.Tag = "05";
this.button7.Tag = "10";
this.button8.Tag = "11";
this.button9.Tag = "12";
this.button10.Tag = "13";
this.button11.Tag = "14";
this.button12.Tag = "15";
Action<Control> me = v1 =>
{
v1.Click += this.ButtonClick;
v1.DoubleClick += this.ButtonClick;
};
this.ForEach(this.gro