Calculate more than one text box and display in a specific text box

Asked

Viewed 641 times

2

Good morning, you guys, I’m racking my brain on how to proceed with such logic;

I would like to take the value of these 4 textbox (txtDinheiro, txtCheque, txtCredito and txtDebito)

and in the end go doing the arithmetic process in txtRestant.

Example would be if the customer would pay in more than one form of payment (cash and debit) that the 2 subtract and show in the remainder until equal to the total amount.

Follow the image of the payment part of my PDV

inserir a descrição da imagem aqui

  • Put in some of your code, what have you tried or done? You’re having trouble converting string values?

  • The code this way works only pro txtbox check wanted to do the process in the other 3 types of payment also and result in the remainder: void Payment() { if (Convert.Todecimal(txtCheque.Text) <= value) { valcheque = Convert.Todecimal(txtCheque.Text); txtRestant.Text = String.Format("{0:c}", value - valcheque); } Else { txtRestant.Text = String.Format("{0:c}", remainder); } }

2 answers

1

You can create a class, representing the payment, and associate an object of that class in the DataBind of textBox. The calculations are all performed in the class, Form only takes care of the display.

I made an example:

 public partial class Form4 : Form
    {
        Pagamento objPg;



        public Form4()
        {
            InitializeComponent();
            objPg = new Pagamento();


            textBoxDinheiro.DataBindings.Add("Text", objPg, "Dinheiro", true, DataSourceUpdateMode.OnPropertyChanged);
            textBoxCredito.DataBindings.Add("Text", objPg, "Credito", true, DataSourceUpdateMode.OnPropertyChanged);
            textBoxDebito.DataBindings.Add("Text", objPg, "Debito", true, DataSourceUpdateMode.OnPropertyChanged);
            textBoxCheque.DataBindings.Add("Text", objPg, "Cheque", true, DataSourceUpdateMode.OnPropertyChanged);
            textBoxTotal.DataBindings.Add("Text", objPg, "TotalVenda", true, DataSourceUpdateMode.OnPropertyChanged);
            textBoxTotalPago.DataBindings.Add("Text", objPg, "TotalPago", true, DataSourceUpdateMode.OnPropertyChanged);
            textBoxRestante.DataBindings.Add("Text", objPg, "Restante", true, DataSourceUpdateMode.OnPropertyChanged);
            textBoxTroco.DataBindings.Add("Text", objPg, "Troco", true, DataSourceUpdateMode.OnPropertyChanged);


        }
    }


    public class Pagamento
    {
        public decimal Dinheiro { get; set; }
        public decimal Credito { get; set; }
        public decimal Debito { get; set; }
        public decimal Cheque { get; set; }
        public decimal TotalVenda { get; set; }
        public decimal TotalPago { get { return Dinheiro + Credito + Debito + Cheque; } }
        public decimal Restante { get { return TotalPago > TotalVenda ? 0 : TotalVenda - TotalPago; } }
        public decimal Troco { get { return TotalPago > TotalVenda ? TotalPago - TotalVenda : 0; } }
    }
}

Upshot:

inserir a descrição da imagem aqui

There is also how to format for currency: https://msdn.microsoft.com/en-us/library/system.windows.forms.binding.format%28v=vs.110%29.aspx? f=255&Mspperror=-2147217396

Tip: Do not use 'ESC' (Escape) to complete the sale. It seems to me something like "Press Delete to Record"

0

I don’t know what program or language you’re doing but here’s my solution.

I don’t know much about the bills, but I assume the debt is what he owes in cash or credit card, so first let’s add up all the ways he might have used to pay, which will be equal to the amount he’s paying. And the next we’ll subtract what he owes from what he’s paying for change.

Thus:

int a;
int b;
int c;
int d;
int resultado;
int valor_pago;
int valor_total_a_pagar;

txtDinheiro.Text = a;
txtCheque.Text = b;
txtCredito.Text = c;
txtDebito.Text = d;

a + b + c = valor_pago;
a + b + c - valor_total_a_pagar = resultado;

valorpago.Text = valor_pago;
troco.Text = resultado;
label1.Text = valor_total_a_pagar;
  • 1

    and if the sale is R$10.50 ? You cannot assign a variable int to a string Text, That: valorpago.Text = valor_pago; will give build error. Thanks for the intention to help, but this code will not work.

  • Replace int with double

  • correct is decimal. Note this: https://answall.com/a/59016/69359

Browser other questions tagged

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