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);