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:
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"
Put in some of your code, what have you tried or done? You’re having trouble converting string values?
– lazyFox
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); } }
– Carlos Henrique