根据点击实现加减菜系及价格的的增减
namespace 点餐系统
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
// 遍历所有的菜品
foreach(Control co in flowLayoutPanel1.Controls)
{
// 创建按钮
Button btn = new Button();
btn.Tag = co;
foreach(Control item in co.Controls)
{
//MessageBox.Show(item.Tag.ToString());
// 设置对应的菜名的tag值
if (item.Tag.ToString() == "xu")
{
btn.Text = item.Text;
}
// 将按钮添加在点餐里面
}
flowLayoutPanel6.Controls.Add(btn);
// 添加事件 += tab两下
btn.Click += Btn_Click;
}
}
int count = 0;
private void Btn_Click(object sender, EventArgs e)
{
// 点击的事件对应的菜
Button cl = (Button)sender;
// 装菜的控件
Control cln=(Control)cl.Tag;
Label lab = new Label();
lab.Click += Lab_Click;
foreach (Control ca in cln.Controls )
{
if (ca.Tag.ToString() == "cai")
{
lab.Text = ca.Text;
}
if (ca.Tag.ToString() == "yuan")
{
count+= int.Parse(ca.Text);
lab.Tag = ca.Text;
}
}
flowLayoutPanel7.Controls.Add(lab);
label17.Text = count.ToString();
}
private void Lab_Click(object sender, EventArgs e)
{
//获取 label内容
Label label = sender as Label;
// MessageBox.Show(label.ToString());
//label.Visible = false;
//释放资源 垃圾回收机制里面的
label.Dispose();
count -= int.Parse(label.Tag.ToString());
label17.Text = count.ToString();
}
}
}