c# - 我试图从组合框获取数据,但它不起作用
问题描述
我使用此代码..但什么也没发生..我已经尝试寻找解决方案,但它仍在发生..当我从 numericUpDown 更改值时,PriceTxt.Text 不会改变..我希望有人能解释我为什么会这样正在发生以及如何解决它..我不知道..
private void quantityTxt_ValueChanged(object sender, EventArgs e)
{
string selected = this.DescTxt.GetItemText(this.DescTxt.SelectedItem);
if (DescTxt.Text == "SET A -AYAM GORENG + FRIES + AIR")
{
MessageBox.Show((5 * quantityTxt.Value).ToString());
PriceTxt.Text = (5 * quantityTxt.Value).ToString();
}
else if (selected == "SET B -AYAM GORENG + NUGGETS + AIR")
{
MessageBox.Show((10 * quantityTxt.Value).ToString());
PriceTxt.Text = (10 * quantityTxt.Value).ToString();
}
else if (selected == "SET C -AYAM GORENG + MEATBALL + AIR")
{
MessageBox.Show((15 * quantityTxt.Value).ToString());
PriceTxt.Text = (15 * quantityTxt.Value).ToString();
}
}
解决方案
它解决了你的问题吗?
// Form.Load event. Here ComboBox fills with some values
// and subscription to ComboBox.SelectedValueChanged event provided
private void OnFormLoad(object sender, EventArgs e)
{
string[] items = new[]
{
"SET A -AYAM GORENG + FRIES + AIR",
"SET B -AYAM GORENG + FRIES + AIR",
"SET C -AYAM GORENG + FRIES + AIR"
};
myComboBox.Items.AddRange(items);
// Subscribe to SelectedValueChanged event
myComboBox.SelectedValueChanged += OnSelectedValueChange;
}
// Our SelectedValueChanged handler
private void OnSelectedValueChange(object sender, EventArgs e)
{
// Getting content of new selected in ComboBox item
string selectedItem = myComboBox.SelectedItem.ToString();
int switchableValue; // Your 5, 10, 15
// Check in proper way
switch (selectedItem)
{
case "SET A -AYAM GORENG + FRIES + AIR":
switchableValue = 5;
break;
case "SET B -AYAM GORENG + FRIES + AIR":
switchableValue = 10;
break;
case "SET C -AYAM GORENG + FRIES + AIR":
switchableValue = 15;
break;
default:
// Maybe reset some values if needed before return
return;
}
// Get your neccesarry result value, converted to string through interpolation
string someResult = $"{switchableValue * quantityTxt.Value}";
// Do work with result value
MessageBox.Show(someResult);
PriceTxt.Text = someResult;
}
UPD:我的示例处理 ComboBox 项目更改,而不是 NumericUpDown 值更改(至少我是盲人)。如果您需要对 NumericUpDown 控件的依赖 - 您应该将订阅更改为 NumericUpDown.ValueChanged 并添加对空 ComboBox.SelectedItem 值的检查:
private void OnFormLoad(object sender, EventArgs e)
{
// Still fill items up there...
// Instead of ComboBox.SelectedValueChanged event subscription
// myComboBox.SelectedValueChanged += OnSelectedValueChange;
// Subscribe to NumericUpDown.ValueChanged event:
quantityTxt.ValueChanged += OnQuantityValueChange;
}
private void OnQuantityValueChange(object sender, EventArgs e)
{
// Add check for null to ComboBox.SelectedItem property
// by appeding '?' at end of it
string selectedItem = myComboBox.SelectedItem?.ToString();
// .. rest of code is the same.
// If ComboBox.SelectedItem would be null -
// method would break in switch statement by 'default' behaviour.
}
或者您可以将 ComboBox.SelectedValueChanged 和 NumericUpDown.ValueChanged 事件组合到一个处理程序中:
private void OnDescTxtOrQuantityTxtChange(object sender, EventArgs e)
{
string selectedItem = myComboBox.SelectedItem?.ToString();
int switchableValue;
switch (selectedItem)
{
case "SET A -AYAM GORENG + FRIES + AIR":
switchableValue = 5;
break;
case "SET B -AYAM GORENG + FRIES + AIR":
switchableValue = 10;
break;
case "SET C -AYAM GORENG + FRIES + AIR":
switchableValue = 15;
break;
default:
// MessageBox.Show("You didn't select Description!")
return;
}
// Add check for NumericUpDown '0' value if needed,
// cuz multiplying 'switchableValue' (5/10/15) to 0 will result with 0
if (quantityTxt.Value == 0)
{
// MessageBox.Show("You didn't select Quantity!")
return;
}
string someOutput = $"{switchableValue * quantityTxt.Value}";
MessageBox.Show(someOutput);
PriceTxt.Text = someOutput;
}
并订阅这两个事件:
myComboBox.SelectedValueChanged += OnDescTxtOrQuantityTxtChange;
quantityTxt.ValueChanged += OnDescTxtOrQuantityTxtChange;
推荐阅读
- java - 即使我没有写,JFrame 也是可见的:frame.setVisible(true)
- javascript - 在 laravel 5.5 中将多个数据表临时数据保存到数据库中
- sql - Oracle PL/SQL-将重复行移入 1 行
- flutter - Flutter:是否可以根据设备大小自动缩放(整个)ui?
- python - 如何在不向 python 路径添加 eclipse 洞察力的情况下将 PyDev 调试器与 python 嵌入(zip 文件)一起使用?
- django - touch 命令杀死 uWSGI 服务
- r - 按 id 对数据框中的一个变量进行排序
- c++ - 使用保存的参数进行立体相机校正
- javascript - 创建一个 html 页面,添加 2 个文本框,用户将在其中输入 2 个数字:n 和 m
- node.js - React 如何添加自定义 js 文件 web 应用程序