0

So i'm trying to extract the text property from a checkbox and displaying it into a listbox, but it seems like it doesn't matter if its checked or not what am i doing wrong.

 private void btnCompute_Click(object sender, EventArgs e)
    {
        string accessories = "";
        if (chkChildSeat.Checked) ;
        {
            accessories = accessories + chkChildSeat.Text+",";
        }
        if (chkLockHelmet.Checked) ;
        {
            accessories = accessories + chkLockHelmet.Text+",";
        }
        if (chkTrailer.Checked) ;
        {
            accessories=accessories+chkTrailer.Text;
        }
        lstInvoice.Items.Add(String.Format("{0,-49}{1,-14}", "Invoice Date", DateTime.Now.ToString("d")));
        lstInvoice.Items.Add(String.Format("{0,-50}{1,-13}", Customername, CustomerID));
        lstInvoice.Items.Add(String.Format("{0,-49}{1,-14}", "Rental Date", dateTimePicker1.Value));
        lstInvoice.Items.Add(string.Format("{0,-25}{1,-28}", "Accessories", accessories.ToString()));
        lstInvoice.Items.Add(string.Format("{0,-49}{1,-14}", "Total Invoice Amount", result.ToString("c")));
    }

this will display all the checkboxname.text properties regardless of whether or not its checked Could anyone tell me why the text is cutoff on the listbox as more options are checked does it has something to do with my string.format line?

lstInvoice.Items.Add(string.Format("{0,-25}{1,-28}", "Accessories", accessories.ToString()));

1 Answer 1

2

Remove the semicolon after the if's

Your Code: if (chkChildSeat.Checked) ;

Should Be: if (chkChildSeat.Checked)

The semicolon will denote end of the if statement. And hence all the accessory assignment statements will execute because they will not be considered inside the if block.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.