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

coolprarun
  • 1,153
  • 2
  • 15
  • 22

4 Answers4

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.

Lost_Cause
  • 96
  • 2
  • 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 – coolprarun Oct 25 '13 at 12:17
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.,. :)

Hitesh
  • 3,508
  • 1
  • 18
  • 24
  • 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 – coolprarun Oct 25 '13 at 12:18
  • do you mean to have all the data from the database and show on the form..?? – Hitesh Oct 25 '13 at 12:28
  • s s that what i have mentioned in the question.. is there any way to solve this.. @Hitesh.. – coolprarun Oct 25 '13 at 12:32
  • You can do it using the listview control. Just see the links i have added and also you can use http://www.c-sharpcorner.com/uploadfile/mahesh/working-with-listview-in-C-Sharp/ All you need to design it properly to show on form. – Hitesh Oct 25 '13 at 12:33
  • i will checkout the links u gave properly and will tell u @hitesh.. Thanks for the reply.. – coolprarun Oct 25 '13 at 12:49
  • My pleasure.. Let me know so that i will help you out.. :) – Hitesh Oct 25 '13 at 12:55
  • You have to customize your `ListView` control **much** to achieve the thing the OP wants. How do you render the `TextBox` and `Button` corresponding to each `ListViewItem`? `ListView` is just for display, to support some control interactivity, you have to customize it. This is not easy. – King King Oct 25 '13 at 13:03
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 ....
}
King King
  • 61,710
  • 16
  • 105
  • 130
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.

MrK
  • 11
  • 1