【问题标题】:Using combobox with switch statement c#使用带有 switch 语句的组合框 c#
【发布时间】:2015-05-06 16:04:11
【问题描述】:

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

这是我的属性代码:

 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 Devices
    {
        public string DeviceName;
        public int DeviceRating;
        public int UsedMins;
    }

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

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


    }

【问题讨论】:

  • 您可以将项目添加到组合框,使用显示成员显示设备名称并将文本框设置为((Devices)comboBox1.SelectedItem).DeviceName
  • 为什么事件命名为comboBox3_SelectedIndexChanged,而switch 使用comboBox1.SelectedIndex。 `comboBox1 & comboBox3?
  • 试试 SelectionChangedCommitted 看看你是否得到不同的结果。还使用 MessageBox.Show(Dishwasher.DeviceName) 等来检查字符串值。另外,您是否在表单的构造函数中初始化了设备?
  • @Saagar Elias Jacky 我想你明白了!当 ComboBox3 的 SelectedIndexChanged 事件被触发时,它会查看 ComboBox1 并获取第一个项目。它没有解释为什么文本框会被清空......

标签: c# class combobox switch-statement


【解决方案1】:

这有点不匹配。您是否将 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;
        ;

【讨论】:

  • 感谢您的帮助,我知道这很可能是微不足道的。分号确实是错别字,除了需要的时候我不会用!
猜你喜欢
  • 1970-01-01
  • 2017-03-14
  • 1970-01-01
  • 2022-06-22
  • 2015-12-23
  • 1970-01-01
  • 2015-05-01
  • 1970-01-01
  • 2020-12-01
相关资源
最近更新 更多