1

I am just creating an application in C# Windows Forms for Book Store application The process i am doing here is
1.Inserting Book name, book image to Database.
2.Now i need to retrieve as in the image to forms but all should be dynamically created like
if i have two books this panel with picture box, button and that textbook should be created and values should be showed there..
If i have got three entereids in DB means the three picture box, button and that textbook should be created and values should be showed there in the form.
is there any way to solve this out..

Need to Create these controls Dynamically according to the "n" number of rows in Database

For more Clarity about my question please see the image

enter image description here

4 Answers 4

1

Create a user control that contains the image, text box and button. Iterate over your list of records retrieved from the database and create new instance of the user control for each item. Add the user controls to the parent control's Controls collection.

Sign up to request clarification or add additional context in comments.

1 Comment

is it possible that we can retrieve all the field in single row to the respective places.. that is i need to create 100 of these if the values are 100 in db @Lost
1

You can achieve this using list view control:

you can get more details on this:

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

I hope it will help you.,. :)

7 Comments

is it possible that we can retrieve all the field in single row to the respective places.. that is i need to create 100 of these if the values are 100 in db. bcoz the above is created manually for all the control i need it dynamically @hitesh
do you mean to have all the data from the database and show on the form..??
s s that what i have mentioned in the question.. is there any way to solve this.. @Hitesh..
You can do it using the listview control. Just see the links i have added and also you can use c-sharpcorner.com/uploadfile/mahesh/… All you need to design it properly to show on form.
i will checkout the links u gave properly and will tell u @hitesh.. Thanks for the reply..
|
1
  1. Use UserControl to build your custom control (image, text field, button) (http://msdn.microsoft.com/en-us/library/aa302342.aspx)
  2. Use FlowLayoutPanel or TableLayoutPanel to add controls on your form.

Create Winforms project and add this file:

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

Next add FlowLayoutPanel and Button on your form. In form code at the begining add this:

private int _bookCount = 0;

Then on button's click event add following code:

var control = new CtrlBuyBook();
control.BookID = ++_bookCount;
control.Cover = null;
control.Quantity = 1;
control.BuyBook += new EventHandler<EventArgs>(control_BuyBook);
flpPanel.Controls.Add(control);

And finally the method for user control's BuyBook event:

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

All you need is to retrieve your books from DB and display on form and handle BuyBook event from User Control. That's all.

3 Comments

I guess the book he wants to sell is some kind of e-book, so the quantity is not relevant.
That was not included in specification :)
Yes Quantity is not important only retrieving values is needed for me.. @MrK
1

It's simple to achieve what you want. You just need to create some UserControl for each book or a Panel is OK. I'll introduce to using the Panel for it. Something like this:

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

NOTE: The code above is not complete, you don't have full ability to customize the look and feel of your TextBox, Button and PictureBox, however you can add code to do so, the code is a little much. I think for a simple control, it's enough. You should also notice the use of Dock property, the Margin and Padding properties and other layout and container controls like Panel, FlowLayoutPanel, TableLayoutPanel to customize your own control.

To use it in a FlowLayoutPanel, try this code:

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 ....
}

1 Comment

Ok +1 for quick solution i will try it out and will tell u

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.