1

我只是在 C# Windows Forms 中为 Book Store 应用程序创建一个应用程序我在这里做的过程是
1.Inserting Book name, book image to Database。
2.现在我需要在图像中检索表单,但所有内容都应该动态创建,就像我 有
两本书一样,这个面板带有图片框、按钮,应该创建教科书并在那里显示值。
DB中的三个entereids意味着应该创建三个图片框,按钮和那个教科书,并且应该在表单中显示值。
有没有办法解决这个问题。。

需要根据数据库中的“ n ”行数

动态创建这些控件 有关我的问题的更多说明,请参见图片

在此处输入图像描述

4

4 回答 4

1

创建一个包含图像、文本框和按钮的用户控件。遍历从数据库中检索到的记录列表,并为每个项目创建用户控件的新实例。将用户控件添加到父控件的 Controls 集合中。

于 2013-10-25T12:11:46.090 回答
1

您可以使用列表视图控件来实现此目的:

您可以获得更多详细信息:

http://www.dotnetperls.com/listview

http://www.c-sharpcorner.com/UploadFile/9a3ae2/using-listview-control-windows-form-application3/

http://www.java2s.com/Code/CSharp/GUI-Windows-Form/ListViewExample.htm

我希望它会帮助你。,。:)

于 2013-10-25T12:14:28.593 回答
1

实现你想要的很简单。你只需要UserControl为每本书创建一些或者一个Panel就可以了。我将介绍如何使用Panel它。像这样的东西:

public class BookPanel : Panel
{
    public string BookName
    {
        get { return text.Text; }
        set { text.Text = value; }
    }
    public Image BookCover
    {
        get { return pic.Image; }
        set { pic.Image = value; }
    }
    public event EventHandler BuyBook;
    public string BuyButtonText
    {
        get { return button.Text; }
        set { button.Text = value; }
    }        
    //inner child controls
    PictureBox pic = new PictureBox();
    TextBox text = new TextBox();
    Button button = new Button();
    public BookPanel()
    {
        pic.Parent = text.Parent = button.Parent = this;
        pic.Top = 5;
        text.Left = pic.Left = 5;
        button.Text = "Buy";
        button.Width = 50;
        button.Anchor = AnchorStyles.Right | AnchorStyles.Bottom;
        text.Anchor = AnchorStyles.Left | AnchorStyles.Bottom | AnchorStyles.Right;
        pic.Anchor = AnchorStyles.Left | AnchorStyles.Top | AnchorStyles.Right | AnchorStyles.Bottom;
        pic.SizeMode = PictureBoxSizeMode.StretchImage;
        button.Click += (s, e) =>
        {
            EventHandler handler = BuyBook;
            if (handler != null) handler(this, EventArgs.Empty);
        };
    }
    bool init;
    protected override void OnSizeChanged(EventArgs e)
    {            
        base.OnSizeChanged(e);
        if (!init)
        {
            text.Width = Width - button.Width - 12;
            button.Left = text.Right + 5;
            pic.Height = Height - 35;
            pic.Width = Width - 10;
            text.Top = pic.Bottom + 5;
            button.Top = text.Top - 2;
            init = true;
        }
    }
}

//Usage
bookPanel1.BookCover = yourImage;
//Try this to see how the Buy button works
bookPanel1.BuyBook += (s, e) => {
    MessageBox.Show(bookPanel1.BookName);
};

注意:上面的代码是不完整的,你没有完全的能力来定制你的外观和感觉TextBox, Button and PictureBox,但是你可以添加代码来做到这一点,代码有点多。我认为对于一个简单的控制,这就足够了。您还应该注意使用Dock属性、MarginPadding属性以及其他布局和容器控件(如PanelFlowLayoutPanel、 )TableLayoutPanel来自定义您自己的控件。

要在 a 中使用它FlowLayoutPanel,请尝试以下代码:

flowLayoutPanel1.AutoScroll = true;
for (int i = 0; i < 100; i++) {
   new BookPanel {
                Parent = flowLayoutPanel1, 
                Width = 150, 
                Height = 200,
                BorderStyle = BorderStyle.FixedSingle,
                BookCover = yourImageList[i]
    }.BuyBook += buyBook;
}
private void buyBook(object sender, EventArgs e){
   BookPanel book = sender as BookPanel;
   //your code goes here ....
}
于 2013-10-25T12:49:19.077 回答
1
  1. 使用 UserControl 构建您的自定义控件(图像、文本字段、按钮)(http://msdn.microsoft.com/en-us/library/aa302342.aspx
  2. 使用 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 事件。就这样。

于 2013-10-25T12:56:28.450 回答