简体   繁体   English

进行进度条更新,具体取决于选中了多少个复选框

[英]making a progress bar update, depending on how many check boxes are ticked

I have almost completed this, but my progress bar is really not very good. 我差不多完成了,但是进度栏确实不是很好。 I have a number of checkboxes. 我有很多复选框。 If a checkbox is checked then a set process will run. 如果选中此复选框,则将运行设置过程。 if the other check box is checked then another given process will run. 如果选中另一个复选框,则将运行另一个给定的进程。 below is an example of the code 下面是代码示例

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();
}

for every check box in my form, there is code very similar to this with a few changes. 对于表单中的每个复选框,都有一些与此非常相似的代码。

the "progress" variable is how i am updating my progress bar, with the backroundworker report progress (as you can see) “ progress”变量是我如何更新进度条以及backroundworker报告进度的方法(如您所见)

for each checkbox i have increased the progress variable by 3. so by time you get to the last checkbox progress = 100 对于每个复选框,我将进度变量增加了3。因此,到时间您将到达最后一个复选框,进度= 100

this is great, but if the user only ticks the first three checkboxes, the progress bar will go from 0% to 3%, to 6% and then jump straight to 100% at the end which isn' great. 这很好,但是如果用户仅勾选前三个复选框,进度条将从0%变为3%,达到6%,然后最后直接跳到100%,这很好。

Does anyone have any suggestions on how to make this a little bit more clever? 有没有人对如何使它变得更聪明有任何建议?

My first thought was to divide the amount of checkboxes ticked by 100, but then I wouldn't know how to update the progress bar with this data. 我最初的想法是将复选框的数量除以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);
}

Basically, keep all the checkboxes in a collection and call a method that updates the progress each time a checkbox is updated. 基本上,将所有复选框保留在一个集合中,并在每次更新复选框时调用一种更新进度的方法。 There are other ways to do this, I used an event-driven style. 还有其他方法可以实现,我使用了事件驱动的样式。

progress = (int)(((float)this.checkboxes.Count(cb => cb.Checked) / this.checkboxes.Length) * 100); progress =(int)((((float)this.checkboxes.Count(cb => cb.Checked)/ this.checkboxes.Length)* 100);;

should give you the percentage of the checkboxes that are checked compared to the total number of checkboxes, as an integer between 0 and 100. 应该为您提供已选中复选框相对于复选框总数的百分比,为0到100之间的整数。

NOTE: This answer provided by the OP, originally as part of his question. 注意:该回答由OP提供,最初是他的问题的一部分。 I have moved it here to be in line with site guidelines. 我已将其移至此处以符合站点指南。


Thanks for your suggestions guys. 谢谢你们的建议。 I have come to a solution I'm happy with. 我找到了自己满意的解决方案。 Because I'm new to C# I've had to come up with a solution that I understand. 因为我是C#的新手,所以必须提出一个我理解的解决方案。

Basically, I created a collection, and then had if statements to add or remove a checkbox from the collection based on whether or not they are checked. 基本上,我创建了一个集合,然后根据是否选中了if语句从集合中添加或删除复选框。

var cbs = new List<CheckBox>();


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

        else
        {
            cbs.Remove(imageinfo);
        }    

I then divided 100 by the collection count. 然后,我将100除以收集计数。 and had this as a variable "interval" 并将其作为变量“间隔”

int interval = 100 / cbs.Count;

I then had within each process 然后我进入了每个过程

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();

        }

progress = progress + interval. 进度=进度+间隔。 This meant that if I had 5 tick boxes ticked. 这意味着如果我勾选了5个复选框。 the interval would be 20 and the progress bar goes up by 20 each time a ticked process is complete. 间隔为20,并且每次打勾的过程完成时进度条都会增加20。

This is probably really dirty code, so I would still really appreciate suggestion on how it could be done better. 这可能是很脏的代码,所以我仍然非常感谢关于如何更好地完成工作的建议。 But as you can see my knowledge is pretty basic so alot of things go straight over my head. 但是,正如您所看到的,我的知识是非常基础的,所以很多事情直达我的脑海。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM