简体   繁体   English

C#将输入的文本框值减去标签值

[英]C# Subracting Inputted Textbox Value to the Label value

I'm building these POS Desktop application in Windows form and it's almost done but i'm having problem in processing the customer bills. 我正在以Windows形式构建这些POS桌面应用程序,并且几乎完成了,但是在处理客户账单时遇到了问题。

To be specific: 再具体一点:
SIMSProduct Usercontrol SIMS产品用户控件

  • cart.lbl_price.Text = lbl_totalprice.Text; cart.lbl_price.Text = lbl_totalprice.Text;
    First the total of the customer bought is in lbl_totalprice.Text 首先,购买的客户总数在lbl_totalprice.Text中
    Next that total is used to ProcessCart Form which is the cart.lbl_price.Text 接下来,将总计用于ProcessCart表单,即cart.lbl_price.Text
  • lbl_price.Text is now successfully getting the value of lbl_totalprice.Text lbl_price.Text现在成功获取了lbl_totalprice.Text的值

ProcessCart Form ProcessCart表格

  • txt_amount(Textbox) is where user input the pay value of customer that should be subracted to the lbl_price.Text which is fail. txt_amount(Textbox)是用户输入应该付诸于lbl_price.Text失败的客户的工资值的位置。
  • lbl_totalprice.Text corresponds to the output of subracted lbl_price and txt_amount which is fail too lbl_totalprice.Text对应于同样失败的lbl_price和txt_amount的输出

Note: lbl corresponds to Windows form Label 注意:lbl对应于Windows窗体标签

The problem is when i tried to input to my txt_amount, let's say i input 5000 and that 5000 is not subracting the value of lbl_price, also the lbl_totalprice is equal to what i type to txt_amount. 问题是当我尝试输入txt_amount时,假设我输入了5000,而5000并未计算lbl_price的值,lbl_totalprice也等于我输入txt_amount的值。 Below these code, What I've done wrong here ?, something that i should not made? 在这些代码下面,我在这里做错了什么?我不应该做的事情? or i forgot something ?. 还是我忘记了什么? I hope someone would be able to help in these matter. 我希望有人能够在这些问题上提供帮助。 Thank you 谢谢

    public partial class SIMSProduct : UserControl
{
    ITEMCount item;
    ProcessCart cart;
    public SIMSProduct()
    {
        InitializeComponent();         
    }

 private void btn_process_Click(object sender, EventArgs e)
    {
        cart = new ProcessCart();
        cart.Show();
        cart.lbl_price.Text = lbl_totalprice.Text; 
    }
}

public partial class ProcessCart : Form
{     
    public ProcessCart()
    {
        InitializeComponent();
    }
 private void txt_amount_TextChanged(object sender, EventArgs e)
    {
        int value1;
        int value2;
        decimal value3;
        if (int.TryParse(lbl_price.Text.Trim(), out value1))
        {
            Total = Convert.ToInt32(lbl_price.Text);            
        }
        if (int.TryParse(txt_amount.Text.Trim(), out value2))
        {

            Paid = Convert.ToInt32(txt_amount.Text);          
        }
        lbl_totalprice.Text = (Paid - Total).ToString();
}

I think your issue is a casting issue. 我认为您的问题是决定性的问题。 Looks like you're attempting to cast a decimal to an integer value. 看起来您正在尝试将小数转换为整数值。 Try replacing with this code. 尝试用此代码替换。

    public partial class SIMSProduct : UserControl
{
    ITEMCount item;
    ProcessCart cart;
    public SIMSProduct()
    {
        InitializeComponent();         
    }

 private void btn_process_Click(object sender, EventArgs e)
    {
        cart = new ProcessCart();
        cart.Show();
        cart.lbl_price.Text = lbl_totalprice.Text; 
    }
}

public partial class ProcessCart : Form
{     
    public ProcessCart()
    {
        InitializeComponent();
    }
 private void txt_amount_TextChanged(object sender, EventArgs e)
    {
        decimal value1;
        decimal value2;
        decimal value3;
        if (decimal.TryParse(lbl_price.Text.Trim(), out value1))
        {
            Total = Convert.ToDecimal(lbl_price.Text);            
        }
        if (decimal.TryParse(txt_amount.Text.Trim(), out value2))
        {

            Paid = Convert.ToDecimal(txt_amount.Text);          
        }
        if (decimal.TryParse(lbl_totalprice.Text.Trim(), out value3))
        {
            Change = Convert.ToDecimal(lbl_totalprice.Text);
        }
        Change = Paid - Total;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM