- 使用 UserControl 构建您的自定义控件(图像、文本字段、按钮)(http://msdn.microsoft.com/en-us/library/aa302342.aspx)
- 使用 FlowLayoutPanel 或 TableLayoutPanel 在表单上添加控件。
创建 Winforms 项目并添加此文件:
CtrlBuyBook.cs
using System;
using System.Drawing;
using System.Windows.Forms;
namespace UserControls
{
public class CtrlBuyBook : UserControl
{
public int BookID { get; set; }
public int Quantity
{
get { return (int)nudQuantity.Value; }
set { nudQuantity.Value = value; }
}
public Image Cover
{
set { picCover.Image = value; }
}
public CtrlBuyBook()
{
InitializeComponent();
}
private void btnBuy_Click(object sender, System.EventArgs e)
{
OnBuyBook(EventArgs.Empty);
}
public event EventHandler<EventArgs> BuyBook;
private void OnBuyBook(EventArgs e)
{
var handler = BuyBook;
if (handler != null)
{
handler(this, e);
}
}
private System.ComponentModel.IContainer components = null;
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
private void InitializeComponent()
{
this.picCover = new System.Windows.Forms.PictureBox();
this.btnBuy = new System.Windows.Forms.Button();
this.nudQuantity = new System.Windows.Forms.NumericUpDown();
((System.ComponentModel.ISupportInitialize)(this.picCover)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.nudQuantity)).BeginInit();
this.SuspendLayout();
this.picCover.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.picCover.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.picCover.Location = new System.Drawing.Point(4, 4);
this.picCover.Name = "picCover";
this.picCover.Size = new System.Drawing.Size(143, 167);
this.picCover.TabIndex = 0;
this.picCover.TabStop = false;
this.btnBuy.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.btnBuy.Location = new System.Drawing.Point(95, 177);
this.btnBuy.Name = "btnBuy";
this.btnBuy.Size = new System.Drawing.Size(52, 20);
this.btnBuy.TabIndex = 2;
this.btnBuy.Text = "Buy";
this.btnBuy.UseVisualStyleBackColor = true;
this.btnBuy.Click += new System.EventHandler(this.btnBuy_Click);
this.nudQuantity.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.nudQuantity.Location = new System.Drawing.Point(4, 177);
this.nudQuantity.Name = "nudQuantity";
this.nudQuantity.Size = new System.Drawing.Size(85, 20);
this.nudQuantity.TabIndex = 3;
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.Controls.Add(this.nudQuantity);
this.Controls.Add(this.btnBuy);
this.Controls.Add(this.picCover);
this.Name = "ctrlBuyBook";
this.Size = new System.Drawing.Size(150, 200);
((System.ComponentModel.ISupportInitialize)(this.picCover)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.nudQuantity)).EndInit();
this.ResumeLayout(false);
}
private System.Windows.Forms.PictureBox picCover;
private System.Windows.Forms.Button btnBuy;
private System.Windows.Forms.NumericUpDown nudQuantity;
}
}
接下来在表单上添加 FlowLayoutPanel 和 Button。在开头的表单代码中添加以下内容:
private int _bookCount = 0;
然后在按钮的单击事件上添加以下代码:
var control = new CtrlBuyBook();
control.BookID = ++_bookCount;
control.Cover = null;
control.Quantity = 1;
control.BuyBook += new EventHandler<EventArgs>(control_BuyBook);
flpPanel.Controls.Add(control);
最后是用户控件的 BuyBook 事件的方法:
private void control_BuyBook(object sender, EventArgs e)
{
var control = (CtrlBuyBook)sender;
MessageBox.Show(string.Format("Customer wants to buy book with ID: {0}. Quantity: {1}", control.BookID, control.Quantity));
}
您只需要从数据库中检索您的书籍并在表单上显示并从用户控件处理 BuyBook 事件。就这样。