Foreach Checkbox.Checked - >进度条价值增加 [英] Foreach Checkbox.Checked --> Progress Bar value increase

查看:96
本文介绍了Foreach Checkbox.Checked - >进度条价值增加的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何根据页面上选中的复选框数量增加进度条的值。我认为它会是这样的但我不太确定。

I would like to know how to increment the value of a progress bar based on the number of checkboxes checked on a page. I would think it would be something like this but i''m not really sure.

protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
   {
       if (CheckBox1.Checked == true)
       {
           int pg = ProgressBar1.Value + 10;
           ProgressBar1.Value = pg;
       }
       else
       {
           int fg = ProgressBar1.Value - 10;
           ProgressBar1.Value = fg;
       }
   }





任何帮助都将不胜感激。



Any help would be appreciated.

推荐答案

richcb有一个有效的点......一个checkedListBox会证明是有用的。在这种情况下你可以使用

richcb has a valid point ... a checkedListBox would prove useful. In which case you could use
private void checkedListBox1_ItemCheck(object sender, ItemCheckEventArgs e)
{
    if (e.NewValue == CheckState.Checked)
        progressBar1.Value += 10;
    else
        progressBar1.Value -= 10;

}






or

if(e.NewValue==CheckState.Checked)
    progressBar1.Value = (checkedListBox1.CheckedItems.Count + 1) * 10;
else
    progressBar1.Value = (checkedListBox1.CheckedItems.Count - 1) * 10;



但是如果你必须使用单独的复选框然后写一个函数,例如


But if you have to use individual checkboxes then write a single function e.g.

private void checkBox_CheckedChanged(object sender, EventArgs e)
{
    CheckBox cb = (CheckBox)sender;
    if(cb.Checked)
        progressBar1.Value += 10;
    else
        progressBar1.Value -= 10;
}



并以表单Designer.cs的形式将其添加为每个复选框的事件处理程序


and in the form Designer.cs add this as the event handler for each of the checkboxes

this.checkBox1.CheckedChanged += new System.EventHandler(this.checkBox_CheckedChanged);
...
this.checkBox2.CheckedChanged += new System.EventHandler(this.checkBox_CheckedChanged);
etc...


您好b $ b

您应该单独调用更新进度条功能。< br $> b $ b

Hi
you should call the updating progress bar function separate.

protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
{
    UpdateProgressBar();
}

protected void CheckBox2_CheckedChanged(object sender, EventArgs e)
{
    UpdateProgressBar();
}

private void UpdateProgressBar()
{
    int count = 0;
    for (int i = 0; i < this.Controls.Count; ++i)
    {
         Control ctl = this.Controls[i];
         Type ty = ctl.GetType();
         if (ty.Name.Equals("CheckBox"))
         {
              CheckBox ch = (CheckBox)ctl;
              if (ch.Checked)
              {
                  count++;
              }
          }
     }

     progressBar.Value = count * 10;
}





这样您就会更新所有已签入的复选框。



问候

Jegan



This way you will update all the check boxes that are checked in.

Regards
Jegan


这篇关于Foreach Checkbox.Checked - &gt;进度条价值增加的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆