利用鼠标点击次数来实现简单投票系统代码请教! 界面大概如下:主要功能:点击图中的红或蓝队中的按钮,分别从0开始递增(+1)。结果分别显示在对应的按钮上面。点击重置后,所有按钮显示结果清为0。【源代码】:http://115.com/file/dpuspedq#简易投票系统.zip http://115.com/file/dpuspedq#各位有空的话,请帮忙直接下载源代码编辑,方便指导!谢谢。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 这个很容易啊,,,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(); } }} 所有按钮都调用同一个事件如果是存数据库tag存储是红队还是蓝队,第几个可以这样 tag=红-1,然后根据信息更新数据库 重置事件将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.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; } } } }} 如果要增加一些功能:1.设置一个限制:任意连续点击了4个之后,将弹出消息(之前点击一个便弹出“投票成功,多谢参与”去掉。):一人最多只能投4票!点击确定后可以重新投票。2.在红队下面加一label20标贴,用以计算上面红队各组鼠标点击的总次数;在蓝队下面加一label21标贴,用以计算上面蓝队各组鼠标点击的总次数;在整个窗口左下方再加一个label22用以,合计lable20和lable21的总次数。这样的功能该怎样实现呢? 相关判断自己写,,,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谢谢参与!");} 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"; } }}把 全部重置 按钮的事件关联下 Action<Control> me = v1 => { v1.Click += this.ButtonClick; v1.DoubleClick += this.ButtonClick; }; this.ForEach(this.groupBox1.Controls, me); this.ForEach(this.groupBox2.Controls, me);中的Action,ForEach等等是怎样输入的?我输入时没有智能提示。。直接输入则错误。为了方便对照,有空麻烦把你做的那个项目压缩一下上传上来或发到我邮箱:[email protected]。谢谢。 你把这全文替换 Vote.cs 里的内容,难道有报错,你换了 .net 版本? 现在电脑不在旁,今晚再试试了。我没有换.net环境,还是原来那个,NET4.0的。全部复制过去的话,有一大堆的错误。 对象调用它的方法,可是在它调用的对象之中如何获得这个对象的属性呢? 如何在3D全屏程序中绘制自己的内容? 搞.net开发,大家用vs2005的多还是用vs2008的多? 寻一个 visual studio 2005 team suite 中文版的下载链接,多谢! 求助编程高手,在线等!一简单的编程题 求助.DataGrid列问题 小弟,期末作业,增加学生信息那一块错在那了啊!帮帮小弟啊! !---问一个小问题-----! 请各位推荐一个好的C#编辑器! Urho c# 引用C++ Dll异常 怎么判断文本中的年月日,是数组的第几个值? Silverlight为什么首次加载比较慢
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属性存储相关的信息,再更新到数据库
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();
}
}
}
所有按钮都调用同一个事件如果是存数据库
tag存储是红队还是蓝队,第几个
可以这样 tag=红-1,然后根据信息更新数据库
将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.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;
}
}
}
}
}
任意连续点击了4个之后,将弹出消息(之前点击一个便弹出“投票成功,多谢参与”去掉。):
一人最多只能投4票!点击确定后可以重新投票。2.
在红队下面加一label20标贴,用以计算上面红队各组鼠标点击的总次数;
在蓝队下面加一label21标贴,用以计算上面蓝队各组鼠标点击的总次数;在整个窗口左下方再加一个label22用以,合计lable20和lable21的总次数。这样的功能该怎样实现呢?
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谢谢参与!");}
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";
}
}
}把 全部重置 按钮的事件关联下
{
v1.Click += this.ButtonClick;
v1.DoubleClick += this.ButtonClick;
};
this.ForEach(this.groupBox1.Controls, me);
this.ForEach(this.groupBox2.Controls, me);中的Action,ForEach等等是怎样输入的?我输入时没有智能提示。。直接输入则错误。
为了方便对照,有空麻烦把你做的那个项目压缩一下上传上来或发到我邮箱:[email protected]。
谢谢。
现在电脑不在旁,今晚再试试了。我没有换.net环境,还是原来那个,NET4.0的。全部复制过去的话,有一大堆的错误。