Click here to Skip to main content
Sign Up to vote bad
good
See more: C#ASP.NETcheckbox
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
Jibesh15.7K

Comments
richcb - 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; } } }

2 solutions

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 - 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!
Jayanta Chatterjee - 22-Feb-13 12:18pm
+4
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  
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
Your Filters
Interested
Ignored
     
0 Christian Graus 544
1 Michael Haephrati 390
2 OriginalGriff 233
3 Ron Beyer 220
4 samadhan_kshirsagar 219
0 Sergey Alexandrovich Kryukov 6,959
1 Prasad_Kulkarni 3,689
2 OriginalGriff 3,402
3 _Amy 3,332
4 CPallini 2,950


Advertise | Privacy | Mobile
Web02 | 2.6.130617.1 | Last Updated 22 Feb 2013
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid