我有一个数组:
decimal[,] SmoothieListDecimal = { {5.99M, 6.99M, 7.99M, 8.99M},
{6.99M, 7.99M, 8.99M, 9.99M} }
我有两个组合框:
cmbSize和cmbStyle
cmbSize有:Small,Medium,Large和King,它们构成数组中的值。
第二个组合框是cmbStyle,它只包含两个选项,“Regular”和“Organic”。 “有机”价格高出1.00美元,来自第二排。
因此,例如,如果用户选择“中”大小和“常规”样式,则价格将从数组中第1行第1列拉出。
我的问题是,如何将变量设置为各自的数组坐标,此外,我如何编码方程式来处理这个价格选择? 我正在使用(Visual Studio 2015和C#) 谢谢!
答案 0 :(得分:0)
如何创建包含有关思慕雪价格信息的类:
public class SmoothiePrices
{
public string Description { get; set; }
public Dictionary<string, double> SizeAndPrice { get; set; }
}
请填写以下信息:
private List<SmoothiePrices> prices = new List<SmoothiePrices>();
public void InitSmoothies
{
prices.Add(new SmoothiePrices()
{
Name = "Regular",
SizeAndPrice = new Dictionary<string, double>()
{
{"Small", 5.99},
{"Normal", 6.99}
// And so on
};
});
}
现在您已经创建了一个数据结构并可以填充组合:
private void InitStyleCombo()
{
this.cmbStyle.DisplayMember = "Description";
this.cmbStyle.DataSource = this.prices;
}
最后要做的是根据cmbStyle组合的选择来填充cmbSize。
private void cmbStyle_SelectedIndexChanged(object sender, EventArgs e)
{
var smoothiePrice = this.cmbStyle.SelectedValue as SmoothiePrice;
this.cmbSize.DisplayMember = "Key";
this.cmbSize.ValueMember = "Value";
this.cmbSize.DataSource = smoothiePrice.SizeAndPrice;
}
要访问尺寸使用的选定价格:
var selectedPrice = (double)this.cmbSize.SelectedValue;
答案 1 :(得分:0)
有很多方法可以做到这一点,但一种简单的方法(但肯定不是最好的或最强大的)是以ComboBox.Items
的正确顺序加载SmoothieListDecimal
。
decimal price;
int column = cmbSize.SelectedIndex; // 0=Small,1=Medium,2=Large,3=King
int row = cmbStyle.SelectedIndex; // 0=Regular,1=Organic
if (column < 0)
MessageBox("Please select a size");
else if (row < 0)
MessageBox("Please select a style");
else
price = SmoothieListDecimal[row, col];