在c#Windows窗体中动态创建控件

在c#Windows窗体中动态创建控件,c#,sql,winforms,c#-4.0,C#,Sql,Winforms,C# 4.0,我正在用C#Windows窗体为书店应用程序创建一个应用程序 我在这里所做的过程是 1.将书籍名称、书籍图像插入数据库。 2.现在我需要检索表单图像中的内容,但所有内容都应动态创建,如如果我有两本书,则应创建带有图片框、按钮和教科书的面板,并在其中显示值。 如果我在DB中有三个EnterEID,则表示应创建三个图片框、按钮和教科书,并在表单中显示这些值。 有没有办法解决这个问题。 需要根据数据库中的“n”行数动态创建这些控件 有关我的问题的更多信息,请参见图片 创建一个包含图像、文本框和按钮的

我正在用C#Windows窗体为书店应用程序创建一个应用程序 我在这里所做的过程是
1.将书籍名称、书籍图像插入数据库。
2.现在我需要检索表单图像中的内容,但所有内容都应动态创建,如
如果我有两本书,则应创建带有图片框、按钮和教科书的面板,并在其中显示值。
如果我在DB中有三个EnterEID,则表示应创建三个图片框、按钮和教科书,并在表单中显示这些值。
有没有办法解决这个问题。

需要根据数据库中的“n”行数动态创建这些控件

有关我的问题的更多信息,请参见图片


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

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

您可以使用列表视图控件实现这一点:

您可以获得有关此的更多详细信息:


我希望它能帮助你

您可以使用列表视图控件实现这一点:

您可以获得有关此的更多详细信息:


我希望它能帮助你

实现你想要的很简单。您只需要为每本书创建一些
UserControl
,或者
面板就可以了。我将介绍如何使用
面板
。大概是这样的:

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);
};
注意:上面的代码不完整,您没有完全的能力自定义
文本框、按钮和图片框的外观,但是您可以添加代码来实现,代码有点多。我认为对于一个简单的控件来说,就足够了。您还应该注意使用
Dock
属性、
Margin
Padding
属性以及其他布局和容器控件,如
Panel
FlowLayoutPanel
TableLayoutPanel
,自定义您自己的控件

要在
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 ....
}
flowLayoutPanel1.AutoScroll=true;
对于(int i=0;i<100;i++){
新书架{
父级=flowLayoutPanel1,
宽度=150,
高度=200,
BorderStyle=BorderStyle.FixedSingle,
BookCover=您的图像列表[i]
}.BuyBook+=购买书;
}
私有void buyBook(对象发送者,事件参数e){
BookPanel book=发送方作为BookPanel;
//你的代码在这里。。。。
}

实现你想要的东西很简单。您只需要为每本书创建一些
UserControl
,或者
面板就可以了。我将介绍如何使用
面板
。大概是这样的:

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);
};
注意:上面的代码不完整,您没有完全的能力自定义
文本框、按钮和图片框的外观,但是您可以添加代码来实现,代码有点多。我认为对于一个简单的控件来说,就足够了。您还应该注意使用
Dock
属性、
Margin
Padding
属性以及其他布局和容器控件,如
Panel
FlowLayoutPanel
TableLayoutPanel
,自定义您自己的控件

要在
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 ....
}
flowLayoutPanel1.AutoScroll=true;
对于(int i=0;i<100;i++){
新书架{
父级=flowLayoutPanel1,
宽度=150,
高度=200,
BorderStyle=BorderStyle.FixedSingle,
BookCover=您的图像列表[i]
}.BuyBook+=购买书;
}
私有void buyBook(对象发送者,事件参数e){
BookPanel book=发送方作为BookPanel;
//你的代码在这里。。。。
}
  • 使用UserControl构建自定义控件(图像、文本字段、按钮)()
  • 使用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;
        }
    }
    
    然后在按钮的单击事件上添加以下代码:

    var control = new CtrlBuyBook();
    control.BookID = ++_bookCount;
    control.Cover = null;
    control.Quantity = 1;
    control.BuyBook += new EventHandler<EventArgs>(control_BuyBook);
    flpPanel.Controls.Add(control);
    
    您所需要的只是从DB中检索书籍,并在表单上显示,以及从用户控件处理BuyBook事件。 就这些

  • 使用UserControl构建自定义控件(图像、文本字段、按钮)()
  • 使用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;
        }
    }
    
    然后在按钮的单击事件上添加以下代码:

    var control = new CtrlBuyBook();
    control.BookID = ++_bookCount;
    control.Cover = null;
    control.Quantity = 1;
    control.BuyBook += new EventHandler<EventArgs>(control_BuyBook);
    flpPanel.Controls.Add(control);
    
    您所需要的只是从DB中检索书籍,并在表单上显示,以及从用户控件处理BuyBook事件。
    仅此而已。

    我们是否可以将单行中的所有字段检索到相应的位置。。也就是说,我需要创建100个这样的字段,如果值在db@Lostis中为100,那么我们可以检索单行中的所有字段到相应的位置。。也就是说,我需要创建100个这样的字段,如果值在db@Lostis中为100,那么我们可以检索单行中的所有字段到相应的位置。。也就是说,如果值是100分贝,我需要创建100个。bcoz以上是为我需要的所有控件手动创建的@hitesh你的意思是从数据库获取所有数据并显示在表单上吗?这就是我在问题中提到的。。有没有办法解决这个问题@Hitesh..您可以使用listview控件执行此操作。只要看看我添加的链接,你也可以使用所有你需要的