gpt4 book ai didi

c# - 带有枚举器的 Switch 语句

转载 作者:行者123 更新时间:2023-11-30 20:47:55 27 4
gpt4 key购买 nike

我正在尝试在 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com