利用鼠标点击次数来实现简单投票系统代码请教!
界面大概如下:
主要功能:
点击图中的红或蓝队中的按钮,分别从0开始递增(+1)。
结果分别显示在对应的按钮上面。
点击重置后,所有按钮显示结果清为0。
【源代码】:http://115.com/file/dpuspedq#简易投票系统.zip
http://115.com/file/dpuspedq#
各位有空的话,请帮忙直接下载源代码编辑,方便指导!谢谢。
[解决办法]
- C# code
这个很容易啊,,,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 界面,看看效果吧
- C# code
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";
}
}
[解决办法]
- C# code
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.groupBox1.Controls, me); this.ForEach(this.groupBox2.Controls, me); } private void ForEach(Control.ControlCollection collection, Action<Control> me) { foreach (Control item in collection) { if (item is Button && item.Tag != null) { me(item); } } } 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(); MessageBox.Show("投票成功!\n谢谢参与!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); } private void btnClear_Click(object sender, EventArgs e) { Action<Control> me = v1 => v1.Text = "0"; this.ForEach(this.groupBox1.Controls, me); this.ForEach(this.groupBox2.Controls, me); for (int i = 0; i < array.Length; i++) { int[] arr = this.array[i]; for (int j = 0; j < arr.Length; j++) { arr[j] = 0; } } } }}
[解决办法]
- C# code
相关判断自己写,,,int red=convert.toInt32(label20.text);int bule=convert.toInt32(label21.text);label22.text=red+bule+"";private void button_Click(object sender, EventArgs e){ Button btn = (Button)sender; btn.Text = Convert.ToInt32(btn.Text.ToString()) + 1+""; if(item.Tag.IndexOf("红")>-1) { red++; }else{bule++;}label20.text=red+"";label21.text=bule+"";label22.text=red+bule+""; MessageBox.Show("投票成功\r\n谢谢参与!");}
[解决办法]
- C# code
using System;using System.Windows.Forms;namespace WindowsFormsApplication1{ public partial class Vote : Form { private int[][] array = new int[][] { new int[6], new int[6] }; private Label[] lbarr = new Label[2]; private byte count = 0; 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.label20.Text = "0"; this.label21.Text = "0"; this.label22.Text = "0"; this.lbarr[0] = this.label20; this.lbarr[1] = this.label21; Action<Control> me = v1 => { v1.Click += this.ButtonClick; v1.DoubleClick += this.ButtonClick; }; this.ForEach(this.groupBox1.Controls, me); this.ForEach(this.groupBox2.Controls, me); } private void ForEach(Control.ControlCollection collection, Action<Control> me) { foreach (Control item in collection) { if (item is Button && item.Tag != null) { me(item); } } } 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(); int index = arr[0] - '0'; btn.Text = (++this.array[index][arr[1] - '0']).ToString(); Action<Label> act = v1 => v1.Text = (int.Parse(v1.Text) + 1).ToString(); act(this.lbarr[index]); act(this.label22); if (++this.count >= 4) { this.count = 0; MessageBox.Show("一人最多只能投4票!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); } else { MessageBox.Show("投票成功!\n谢谢参与!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); } } private void btnClear_Click(object sender, EventArgs e) { Action<Control> me = v1 => v1.Text = "0"; this.ForEach(this.groupBox1.Controls, me); this.ForEach(this.groupBox2.Controls, me); for (int i = 0; i < array.Length; i++) { int[] arr = this.array[i]; for (int j = 0; j < arr.Length; j++) { arr[j] = 0; } } this.label20.Text = "0"; this.label21.Text = "0"; this.label22.Text = "0"; } }}