0

每次选中复选框时,我都希望能够以增量方式添加到进度条。因此,假设 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

3 回答 3

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);
}
于 2013-01-20T04:03:58.897 回答
1

废弃 ProgressBar1_click,并为每个框简单地从 CheckedChanged 上的 ProgressBar1.Value 中添加(如果选中)或减去(如果没有)25。

于 2013-01-20T04:03:54.063 回答
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;
}
于 2013-01-20T04:17:11.583 回答