Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET checkbox
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 22-Feb-13 4:35am
Edited 22-Feb-13 7:12am
Jibesh16.6K
v2
Comments
richcb at 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 at 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;
}
}
}
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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;
}
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...
  Permalink  
Comments
Dustin Prevatt at 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 at 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 at 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 at 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 at 22-Feb-13 12:05pm
   
@CHill60 - Thank You very much for your help!
Jayanta Chatterjee at 22-Feb-13 12:18pm
   
+4
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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;
}
 
This way you will update all the check boxes that are checked in.
 
Regards
Jegan
  Permalink  
v2
Comments
Dustin Prevatt at 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 at 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)



Advertise | Privacy | Mobile
Web03 | 2.8.141015.1 | Last Updated 22 Feb 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100