Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 根据勾选的复选框数量进行进度条更新_C#_Checkbox_Progress Bar - Fatal编程技术网

C# 根据勾选的复选框数量进行进度条更新

C# 根据勾选的复选框数量进行进度条更新,c#,checkbox,progress-bar,C#,Checkbox,Progress Bar,我几乎完成了这个,但我的进度条真的不是很好。我有许多复选框。如果选中复选框,则将运行设置进程。如果选中“其他”复选框,则将运行另一个给定进程。下面是一个代码示例 if (pslist.Checked == true) { progress = 9; backgroundWorker1.ReportProgress(progress); string commandlineargs = "-f " + imagefile + " --profile=" + pr

我几乎完成了这个,但我的进度条真的不是很好。我有许多复选框。如果选中复选框,则将运行设置进程。如果选中“其他”复选框,则将运行另一个给定进程。下面是一个代码示例

if (pslist.Checked == true)
{
      progress = 9;
      backgroundWorker1.ReportProgress(progress);
      string commandlineargs = "-f " + imagefile + " --profile=" + profile + " pslist" + " --output-file=" + output + @"\pslist.txt";
      Process process = new Process();
      ProcessStartInfo startInfo = new ProcessStartInfo();
      startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
      startInfo.FileName = vollocation;
      startInfo.Arguments = commandlineargs;
      process.StartInfo = startInfo;
      process.Start();
      process.WaitForExit();
      process.Close();
}
对于我表单中的每个复选框,都有与此非常相似的代码,只是做了一些更改

“progress”变量是我使用backroundworker报告进度更新进度栏的方式(如您所见)

对于每个复选框,我将进度变量增加了3。因此,当您到达最后一个复选框时,progress=100

这很好,但如果用户只勾选前三个复选框,进度条将从0%变为3%,再变为6%,然后在最后直接跳到100%,这并不好

有没有人对如何让这更聪明一点有什么建议

我的第一个想法是将勾选的复选框数量除以100,但是我不知道如何用这些数据更新进度条

Checkbox[] checkboxes;    

public void FormInitializationOrWhatever()
{
    Checkbox checkboxName1 = new CheckBox(), checkboxName2 = new CheckBox(), ...

    this.checkboxes = new CheckBox[] { checkboxName1, checkboxName2, ... };

    foreach (CheckBox cb in this.checkboxes)
    {
        cb.CheckedChanged += onSomeCheckboxWasClicked;
    }
}    

void onSomeCheckboxWasClicked(object sender, EventArgs e)
{
    progress = (int)(((float)this.checkboxes.Count(cb => cb.Checked) / this.checkboxes.Length) * 100);
}
基本上,保留集合中的所有复选框,并在每次更新复选框时调用更新进度的方法。还有其他方法可以做到这一点,我使用了事件驱动的风格

进度=(int)((float)this.checkbox.Count(cb=>cb.Checked)/this.checkbox.Length)*100)


应给出选中的复选框占复选框总数的百分比,作为0到100之间的整数。

注意:此答案由OP提供,最初作为其问题的一部分。我把它移到这里是为了符合现场指南


谢谢你们的建议,伙计们。我找到了一个我满意的解决办法。因为我是C#的新手,我必须想出一个我理解的解决方案

基本上,我创建了一个集合,然后使用if语句根据是否选中它们来添加或删除集合中的复选框

var cbs = new List<CheckBox>();


        if(imageinfo.Checked == true)
        {
            cbs.Add(imageinfo);
        }

        else
        {
            cbs.Remove(imageinfo);
        }    
然后我在每个过程中

if (imageinfo.Checked == true)
        {

            progress = progress + interval;
            string commandlineargs = "-f " + imagefile + " imageinfo" + " --output-file=" + output + @"\imageinfo.txt";
            Process process = new Process();
            ProcessStartInfo startInfo = new ProcessStartInfo();
            startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
            startInfo.FileName = vollocation;
            startInfo.Arguments = commandlineargs;
            process.StartInfo = startInfo;
            process.Start();
            process.WaitForExit();
            process.Close();

        }
进度=进度+间隔。这意味着如果我有5个勾选框。间隔为20,每次勾选流程完成时,进度条都会增加20


这可能是非常肮脏的代码,所以我仍然非常感谢关于如何做得更好的建议。但是正如你所看到的,我的知识非常基础,所以很多事情我都不知道。

当你选中任何复选框时,为什么不使用全局计数器,比如
counter+=3
。?
if (imageinfo.Checked == true)
        {

            progress = progress + interval;
            string commandlineargs = "-f " + imagefile + " imageinfo" + " --output-file=" + output + @"\imageinfo.txt";
            Process process = new Process();
            ProcessStartInfo startInfo = new ProcessStartInfo();
            startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
            startInfo.FileName = vollocation;
            startInfo.Arguments = commandlineargs;
            process.StartInfo = startInfo;
            process.Start();
            process.WaitForExit();
            process.Close();

        }