如何使用组合框事件处理程序以“数字向下”显示信息?

时间:2019-03-27 17:03:32

标签: c# winforms

当我更改组合框的成员时,我想上下更改数字的文本(或值)。我喜欢这样:

private void cbProductName_ValueMemberChanged(object sender, EventArgs e)
{
    if ((int)cbProductName.SelectedValue != 0)
    {
        using (UnitOfWork db = new UnitOfWork())
        {
            txtSalesPrice.ResetText();

            int id = Convert.ToInt32(cbProductName.SelectedValue); ;

            float salesPrice = db.FactorRepository.GetSalesPriceById(id);
            txtSalesPrice.Value = (decimal)(salesPrice);
            float discountPrice = float.Parse(txtDiscountPrice.Value.ToString());
            float finalPrice = FinalPrice.GetFinalPrice(salesPrice, discountPrice);
            txtFinalPrice.Value = (decimal)finalPrice;
            txtSalesPrice.Value = (decimal)salesPrice;

        }

txtSalePrice和txtFinalPrice是数字

但这不起作用。我要去哪里错了?

1 个答案:

答案 0 :(得分:0)

这是Microsoft的一个示例,与您要执行的操作类似。

// Check box to toggle decimal places to be displayed.

private void checkBox1_Click(Object sender, EventArgs e)
{
   /* If DecimalPlaces is greater than 0, set them to 0 and round the 
      current Value; otherwise, set DecimalPlaces to 2 and change the 
      Increment to 0.25. */
   if (numericUpDown1.DecimalPlaces > 0)
   {
      numericUpDown1.DecimalPlaces = 0;
      numericUpDown1.Value = Decimal.Round(numericUpDown1.Value, 0);
   }
   else
   {
      numericUpDown1.DecimalPlaces = 2;
      numericUpDown1.Increment = 0.25M;
   }
}

示例来自https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.numericupdown.value?view=netframework-4.7.2

我建议您查看更改“增量”是否对您的问题有所帮助。