首页 > 解决方案 > 我试图从组合框获取数据,但它不起作用

问题描述

我使用此代码..但什么也没发生..我已经尝试寻找解决方案,但它仍在发生..当我从 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();
            }
        }

标签: c#winforms

解决方案


它解决了你的问题吗?

// 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;

推荐阅读