一个listbox中循环添加项,添加完以后双击listbox中的项就可以记录下双击每个项的次数,显示在label中!
其实就是做个小型的投票器,将侯选人添加到listbox中,双击侯选人名字,就可以记录他们的选票,不用数据库!

解决方案 »

  1.   

    定义一个按钮button1向listBox1中添加
    a b c d 分别放侯选人的次数 
    四个候选人 为ass   ass01   ass02   ass03        private void button1_Click(object sender, EventArgs e)
            {
                if (this.comboBox1.Text != "")
                { this.listBox1.Items.Add(this.comboBox1.Text); }
                else { MessageBox.Show("请在列表中选择项目"); }
                if (this.comboBox1.Text == "ass")
                { a++; }
                if (this.comboBox1.Text == "ass01")
                { b++; }
                if (this.comboBox1.Text == "ass02")
                { c++; }
                if (this.comboBox1.Text == "ass03")
                { d++; }
            }        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
            {
                if (this.listBox1.SelectedItem.ToString() == "ass")
                    label1.Text = a.ToString();
                if (this.listBox1.SelectedItem.ToString() == "ass01")
                    label1.Text = b.ToString();
                if (this.listBox1.SelectedItem.ToString() == "ass02")
                    label1.Text = c.ToString();
                if (this.listBox1.SelectedItem.ToString() == "ass03")
                    label1.Text = d.ToString();
            }