c# - 带枚举器的 Switch 语句

标签 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;
    }
}

最佳答案

你可以尝试这样绑定(bind):

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

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

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

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

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

仅此而已,每次您从组合框中选择另一个值时,文本框都会反射(reflect)该更改。

关于c# - 带枚举器的 Switch 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25318283/

相关文章:

c# - 是否可以在 Windows 窗体应用程序中将 psycopg2 与 IronPython 一起使用?

c# - 我如何使用 Fluent NHibernate 来区分父关系的列

c# - 更新文件中的 xml 的最佳方法是什么?

c# - 任务、等待和 ManagementObjectCollection

swift - 在无关紧要的 switch case 中放什么?

c# - {} 表达式的含义

c# - 如何更改 Vs2012 中的异常消息?

c# - DataSet 更改未保存到数据库

c++ - 如何为类(class)成员创建开关?

java - leet Java程序中的非法转义