Hi All,
This is with regard to winforms application.
My assignment requires to create 23 dynamic controls based on the 80 fields in the database.
At a time i should be able to display 23 controls in a form
Those fields can be edit box, comobo,listbox etc.
Please suggest me the best way of doing this.

Thanks and Regards
Subha.

Recommended Answers

All 2 Replies

One quick and easy way, is to use the designer to create all your controls, and adjust any properties. Open the designer file(i.e. Form1.Designer.cs) and copy everything in private void InitializeComponent(), and put it into your main code file(i.e. Form1.cs), right after InitializeComponent();, now delete all the controls from the designer. Now the same controls will load at run time in your code. I've never had a reason to add controls at run time but the code is relatively simple and this way you can see what steps are required. From this point it's should be quite easy make sub routines to add the controls on demand.

Here is a short tutorial

Example of adding a combobox control

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            ComboBox comboBox1 = new ComboBox();
            comboBox1.Items.Add("Item 1");
            comboBox1.Items.Add("Item 2");
            comboBox1.Items.Add("Item 3");
            comboBox1.Location = new Point(100, 100);
            comboBox1.Width = 200;
            // Add the combo box to a parent container in the visual tree.
            this.Controls.Add(comboBox1);

        }
    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.