使用枚举器切换语句

时间:2014-08-14 22:03:35

标签: c# winforms switch-statement

我正在尝试在Visual Studio C#中开发一个酒店管理系统。我有一个组合框与酒店房间类型和一个价格的文本框。我希望当用户选择房间类型时,文本框会显示房间的价格。我试图使用switch语句,但它给了我一个Stackoverflow异常。有人可以请帮助。感谢

enum RoomType
{
    DoubleBB,
    SingleFullboard,
    SingleBB,
    DoubleFullboard,
    TwinBB,
    TwinFullboard
}

private void comboBoxroomtype_SelectedIndexChanged(object sender, EventArgs e)
{
    RoomType myChoice = new RoomType();
    /*RoomType myChoice2 = RoomType.TwinFullboard;
    RoomType myChoice3 = RoomType.SingleBB;
    RoomType myChoice4 = RoomType.SingleFullboard;
    RoomType myChoice5 = RoomType.DoubleBB;
    RoomType myChoice6 = RoomType.DoubleFullboard;*/

    switch(myChoice)
    {
        case RoomType.TwinBB:
            comboBoxroomtype.Text = "TwinBB";
            txtroomrate.Text = "55 USD";
            goto case RoomType.TwinFullboard;
            break;

      case RoomType.TwinFullboard:
            comboBoxroomtype.Text = "TwinFullboard";
            txtroomrate.Text = "65 USD";
            goto case RoomType.DoubleBB;
            break;

        case RoomType.DoubleBB:
            comboBoxroomtype.Text = "DoubleBB";
            txtroomrate.Text = "50 USD";
            goto case RoomType.DoubleFullboard;
            break;

        case RoomType.DoubleFullboard:
            comboBoxroomtype.Text = "DoubleFullboard";
            txtroomrate.Text = "60 USD";
            goto case RoomType.SingleBB;
            break;

        case RoomType.SingleBB:
            comboBoxroomtype.Text = "SingleBB";
            txtroomrate.Text = "40 USD";
            goto case RoomType.SingleFullboard;
            break;

        case RoomType.SingleFullboard:
            comboBoxroomtype.Text = "SingleFullboard";
            txtroomrate.Text = "50 USD";
            break;
        default:
            comboBoxroomtype.Text = "";
            txtroomrate.Text = "";
            break;
    }
}

4 个答案:

答案 0 :(得分:3)

您可以尝试像这样绑定:

首先创建一个类,用于封装每种类型的房间的映射及其所需的价格,例如:

    public enum RoomType
    {
        DoubleBB,
        SingleFullboard,
        SingleBB,
        DoubleFullboard,
        TwinBB,
        TwinFullboard
    }

    public class Room
    {
        public RoomType Type { get; set; }
        public int Price { get; set; }
    }

接下来在您的表单上,您只需使用绑定。我留下了一些代码注释掉我们以前也可以用来格式化数据,但对于您的情况,这可能是好的:

        BindingSource source = new BindingSource();

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

            source.Add(new Room() { Type = RoomType.DoubleBB, Price = 50 });
            source.Add(new Room() { Type = RoomType.DoubleFullboard, Price = 60 });
            source.Add(new Room() { Type = RoomType.SingleBB, Price = 40 });
            source.Add(new Room() { Type = RoomType.SingleFullboard, Price = 50 });
            source.Add(new Room() { Type = RoomType.TwinBB, Price = 55 });
            source.Add(new Room() { Type = RoomType.TwinFullboard, Price = 65 });

            comboBox1.DataSource = source;
            comboBox1.DisplayMember = "Type";
            comboBox1.ValueMember = "Price";

            Binding b = new Binding("Text", source, "Price");
            b.Format += new ConvertEventHandler(b_Format);

            textBox1.DataBindings.Add(b);
        }

        void b_Format(object sender, ConvertEventArgs e)
        {
           e.Value = string.Format("{0:0 USD}", e.Value);
        }

        //private void comboBox1_SelectedValueChanged(object sender, EventArgs e)
        //{
        //    textBox1.Text = comboBox1.SelectedValue.ToString() + "USD";
        //}

这就是全部,每次从组合框中选择另一个值时,文本框都会反映出这一变化。

答案 1 :(得分:1)

不确定是什么让你使用go to statement。我认为你只需要摆脱它们,你就会得到你想要的东西。

    switch(myChoice)
    {
        case RoomType.TwinBB:
            comboBoxroomtype.Text = "TwinBB";
            txtroomrate.Text = "55 USD";
            break;

      case RoomType.TwinFullboard:
            comboBoxroomtype.Text = "TwinFullboard";
            txtroomrate.Text = "65 USD";
            break;

        case RoomType.DoubleBB:
            comboBoxroomtype.Text = "DoubleBB";
            txtroomrate.Text = "50 USD";
            break;

        case RoomType.DoubleFullboard:
            comboBoxroomtype.Text = "DoubleFullboard";
            txtroomrate.Text = "60 USD";
            break;

        case RoomType.SingleBB:
            comboBoxroomtype.Text = "SingleBB";
            txtroomrate.Text = "40 USD";
            break;

        case RoomType.SingleFullboard:
            comboBoxroomtype.Text = "SingleFullboard";
            txtroomrate.Text = "50 USD";
            break;
        default:
            comboBoxroomtype.Text = "";
            txtroomrate.Text = "";
            break;

    }

答案 2 :(得分:0)

您只需删除中断关键字,即可使用

 switch(myChoice)
    {
        case RoomType.TwinBB:
            comboBoxroomtype.Text = "TwinBB";
            txtroomrate.Text = "55 USD";
            goto case RoomType.TwinFullboard;


      case RoomType.TwinFullboard:
            comboBoxroomtype.Text = "TwinFullboard";
            txtroomrate.Text = "65 USD";
            goto case RoomType.DoubleBB;


        case RoomType.DoubleBB:
            comboBoxroomtype.Text = "DoubleBB";
            txtroomrate.Text = "50 USD";
            goto case RoomType.DoubleFullboard;


        case RoomType.DoubleFullboard:
            comboBoxroomtype.Text = "DoubleFullboard";
            txtroomrate.Text = "60 USD";
            goto case RoomType.SingleBB;


        case RoomType.SingleBB:
            comboBoxroomtype.Text = "SingleBB";
            txtroomrate.Text = "40 USD";
            goto case RoomType.SingleFullboard;


        case RoomType.SingleFullboard:
            comboBoxroomtype.Text = "SingleFullboard";
            txtroomrate.Text = "50 USD";
            break;
        default:
            comboBoxroomtype.Text = "";
            txtroomrate.Text = "";
            break;
    }

因为可以通过切换案例来实现:

  1. 在案件中没有代码
  2. 或使用特殊的转到案例
  3. 或转到默认情况。

答案 3 :(得分:0)

@ user3385970的答案将有效。但您可以从switch语句中删除comboBoxroomtype.Text setter,只需对枚举值使用Enum.ToString()方法:

comboBoxroomType.Text = myChoice.ToString();
switch(myChoice)
{
    case RoomType.TwinBB:
        txtroomrate.Text = "55 USD";
        break;

    case RoomType.TwinFullboard:
        txtroomrate.Text = "65 USD";
        break;

    case RoomType.DoubleFullboard:
        txtroomrate.Text = "60 USD";
        break;

    case RoomType.SingleBB:
        txtroomrate.Text = "40 USD";
        break;

    case RoomType.DoubleBB:
    case RoomType.SingleFullboard:
        txtroomrate.Text = "50 USD";
        break;

    default:
        comboBoxroomtype.Text = "";
        txtroomrate.Text = "";
        break;

}