每次选中复选框,我都希望能够以增量方式添加到进度条。因此,如果检查4个复选框中的1个,则等于让我们说25%的进度条。此外,如果取消选中4个复选框中的一个,则进度条将相应减少。这就是我遇到的问题。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void progressBar1_Click(object sender, EventArgs e)
{
progressBar1.Minimum = 0;
progressBar1.Maximum = 100;
int num1 = progressBar1.Maximum / 4;
int num2 = progressBar1.Maximum / 4;
int num3 = progressBar1.Maximum / 4;
int num4 = progressBar1.Maximum / 4;
int numAns;
numAns = num1 + num2 + num3 + num4;
progressBar1.Value = numAns;
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
if(checkBox1.Checked == true)
{
}
else if (checkBox1.Checked == false)
{
}
}
private void checkBox2_CheckedChanged(object sender, EventArgs e)
{
}
private void checkBox3_CheckedChanged(object sender, EventArgs e)
{
}
private void checkBox4_CheckedChanged(object sender, EventArgs e)
{
}
}
}
答案 0 :(得分:2)
您可以为所有复选框使用相同的事件处理程序,而无需为4个复选框制作4个方法...
private const Int32 TOTAL_CHECKBOXES = 4;
private static Int32 s_Checks = 0;
private void OnCheckedChanged(object sender, EventArgs e)
{
if (((CheckBox)sender).Checked)
++s_Checks;
else
--s_Checks;
progressBar.Value = s_Checks * (progressBar.Maximum / TOTAL_CHECKBOXES);
}
答案 1 :(得分:1)
废弃ProgressBar1_click,对于每个框,只需在CheckedChanged上的ProgressBar1.Value中添加(如果已选中)或减去(如果不是)25。
答案 2 :(得分:0)
您可以将同一事件连接到所有复选框。我将我添加到列表中,因此如果您希望将来添加更多内容,您只需添加处理程序并将其添加到列表中即可完成。
public Form1()
{
InitializeComponent();
checkBox1.CheckedChanged += CheckedChanged_1;
checkBox2.CheckedChanged += CheckedChanged_1;
checkBox3.CheckedChanged += CheckedChanged_1;
checkBox4.CheckedChanged += CheckedChanged_1;
checkboxesToCount.AddRange(new CheckBox[] {checkBox1, checkBox2, checkBox3, checkBox4});
}
private void CheckedChanged_1(object sender, EventArgs e)
{
progressBar1.Value = 100 * checkboxesToCount.Count((c) => { return c.Checked; }) / checkboxesToCount.Count;
}