点餐系统案例

在点餐这里插入图片描述
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

    private void Form1_Load(object sender, EventArgs e)
    {
        foreach (Control item in flowLayoutPanel1.Controls)
        //第一个foreach循环 是找到菜单中所有的 控件(Panel)//
        //而找到的这每一个Panel控件中 还包含有label控件集合//
        {
            Button bu = new Button();
            bu.Size = new Size(50, 30);
            bu.Font = new Font("宋体", 18F);
            bu.Tag = item;
            //将全部tag赋值给item//

            foreach (Control control in item.Controls)
            //在子控件中找到tag为x的标签//
            {
                if (control.Tag.ToString() == "x")
                {
                    bu.Text = control.Text;
                }
            }
            flowLayoutPanel10.Controls.Add(bu);
            // 在flow里面显示//
            bu.Click += Bu_Click;
            // 用代码添加事件//
            bu.AutoSize = true;
        }
    }

    private void Bu_Click(object sender, EventArgs e)
    {
        Button clickBu = (Button)sender;
        //表示为事件的发起者,(Button)强制转换为button类型//
        Control menu = (Control)clickBu.Tag;
        //找到对应的子控件赋值给me   control为控件类型//
        Label lab = new Label();
        foreach (Control item in menu.Controls)
        {
            if (item.Tag.ToString() == "xx")
            //拿下来item中的xx标签//
            {
                lab.Text = item.Text;
                //在lab中显示//
            }
            lab.AutoSize = true;//显示完整的label名称
            flowLayoutPanel11.Controls.Add(lab);//向要显示的flowLayoutPanel中

            int count = 0;

            if (item.Tag.ToString() == "xxx")
            //拿下来item中的xxx标签
            {
                // int.Parse()将字符串转换为数字//
                count += int.Parse(item.Text);
            }
            label27.Text = count + "元";
        }

        
    }
}

}
难点
bu.Click += Bu_Click;
foreach (Control item in menu.Controls)
{
if (item.Tag.ToString() == “xx”)
//拿下来item中的xx标签//
{
lab.Text = item.Text;
//在lab中显示//
}
// int.Parse()将字符串转换为数字//
count += int.Parse(item.Text);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值