Show how much it costs in total

Asked

Viewed 68 times

-1

A program in Windows Form that I am creating , I want to click on the check of the chosen foods the full bar should show how much is getting, but always appears blank what should I do? Follow the code of what I did:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;

namespace WindowsFormsApp4
{
    public partial class FourCustomController : UserControl
    {
        float[] ValorProduto = new float[9];


        float somaTotal = 15;
        private string txtTotal;

        public FourCustomController()
        {
            InitializeComponent();
        }

        private void pictureBox3_Click(object sender, EventArgs e)
        {
            printDialog1.ShowDialog();
        }


         private void Salvar(object sender, EventArgs e)
        {
            saveFileDialog1.ShowDialog();
        }

        private void NaoNulo(object sender, CancelEventArgs e)
        {
            string caminho = saveFileDialog1.FileName;
            File.WriteAllText(caminho, textBox2.Text)  ;
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            ValorProduto[0] = 15f;
            somaTotal += ValorProduto[0];
        }

        private void checkBox2_CheckedChanged(object sender, EventArgs e)
        {
            ValorProduto[1] = 16f;
            somaTotal += ValorProduto[1];
        }

        private void checkBox3_CheckedChanged(object sender, EventArgs e)
        {
            ValorProduto[2] = 17f;
            somaTotal += ValorProduto[2];
        }

        private void checkBox4_CheckedChanged(object sender, EventArgs e)
        {
            ValorProduto[3] = 10f;
            somaTotal += ValorProduto[3];
        }

        private void checkBox5_CheckedChanged(object sender, EventArgs e)
        {
            ValorProduto[4] = 12f;
            somaTotal += ValorProduto[4];
        }

        private void checkBox6_CheckedChanged(object sender, EventArgs e)
        {
            ValorProduto[5] = 16f;
            somaTotal += ValorProduto[5];
        }

        private void checkBox7_CheckedChanged(object sender, EventArgs e)
        {
            ValorProduto[6] = 5f;
            somaTotal += ValorProduto[6];
        }

        private void checkBox8_CheckedChanged(object sender, EventArgs e)
        {
            ValorProduto[7] = 7f;
            somaTotal += ValorProduto[7];
        }

        private void checkBox9_CheckedChanged(object sender, EventArgs e)
        {
            ValorProduto[8] = 7f;
            somaTotal += ValorProduto[8];
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

            textBox1.Text = Convert.ToString(String.Format("#{0}" , somaTotal));


        }

        private void textBox1_Click(object sender, EventArgs e)
        {
            MessageBox.Show(textBox1.Text);
        }
    }
}

inserir a descrição da imagem aqui

  • Good evening. Tried debugging? See if the value is being assigned. Try using this.sum total instead of sum total.

  • I tried yes, it was not being assigned, because of this the error.

2 answers

1


I cleaned up your code a little bit and optimised some points:

namespace WindowsFormsApp4
{
    public partial class FourCustomController : UserControl
    {   
        float somaTotal = 15;

        public FourCustomController()
        {
            InitializeComponent();
            InicializaControls();
        }

        private void InicializaControls()
        {
            checkBox1.Tag = 15f;
            checkBox2.Tag = 16f;
            checkBox3.Tag = 17f;
            checkBox4.Tag = 10f;
            checkBox5.Tag = 12f;
            checkBox6.Tag = 16f;
            checkBox7.Tag = 5f;
            checkBox8.Tag = 7f;
            checkBox9.Tag = 7f;

            textBox1.Text = string.Format("{0:#,##0.00}", 0);
        }

        private void pictureBox3_Click(object sender, EventArgs e)
        {
            printDialog1.ShowDialog();
        }

        private void Salvar(object sender, EventArgs e)
        {
            saveFileDialog1.ShowDialog();
        }

        private void NaoNulo(object sender, CancelEventArgs e)
        {
            string caminho = saveFileDialog1.FileName;
            File.WriteAllText(caminho, textBox2.Text);
        }

        private void checkBox_CheckedChanged(object sender, EventArgs e)
        {
            var checkBox = sender as CheckBox;

            if(checkBox != null && checkBox.Tag != null)
                SomaTotal((float)checkBox.Tag, checkBox.Checked);
        }

        private void textBox1_Click(object sender, EventArgs e)
        {
            MessageBox.Show(textBox1.Text);
        }

        private void SomaTotal(float valor, bool blnEntrada)
        {
            if(blnEntrada)
                somaTotal += valor;
            else
                somaTotal -= valor;

            textBox1.Text = string.Format("{0:#,##0.00}", somaTotal);
        }
    }
}

For this solution to work, you must define the event checkBox_CheckedChanged in all CheckBox in the same way, that is to say when the value of any of the CheckBox is changed the evoked method should always be the same, because it makes the same procedure for all.

inserir a descrição da imagem aqui

  • My, my, my ! worked very well , I had tried before testing your code with everyone in Checkedchanged and n had gone , but now really with this improvement in logic and syntax worked properly, thank you even, I’m starting in WF and was studying some things ,

  • You are welcome! Take advantage and give an UP on the answer :)

0

You made a TextChanged for his textBox1, but it will never have the text changed since it is Enable = false.

I’m referring to this excerpt, where textBox1.Text only gets the value of SomaTotal if the event TextChanged occur:

private void textBox1_TextChanged(object sender, EventArgs e)
{
    textBox1.Text = Convert.ToString(String.Format("#{0}" , somaTotal));
}

Instead do the assignment here:

private void textBox1_Click(object sender, EventArgs e)
{
    textBox1.Text = Convert.ToString(String.Format("#{0}" , somaTotal));
    MessageBox.Show(textBox1.Text);
}

However your code has more problems. Its sum happens whenever the checkbox changes, no matter if it is selected or not.

If it was selected it must add and if it was removed subtracts, otherwise the value shown will be wrong.

Apparently you are using this application to study, try to read carefully everything that is in your code and use the Debugging, will help in your learning.

  • 1

    Thank you very much, I’m really using to study, it was my first application in WF and I wanted to test some things.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.