Click here to Skip to main content
15,884,388 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.
Posted
Updated 22-Feb-13 7:12am
v2
Comments
Richard C Bishop 22-Feb-13 10:59am    
You might want to use a checkboxlist so that you can have multiple selections on one checkchanged event. Then you can use your if statements to check if a particular checkbox was checked.
Dustin Prevatt 22-Feb-13 11:22am    
I now have this but it doesn't seem to work as expected.

protected void CheckBoxList1_SelectedIndexChanged(object sender, EventArgs e)
{
foreach (ListItem chek in CheckBoxList1.Items)
{
if (chek.Selected)
{
int pg = ProgressBar1.Value + 10;
ProgressBar1.Value = pg;
}
else
{
int fg = ProgressBar1.Value - 10;
ProgressBar1.Value = fg;
}
}
}

richcb has a valid point ... a checkedListBox would prove useful. In which case you could use
C#
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.
C#
private void checkBox_CheckedChanged(object sender, EventArgs e)
{
    CheckBox cb = (CheckBox)sender;
    if(cb.Checked)
        progressBar1.Value += 10;
    else
        progressBar1.Value -= 10;
}

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...
 
Share this answer
 
Comments
Dustin Prevatt 22-Feb-13 11:34am    
i tried to use the following.
private void checkedListBox1_ItemCheck(object sender, ItemCheckEventArgs e)
{
if (e.NewValue == CheckState.Checked)
progressBar1.Value += 10;
else
progressBar1.Value -= 10;

}
But "CheckState" seems to be unrecognized. This is a C# asp page.
CHill60 22-Feb-13 11:51am    
Ah - didn't realise it was asp. I think you can do something like CheckBox cb = (CheckBox)sender;
if(cb.Checked) ...
Dustin Prevatt 22-Feb-13 11:55am    
Now.
private void checkedListBox1_ItemCheck(object sender, EventArgs e)
{
CheckBox cb = (CheckBox)sender;
if (e.NewValue == cb.Checked)
ProgressBar1.Value += 10;
else
ProgressBar1.Value -= 10;

}
and this error.
Error 1 'System.EventArgs' does not contain a definition for 'NewValue' and no extension method 'NewValue' accepting a first argument of type 'System.EventArgs' could be found (are you missing a using directive or an assembly reference?)
CHill60 22-Feb-13 12:00pm    
ASP isn't my area of expertise to be honest - I posted a straight forward WinForms solution that works in VS2005. Best use Jegan's solution
Dustin Prevatt 22-Feb-13 12:05pm    
@CHill60 - Thank You very much for your help!
Hi
you should call the updating progress bar function separate.

C#
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;
}


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

Regards
Jegan
 
Share this answer
 
v2
Comments
Dustin Prevatt 22-Feb-13 12:14pm    
Jegan, I tried to use the following code but the progress bar doesn't seem to be updating.

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++;
}
if (!ch.Checked)
{
count--;
}
}
}

ProgressBar1.Value = count * 10;
}


protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
{
UpdateProgressBar();
}
FYI, I am using this in asp.net. Thanks!
Jegan Thiyagesan 24-Feb-13 6:05am    
The code I wrote certainly works in WinformApplication, I am not sure about asp.net.

The additional code you wrote
if (!ch.Checked)
{
count--;
}
is wrong, if the first check box is not checked, you will decrement your count (means negative count), further down the multiplication by the negative count will be negative, so the progress bar will not display. In my original code, it will only increment the count if the check box is checked per check box.

Regards
Jegan

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900