c#/将选定的组合框项目添加到列表框中

时间:2016-12-18 19:55:57

标签: c# winforms visual-studio combobox listbox

我需要在Visual Studio中使用Windows窗体应用程序创建一个小应用程序,该应用程序将列出列表框中多个组合框中的选定项目,然后添加每个选择的总价格并放入文本框中。问题是在选择时项目未被添加到列表中。我在之前的应用程序中遇到了类似的问题,并意识到我在方法声明中使用SelectedIndex而不是SelectedIndexChanged。这次情况并非如此。我还是C#的新手,所以我为任何草率的代码道歉。

lbDishes是列表框的设计名称,txtTotal是要显示的总价格的文本框。

namespace Exercise_15._9
{
public partial class ResBillCalc : Form
{
    public ResBillCalc()
    {
        InitializeComponent();
    }

    // Method to handle the SelectedIndexChanged event of cbBeverage       

    private void cbBeverage_SelectedIndexChanged(object sender,
        EventArgs e)
    {
        // add selected item to ListBox
        lbDishes.Items.Add(cbBeverage.Text);

        //Switch to appropriate choice to get subtotal price
        switch (cbBeverage.SelectedIndex)
        {
            case 0:
                txtTotal.Text =
                    Convert.ToString(Convert.ToDouble(txtTotal.Text) + 1.95);
                break;

            case 1:
                txtTotal.Text =
                    Convert.ToString(Convert.ToDouble(txtTotal.Text) + 1.50);
                break;

            case 2:
                txtTotal.Text =
                    Convert.ToString(Convert.ToDouble(txtTotal.Text) + 1.25);
                break;

            case 3:
                txtTotal.Text =
                    Convert.ToString(Convert.ToDouble(txtTotal.Text) + 2.95);
                break;

            case 4:
                txtTotal.Text =
                    Convert.ToString(Convert.ToDouble(txtTotal.Text) + 2.50);
                break;

            case 5:
                txtTotal.Text =
                    Convert.ToString(Convert.ToDouble(txtTotal.Text) + 1.50);
                break;
        }
    }

    // Method to handle SelectedIndexChanged event of cbAppetizer
    private void cbAppetizer_SelectedIndexChanged (object sender,
        EventArgs e)
    {
        // add selected item to ListBox
        lbDishes.Items.Add(cbAppetizer.Text);

        //Switch to appropriate choice to get subtotal
        switch (cbAppetizer.SelectedIndex)
        {
            case 0:
                txtTotal.Text =
                    Convert.ToString(Convert.ToDouble(txtTotal.Text) + 5.95);
                break;

            case 1:
                txtTotal.Text =
                    Convert.ToString(Convert.ToDouble(txtTotal.Text) + 6.95);
                break;

            case 2:
                txtTotal.Text =
                    Convert.ToString(Convert.ToDouble(txtTotal.Text) + 8.95);
                break;

            case 3:
                txtTotal.Text =
                    Convert.ToString(Convert.ToDouble(txtTotal.Text) + 8.95);
                break;

            case 4:
                txtTotal.Text =
                    Convert.ToString(Convert.ToDouble(txtTotal.Text) + 10.95);
                break;

            case 5:
                txtTotal.Text =
                    Convert.ToString(Convert.ToDouble(txtTotal.Text) + 12.95);
                break;

            case 6:
                txtTotal.Text =
                    Convert.ToString(Convert.ToDouble(txtTotal.Text) + 6.95);
                break;
        }
    }

    // Method to handle SelectedIndexChanged event of cbMainCourse
    private void cbMainCourse_SelectedIndexChanged (object sender,
        EventArgs e)
    {
        lbDishes.Items.Add(cbMainCourse.Text);

        //Switch to appropriate choice to get subtotal
        switch (cbMainCourse.SelectedIndex)
        {
            case 0:
                txtTotal.Text =
                    Convert.ToString(Convert.ToDouble(txtTotal.Text) + 13.95);
                break;

            case 1:
                txtTotal.Text =
                    Convert.ToString(Convert.ToDouble(txtTotal.Text) + 13.95);
                break;

            case 2:
                txtTotal.Text =
                    Convert.ToString(Convert.ToDouble(txtTotal.Text) + 11.95);
                break;

            case 3:
                txtTotal.Text =
                    Convert.ToString(Convert.ToDouble(txtTotal.Text) + 19.95);
                break;

            case 4:
                txtTotal.Text =
                    Convert.ToString(Convert.ToDouble(txtTotal.Text) + 20.95);
                break;

            case 5:
                txtTotal.Text =
                    Convert.ToString(Convert.ToDouble(txtTotal.Text) + 18.95);
                break;

            case 6:
                txtTotal.Text =
                    Convert.ToString(Convert.ToDouble(txtTotal.Text) + 13.95);
                break;

            case 7:
                txtTotal.Text =
                    Convert.ToString(Convert.ToDouble(txtTotal.Text) + 14.95);
                break;

            case 8:
                txtTotal.Text =
                    Convert.ToString(Convert.ToDouble(txtTotal.Text) + 15.95);
                break;
        }
    }

    // Method to handle SelectedIndexChange event of cbDessert
    private void cbDessert_SelectedIndexChanged(object sender,
        EventArgs e)
    {
        // Add selected text to ListBox
        lbDishes.Items.Add(cbDessert.Text);

        // Switch to choice to get subtotal
        switch (cbDessert.SelectedIndex)
        {
            case 0:
                txtTotal.Text =
                    Convert.ToString(Convert.ToDouble(txtTotal.Text) + 5.95);
                break;

            case 1:
                txtTotal.Text =
                    Convert.ToString(Convert.ToDouble(txtTotal.Text) + 3.95);
                break;

            case 2:
                txtTotal.Text =
                    Convert.ToString(Convert.ToDouble(txtTotal.Text) + 5.95);
                break;

            case 3:
                txtTotal.Text =
                    Convert.ToString(Convert.ToDouble(txtTotal.Text) + 4.95);
                break;

            case 4:
                txtTotal.Text =
                    Convert.ToString(Convert.ToDouble(txtTotal.Text) + 5.95);
                break;
        }
    }

}
}

0 个答案:

没有答案