日期:2014-05-18  浏览次数:20917 次

利用鼠标点击次数来实现简单投票系统代码请教!

界面大概如下:






主要功能:

点击图中的红或蓝队中的按钮,分别从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.gro