none
I want to display the total (price of each product)which are in the listbox in a label1 RRS feed

  • Question

  • As below I want the total to be displayed in the label1. How can I do that?

    This is my code

    namespace TEST
    {
        public partial class Form1 : Form
        {
            SqlConnection con = new SqlConnection(@"Data Source=...");
            public Form1()
            {
                InitializeComponent();
            }
            SqlCommand cmd;
            SqlDataAdapter da;
            DataSet ds;
            private int i;
            private IFormatProvider async;
    
            private void Form1_Load(object sender, EventArgs e)
            {
                con = new SqlConnection(@"Data Source=...");
                cmd = new SqlCommand("select * from Producten", con);
                da = new SqlDataAdapter(cmd);
                DataSet ds = new DataSet();
                da.Fill(ds);
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
    
                    comboBox1.Items.Add(ds.Tables[0].Rows[i][0] + " | " + ds.Tables[0].Rows[i][1] + " | € " + ds.Tables[0].Rows[i][2]);
    
                }
            }
    
            private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
            {
                listBox1.Items.Add(comboBox1.Text);
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                string revomeItemFromList = listBox1.Text;
                listBox1.Items.Remove(revomeItemFromList);
            }
    
            private void button2_Click(object sender, EventArgs e)
            {
                listBox1.Items.Clear();
            }
    
            private void label1_Click(object sender, EventArgs e)
            {
    
            }
        }
    }

    I would be very thankfull if somebody can help me out


    Saturday, December 29, 2018 12:50 PM

Answers

  • Hi,

    You can do the calculation of the sum in the loop. The modified code is shown as follows.

        private void Form1_Load(object sender, EventArgs e)
        {
            // code omitted
            int total = 0;
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                comboBox1.Items.Add(ds.Tables[0].Rows[i][0] + " | " + ds.Tables[0].Rows[i][1] + " | € " + ds.Tables[0].Rows[i][2]);
                total += Convert.ToInt32(ds.Tables[0].Rows[i][2]);
            }
            label1.Text = "€ " + total.ToString();
        }
    

    Regards,

    Kyle


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by WilliamAshoti Wednesday, January 2, 2019 7:50 AM
    Monday, December 31, 2018 1:45 AM
    Moderator
  • Hi,

    Here is the code you can refer to.

        private void Form1_Load(object sender, EventArgs e)
        {
            // code omitted
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                comboBox1.Items.Add(ds.Tables[0].Rows[i][0] + " | " + ds.Tables[0].Rows[i][1] + " | € " + ds.Tables[0].Rows[i][2]);
            }
        }
    
        int total = 0;
    
        private void Add_Click(object sender, EventArgs e)
        {
            listBox1.Items.Add(comboBox1.Text);
            GetTotal();
        }
    
        private void DeleteAll_Click(object sender, EventArgs e)
        {
            listBox1.Items.Clear();
            GetTotal();
        }
    
        private void Delete_Click(object sender, EventArgs e)
       {
            if (this.listBox1.SelectedIndex != -1)
            {
                this.listBox1.Items.RemoveAt(this.listBox1.SelectedIndex);
            }
            GetTotal();
        }
    
        private void GetTotal()
        {
            total = 0;
            foreach (var item in listBox1.Items)
            {
                int index = item.ToString().IndexOf('€');
                //string s = item.ToString().Substring(index + 1, item.ToString().Length - (index + 1)).Trim();
                //int price = Convert.ToInt32(s);
                int price = Convert.ToInt32(item.ToString().Substring(index + 1, item.ToString().Length - (index + 1)).Trim());
                total += price;
            }
            label1.Text = "€ " + total.ToString();
        }

    Result:

    Regards,

    Kyle


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, December 31, 2018 8:37 AM
    Moderator
  • Hi Kyle, 

    Its working for now, but the label shows now always the total of the combobox items which is in this case is 17. What I want is the total of the added items in the listbox see below the picture. For example when I add 4 items of Margherita the total shoud be in this case €40,00 and by deleting one of the items should be €30,00. In case we delete the all the total should be €0,00. I hope my explanaition should understand what really want to achieve. Thanks for your help in advance 

    • Marked as answer by WilliamAshoti Wednesday, January 2, 2019 7:50 AM
    Monday, December 31, 2018 4:35 AM
  • Hello Kyle, 

    First happy new year and all the best for 2019. Thanks for sending me the code. It's working now as expected. I'am very thankfull for your help and patience. All the best

    • Marked as answer by WilliamAshoti Wednesday, January 2, 2019 7:49 AM
    Tuesday, January 1, 2019 10:16 PM
  • Hi WilliamAshoti,

    Thank you for your blessing, and the same wish to you.

    If the issue has been solved, please post "Mark as answer" to the appropriate answer , so that it will help other members to find solution quickly if they faces similar issue.

    Regards,

    Kyle


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by WilliamAshoti Wednesday, January 2, 2019 7:49 AM
    Wednesday, January 2, 2019 1:26 AM
    Moderator

All replies

  • Hi,

    You can do the calculation of the sum in the loop. The modified code is shown as follows.

        private void Form1_Load(object sender, EventArgs e)
        {
            // code omitted
            int total = 0;
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                comboBox1.Items.Add(ds.Tables[0].Rows[i][0] + " | " + ds.Tables[0].Rows[i][1] + " | € " + ds.Tables[0].Rows[i][2]);
                total += Convert.ToInt32(ds.Tables[0].Rows[i][2]);
            }
            label1.Text = "€ " + total.ToString();
        }
    

    Regards,

    Kyle


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by WilliamAshoti Wednesday, January 2, 2019 7:50 AM
    Monday, December 31, 2018 1:45 AM
    Moderator
  • Hi Kyle, 

    Its working for now, but the label shows now always the total of the combobox items which is in this case is 17. What I want is the total of the added items in the listbox see below the picture. For example when I add 4 items of Margherita the total shoud be in this case €40,00 and by deleting one of the items should be €30,00. In case we delete the all the total should be €0,00. I hope my explanaition should understand what really want to achieve. Thanks for your help in advance 

    • Marked as answer by WilliamAshoti Wednesday, January 2, 2019 7:50 AM
    Monday, December 31, 2018 4:35 AM
  • Hi,

    Here is the code you can refer to.

        private void Form1_Load(object sender, EventArgs e)
        {
            // code omitted
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                comboBox1.Items.Add(ds.Tables[0].Rows[i][0] + " | " + ds.Tables[0].Rows[i][1] + " | € " + ds.Tables[0].Rows[i][2]);
            }
        }
    
        int total = 0;
    
        private void Add_Click(object sender, EventArgs e)
        {
            listBox1.Items.Add(comboBox1.Text);
            GetTotal();
        }
    
        private void DeleteAll_Click(object sender, EventArgs e)
        {
            listBox1.Items.Clear();
            GetTotal();
        }
    
        private void Delete_Click(object sender, EventArgs e)
       {
            if (this.listBox1.SelectedIndex != -1)
            {
                this.listBox1.Items.RemoveAt(this.listBox1.SelectedIndex);
            }
            GetTotal();
        }
    
        private void GetTotal()
        {
            total = 0;
            foreach (var item in listBox1.Items)
            {
                int index = item.ToString().IndexOf('€');
                //string s = item.ToString().Substring(index + 1, item.ToString().Length - (index + 1)).Trim();
                //int price = Convert.ToInt32(s);
                int price = Convert.ToInt32(item.ToString().Substring(index + 1, item.ToString().Length - (index + 1)).Trim());
                total += price;
            }
            label1.Text = "€ " + total.ToString();
        }

    Result:

    Regards,

    Kyle


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, December 31, 2018 8:37 AM
    Moderator
  • Hello Kyle, 

    First happy new year and all the best for 2019. Thanks for sending me the code. It's working now as expected. I'am very thankfull for your help and patience. All the best

    • Marked as answer by WilliamAshoti Wednesday, January 2, 2019 7:49 AM
    Tuesday, January 1, 2019 10:16 PM
  • Hi WilliamAshoti,

    Thank you for your blessing, and the same wish to you.

    If the issue has been solved, please post "Mark as answer" to the appropriate answer , so that it will help other members to find solution quickly if they faces similar issue.

    Regards,

    Kyle


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by WilliamAshoti Wednesday, January 2, 2019 7:49 AM
    Wednesday, January 2, 2019 1:26 AM
    Moderator
  • Heey Keyle, 

    Iam facing now an issue for the following changes in the database. I have changed the datatype of the price from int to decimal (18,2). As you can see below in the picture. The price has now two decimals. When I run the program the following error appear in the int price of the GetTotal(). I have also attached See below the FormatExceptionUnhandeld

    Can you help me out how to solve this problem?

    Many thanks

    Friday, January 4, 2019 9:26 PM
  • Hi William,

    To solve this issue, you need to set the data type of "total" and "price" to "double", and modify the following code like this:

    double price = Convert.ToDouble(item.ToString().Substring(index + 1, item.ToString().Length - (index + 1)).Trim())

    Besides, one thread just discuss one problem. It would be appreciated if you can create another thread to post this issue.

    Thank you for your understanding.

    Regards,

    Kyle


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, January 7, 2019 1:31 AM
    Moderator