将组合框与 switch 语句一起使用 c#

Using combobox with switch statement c#

我正在尝试使用 c# 中的 switch 语句根据组合框中的选项 select 在文本框中显示一些文本并在另一个文本框中显示数字。我已经将自己的 class 命名为 'Devices',并在 class 中创建了多个对象。我还为每个对象提供了几个属性(例如 DeviceName、DeviceRating)。但是,当我启动我的表单并从我的组合框中 select 一个选项时,第一个选项按计划工作(文本显示在相关文本框中)但所有选项都显示空白文本框。关于为什么会发生这种情况有什么想法吗?

这是我的属性代码:

 private void Form1_Load(object sender, EventArgs e)
    {

        // Gives properties of each object in the 'Devices' class.

        WashingMachine.DeviceName = "Washing Machine";
        WashingMachine.DeviceRating = 1200;


        Dishwasher.DeviceName = "Dishwasher";
        Dishwasher.DeviceRating = 1;
        ;

        OvenHob.DeviceName = "Oven/Hob";
        OvenHob.DeviceRating = 1;
        ;

        TowelRail.DeviceName = "Towel Rail";
        TowelRail.DeviceRating = 1;


        Hairdryer.DeviceName = "Hairdryer";
        Hairdryer.DeviceRating = 1;


        Shower.DeviceName = "Shower";
        Shower.DeviceRating = 1;

    }

这是我创建的 class 的代码:

class Devices
    {
        public string DeviceName;
        public int DeviceRating;
        public int UsedMins;
    }

这是我在 class 中创建新对象的代码:

// Creating new objects under the 'Devices' class.

    Devices WashingMachine = new Devices();
    Devices Dishwasher = new Devices();
    Devices OvenHob = new Devices();
    Devices TowelRail = new Devices();
    Devices Hairdryer = new Devices();
    Devices Shower = new Devices();
    Devices PhoneCharger = new Devices();
    Devices TabletCharger = new Devices();
    Devices ElectricBlanket = new Devices();

这里是 switch 语句,控制组合框中每种情况发生的情况(请注意,组合框列表与列出的对象的顺序相同):

 // Puts a Name and Power Rating into the textboxes based on the chosen device
    private void comboBox3_SelectedIndexChanged(object sender, EventArgs e)
    {
        switch (comboBox1.SelectedIndex)
        {
            case 0:
                DeviceName.Text = WashingMachine.DeviceName;
                PowerRating.Text = WashingMachine.DeviceRating.ToString();
                break;
            case 1:
                DeviceName.Text = Dishwasher.DeviceName;
                PowerRating.Text = Dishwasher.DeviceName.ToString();
                break;
            case 2:
                DeviceName.Text = OvenHob.DeviceName;
                PowerRating.Text = OvenHob.DeviceRating.ToString();
                break;
            case 3:
                DeviceName.Text = TowelRail.DeviceName;
                PowerRating.Text = TowelRail.DeviceRating.ToString();
                break;
            case 4:
                DeviceName.Text = Hairdryer.DeviceName;
                PowerRating.Text = Hairdryer.DeviceRating.ToString();
                break;
            case 5:
                DeviceName.Text = Shower.DeviceName;
                PowerRating.Text = Shower.DeviceRating.ToString();
                break;
        }


    }

这有点不匹配。您是否为 ComboBox 命名了 comboBox1 并使用了名为 comboBox3_SelectedIndexChanged() 的事件?

private void comboBox3_SelectedIndexChanged(object sender, EventArgs e)
{
    switch (comboBox1.SelectedIndex)
    {

不应该吗

private void comboBox3_SelectedIndexChanged(object sender, EventArgs e)
{
    switch (comboBox3.SelectedIndex)
    {

请原谅,如果偶数名称有意保留原样。另外,您的代码中真的有这些额外的分号吗?

Dishwasher.DeviceRating = 1;
        ;

OvenHob.DeviceRating = 1;
        ;