首先,来看下效果:
代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace FilmSample
{
public partial class Form_mainWin : Form
{
List<FilmLayout> layouts; //存放创建的组件
int num;
public Form_mainWin()
{
InitializeComponent();
num = 0;
}
private void Form1_mainWin_Load(object sender, EventArgs e)
{
layouts = new List<FilmLayout>();
}
void layout_base_MouseClick(object sender, MouseEventArgs e)
{
MessageBox.Show("groupbox被点击!");
}
private void button_addcontrol_Click(object sender, EventArgs e)
{
FilmLayout fl = new FilmLayout();
this.flowLayoutPanel1.Controls.Add(fl.getBaseLayout());
fl.getBaseLayout().MouseClick += new MouseEventHandler(layout_base_MouseClick);
layouts.Add(fl);
}
}
public class FilmLayout
{
private GroupBox layout_base; //基础组件
private PictureBox picturebox; //显示海报
private Label name; //显示电影名称
private Label detail; //电影描述
/**
* 本类构造函数
*/
public FilmLayout()
{
picturebox = new PictureBox();
name = new Label();
detail = new Label();
layout_base = new GroupBox();
//位置初始化
layout_base.Width = 144;
layout_base.Height = 195;
picturebox.Width = 144;
picturebox.Height = 140;
picturebox.Left = 0;
picturebox.Top = 0;
picturebox.Image = new Bitmap(@"C:\Users\879897637\Desktop\sample.jpg");
picturebox.SizeMode = PictureBoxSizeMode.StretchImage;
layout_base.Controls.Add(picturebox);
name.Text = "电影名称";
name.Left = 5;
name.Top = this.picturebox.Height + 5;
detail.Text = "电影描述..";
detail.Left = 5;
detail.Top = this.picturebox.Height + this.name.Height + 10;
//向根布局添加组件
this.layout_base.Controls.Add(this.name);
this.layout_base.Controls.Add(this.detail);
}
/**
* 获取根布局组件 GroupBox
*/
public GroupBox getBaseLayout()
{
return this.layout_base;
}
/**
* 设置label组件的text属性
*/
public void setLabelText(string name)
{
this.name.Text = name;
}
}
}