如何使用组合框事件处理程序显示"numeric up down"中的信息?



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

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 是数字

但这行不通。我哪里出错了?

下面是一个来自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://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.numericupdown.value?view=netframework-4.7.2

我建议看看更改增量是否有助于解决您的问题。

最新更新