Click here to Skip to main content
15,881,204 members
Please Sign up or sign in to vote.
2.00/5 (2 votes)
See more:
i want to add the integer if the items of the checklist box is checked.
and vice versa...
any idea...

lets say..
there are three items book, pen, color
if book checked the amount should be 200 and if both pen and book is checked 200+40=240 should be the new amount .
if book is unchecked then new amount should be 40...
Posted

Hello,

you can try this also

aspx markup:

XML
<asp:CheckBoxList ID="CheckBoxList1" runat="server">
            <asp:ListItem Value="book">The Book</asp:ListItem>
            <asp:ListItem Value="pen">The Pen</asp:ListItem>
            <asp:ListItem Value="color">The Color</asp:ListItem>
        </asp:CheckBoxList>


Code behind:
C#
public partial class CheckBoxLists : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            int totalValue = 0;
            foreach (ListItem item in CheckBoxList1.Items)
            {
                if (item.Selected)
                {
                    Choices currentChoice = (Choices)Enum.Parse(typeof(Choices),item.Value.ToString());
                    totalValue += (int)currentChoice;
                }
            }
            Response.Write("Total Choices: " + totalValue.ToString());
        }
    }

    public enum Choices
    {
        book = 100,
        pen = 40,
        color = 60
    }



Enjoy....

Thanks,
Hemnat
 
Share this answer
 
Comments
butchzn 9-May-11 10:02am    
Good solution, don't even need to use Enum can just use item.Value and convert to int
Hemant__Sharma 9-May-11 10:37am    
thanks butchzn. i just kept "value" member for its default usage i.e. some database field relate usage and the calculation values seperatly.
Anyway Thanks again.
Tried anything yourself? This should be extremely simple really. How about a method with an if block for each check box that simply adds the number relevant to that check box to a total then returns the total.

e.g.

C#
private int TotalCheckBoxes()
{
    int Total = 0;

    if([BookBox].Checked)
        Total += 200;

    if([PenBox].Checked)
        Total += 40;

    if([ColorBox].Checked)
        Total += 50;

    return Total;
}


Or even just have an array of ints that corresponds to each checkbox in your list then enumerate each checkbox in the list adding the relevant number from the array to a total.

Simply really,

Ed :)
 
Share this answer
 
v2
I got it...
now i have list of items in list box.
and same items in check list box.
as i select items in list box i want same item checked in listchecked box
any idea...
i tried
lstchk_box.Items[index].Select=ture;

but is not available in c sharp.
any idea...
 
Share this answer
 
you can try this it's simple and active

C#
int i;
int amount = 0;
string s;
for (i = 0; i <= (checkedListBox1.Items.Count - 1); i++)
{
    if (checkedListBox1.GetItemChecked(i))
    {
        switch (i)
        {
            case 0: amount = amount + 200; break;
            case 1: amount = amount + 40; break;
            case 2: amount = amount + 50; break;
        }
    }
}
s = amount.ToString();
MessageBox.Show(s);

Enjoy
Eshaq
 
Share this answer
 
its simple:
foreach(listitem lst in checkbox.item)
{
if(lst.checked)
{
//your code here
}
}
 
Share this answer
 

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



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