我无法让 textBox_name 文本和 listBox_order 项目在另一个列表框的一行中正确显示
I can't get the textBox_name text and listBox_order items to display correctly in one line on another listbox
我有 3 个列表框,如果单击按钮(主菜、饮料和配菜),listBox_menu 是首先显示项目的地方,然后添加要订购的项目 select您的商品来自 listBox_menu,然后单击“添加到订单”以使其显示在 listBox_order 中。一旦客户订单准备就绪,提交按钮将打印出客户姓名 (textBox_name) 和 listBox_order 中的所有项目,并将其全部显示在一行中。
我只是不知道该怎么做,如果有人能帮忙的话。
先定义两个全局变量,然后使用CalculateTotalCost
函数(我定义的)
我的回答已更新。
输出(在 Visual Studio 2017
、.Net Framework 4.5.2
中测试):
这些是我的代码:
public Form1()
{
InitializeComponent();
listBox_menu.SelectionMode = SelectionMode.MultiExtended;
}
double Cost = 0;
string Order = null;
public double CalculateTotalCost(object input, bool Total)
{
if (Total == true)
{
switch (input)
{
case "Salad":
Cost += 2500;
break;
case "Rice":
Cost += 3000;
break;
case "non-alcoholic beer":
Cost += 1000;
break;
case "Water":
Cost += 200;
break;
case "Ex1":
Cost += 2200;
break;
default:
Cost += 2200;
break;
}
}
else
{
switch (input)
{
case "Salad":
Cost -= 2500;
break;
case "Rice":
Cost -= 3000;
break;
case "non-alcoholic beer":
Cost -= 1000;
break;
case "Water":
Cost -= 200;
break;
case "Ex1":
Cost -= 2200;
break;
default:
Cost -= 2200;
break;
}
}
return Cost;
}
private void Entrees_Click(object sender, EventArgs e)
{
listBox_menu.Items.Clear();
listBox_menu.Items.Add("Salad");
listBox_menu.Items.Add("Rice");
}
private void Drinks_Click(object sender, EventArgs e)
{
listBox_menu.Items.Clear();
listBox_menu.Items.Add("non-alcoholic beer");
listBox_menu.Items.Add("Water");
}
private void Sides_Click(object sender, EventArgs e)
{
listBox_menu.Items.Clear();
listBox_menu.Items.Add("Ex1");
listBox_menu.Items.Add("Ex2");
}
private void AddtoOrder_Click(object sender, EventArgs e)
{
if(listBox_menu.SelectedItems.Count>0)
{
for (int i = 0; i < listBox_menu.SelectedItems.Count; i++)
{
listBox_order.Items.Add(listBox_menu.SelectedItems[i].ToString());
lblTotalCost.Text = (CalculateTotalCost(listBox_menu.SelectedItems[i].ToString(),true)).ToString();
}
}
}
private void RemoveFromOrder_Click(object sender, EventArgs e)
{
if (listBox_order.SelectedItems.Count > 0)
{
listBox_order.Items.Remove(listBox_order.SelectedItem);
}
Order = null;
for (int i = 0; i < listBox_order.Items.Count; i++)
{
Order += listBox_order.Items[i].ToString() + " , ";
}
Cost = 0;
if (listBox_order.Items.Count > 0)
{
for (int i = 0; i < listBox_order.Items.Count; i++)
{
Cost = (CalculateTotalCost(listBox_order.Items[i], true));
}
}
lblTotalCost.Text = Cost.ToString();
}
private void Submit_Click(object sender, EventArgs e)
{
MessageBox.Show("Customer name: " + txtBoxCustomerName.Text + " Orders: " + Order + " Total cost: " + Cost);
}
我有 3 个列表框,如果单击按钮(主菜、饮料和配菜),listBox_menu 是首先显示项目的地方,然后添加要订购的项目 select您的商品来自 listBox_menu,然后单击“添加到订单”以使其显示在 listBox_order 中。一旦客户订单准备就绪,提交按钮将打印出客户姓名 (textBox_name) 和 listBox_order 中的所有项目,并将其全部显示在一行中。
我只是不知道该怎么做,如果有人能帮忙的话。
先定义两个全局变量,然后使用CalculateTotalCost
函数(我定义的)
我的回答已更新。
输出(在 Visual Studio 2017
、.Net Framework 4.5.2
中测试):
这些是我的代码:
public Form1()
{
InitializeComponent();
listBox_menu.SelectionMode = SelectionMode.MultiExtended;
}
double Cost = 0;
string Order = null;
public double CalculateTotalCost(object input, bool Total)
{
if (Total == true)
{
switch (input)
{
case "Salad":
Cost += 2500;
break;
case "Rice":
Cost += 3000;
break;
case "non-alcoholic beer":
Cost += 1000;
break;
case "Water":
Cost += 200;
break;
case "Ex1":
Cost += 2200;
break;
default:
Cost += 2200;
break;
}
}
else
{
switch (input)
{
case "Salad":
Cost -= 2500;
break;
case "Rice":
Cost -= 3000;
break;
case "non-alcoholic beer":
Cost -= 1000;
break;
case "Water":
Cost -= 200;
break;
case "Ex1":
Cost -= 2200;
break;
default:
Cost -= 2200;
break;
}
}
return Cost;
}
private void Entrees_Click(object sender, EventArgs e)
{
listBox_menu.Items.Clear();
listBox_menu.Items.Add("Salad");
listBox_menu.Items.Add("Rice");
}
private void Drinks_Click(object sender, EventArgs e)
{
listBox_menu.Items.Clear();
listBox_menu.Items.Add("non-alcoholic beer");
listBox_menu.Items.Add("Water");
}
private void Sides_Click(object sender, EventArgs e)
{
listBox_menu.Items.Clear();
listBox_menu.Items.Add("Ex1");
listBox_menu.Items.Add("Ex2");
}
private void AddtoOrder_Click(object sender, EventArgs e)
{
if(listBox_menu.SelectedItems.Count>0)
{
for (int i = 0; i < listBox_menu.SelectedItems.Count; i++)
{
listBox_order.Items.Add(listBox_menu.SelectedItems[i].ToString());
lblTotalCost.Text = (CalculateTotalCost(listBox_menu.SelectedItems[i].ToString(),true)).ToString();
}
}
}
private void RemoveFromOrder_Click(object sender, EventArgs e)
{
if (listBox_order.SelectedItems.Count > 0)
{
listBox_order.Items.Remove(listBox_order.SelectedItem);
}
Order = null;
for (int i = 0; i < listBox_order.Items.Count; i++)
{
Order += listBox_order.Items[i].ToString() + " , ";
}
Cost = 0;
if (listBox_order.Items.Count > 0)
{
for (int i = 0; i < listBox_order.Items.Count; i++)
{
Cost = (CalculateTotalCost(listBox_order.Items[i], true));
}
}
lblTotalCost.Text = Cost.ToString();
}
private void Submit_Click(object sender, EventArgs e)
{
MessageBox.Show("Customer name: " + txtBoxCustomerName.Text + " Orders: " + Order + " Total cost: " + Cost);
}