Click here to Skip to main content
15,881,172 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
So I am working on a small project and I have buttons lots of buttons. These buttons fill in a cost in a listbox and update a running total in a textbox below it. What I want to happen is the resulting textbox to be added with user values from another textbox when a radio button is selected. the code below is the button to add the product to the listbox and textbox and also the code for the radio button.


C#
private void btn_product_Click(object sender, EventArgs e)
    {
        listBox1.Items.Add("01.25");
        Decimal total = 0;
        foreach (var v in listBox1.Items)
        {
            total += Convert.ToDecimal(v);
        }
        resultTextbox.Text = total.ToString();

    }


    private void rdCash_CheckedChanged(object sender, EventArgs e)
    {
        if (rdCash.Checked)
        {
            decimal total = Convert.ToDecimal(resultTextbox.Text);
            decimal paid = Convert.ToDecimal(txtPaid.Text);
            decimal results = Decimal.Add(total, paid);
            txtChange.Text = results.ToString();
        }



    }


Im getting an error at the decimal total = Convert.ToDecimal(resultTextbox.Text); line that says that it is not a correctly formatted value any help to solve the problem I am having would be appreciated and I hope it is just something I overlooked thank you!



Edit: I should mention that there is more to the if statement i just have not added it yet as I am stuck on this problem. Also Visual Studio shows no errors until I run to this line in the program.




IMAGES OF THE PROJECT AND ERROR:

http://imgur.com/50R001b[^]
http://imgur.com/ej6i2qN[^]




there is probably a more efficient way to accomplish this register than how I am doing it so please feel free to add input on what I could change to keep it simple as compared to what I have.
Posted
Updated 9-Aug-14 9:05am
v4
Comments
Atyant Srivastava 9-Aug-14 0:16am    
your code is working fine at my end, please paste the snapshot of the error.
Are you entering some string in the textbox resultTextbox.

Or you can check the resultTextbox.Text value whether it is null or blank before converting it into decimal.

hi,

I think you are not checking resultTextbox.Text for null or empty try this before assigning value to total(you can do the changes accordingly):

C#
private void rdCash_CheckedChanged(object sender, EventArgs e)
        {
            if (rdCash.Checked)
            {
                //you can do like this:(Option 1)
                if (string.IsNullOrEmpty(resultTextbox.Text) != true )
                {
                 decimal total =  Convert.ToDecimal(resultTextbox.Text);
                }
                else
                {
                 decimal total = Convert.ToDecimal("0.00"); 
                }

                //or same thing you can check in one line like this:(Option 2)
                decimal total = string.IsNullOrEmpty(resultTextbox.Text) ?  
                0: Convert.ToDecimal(resultTextbox.Text);

                decimal paid = Convert.ToDecimal(txtPaid.Text);
                decimal results = Decimal.Add(total, paid);
                txtChange.Text = results.ToString();
            }



        }
 
Share this answer
 
v5
Comments
Member 10147675 9-Aug-14 14:56pm    
Same problem, it runs fine until I try to interact with the radio button then the program fails. I took some screenshots so that it is a bit easier to understand. basically i hit btn_product and it fills the information into the listbox and then adds the running total to the resultTextbox. Once the user adds how much cash was used to pay the total price into txtPaid they would check the CASH radio button and resultsTextbox and txtPaid textboxes will be added together and display the amount of change the customer will recieve.

I have been trying a couple different methods to accomplish this for this little "register" so if there is a more efficent way to make a register setup than how i have it it would be much appreciated.
resultTextbox.Text may not be a valid decimal number.
Use Decimal.TryParse[^] to ensure you are entering valide decimal numbers before adding them together.
 
Share this answer
 
Comments
Member 10147675 9-Aug-14 15:04pm    
I have tried this and it still threw the same exception.

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