进度条应该反映选中的复选框的数量
本文关键字:复选框 | 更新日期: 2023-09-27 17:54:28
每次选中复选框时,我都希望能够以增量方式添加到进度条中。假设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)
{
}
}
}
你可以为所有的复选框使用相同的事件处理程序,而不需要为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);
}
取消ProgressBar1_click,对于每个框只需从ProgressBar1中添加(如果勾选了)或减去(如果没有勾选)25。CheckedChanged上的值。
您可以将相同的事件连接到所有复选框。我将我的添加到一个列表中,因此,如果您将来想添加更多,您可以简单地添加处理程序并将其添加到列表中,然后就完成了。
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;
}