C# 如何在“中显示信息”;“数字上下”;使用组合框事件处理程序?
当我更改组合框的成员时,我想上下更改数字的文本(或值)。我喜欢这样:C# 如何在“中显示信息”;“数字上下”;使用组合框事件处理程序?,c#,winforms,C#,Winforms,当我更改组合框的成员时,我想上下更改数字的文本(或值)。我喜欢这样: private void cbProductName_ValueMemberChanged(object sender, EventArgs e) { if ((int)cbProductName.SelectedValue != 0) { using (UnitOfWork db = new UnitOfWork()) { txtSalesPrice.R
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是数字
但这是行不通的。我哪里做错了?这里有一个来自微软的例子,与您想要做的事情类似
// 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;
}
}
样品来自
我建议看看更改增量是否有助于解决您的问题 我想我遗漏了什么,你在哪里改变数字?我知道你在哪里设置TXTSALLEPRICE和txtFinalPrice,它们应该是NUMICUPPOWN控件吗?TXTSALLEPRICE和txtFinal Price是NUMERICUDOWNS
ValueMemberChanged
不是你认为的事件。如果控件有数据源,您可能需要SelectedValueChanged
,或者通常只需要SelectedIndexChanged
事件。正如您所说,我在运行prpgram时使用了selectedValueChange bu,编译器说“强制转换(id)无效”诸如此类。为什么?你说的是我看不见的代码。我也不熟悉你使用的工作类的单位,所以这可能也与此有关。