Skip to main content

Windows Forms - Adding Controls Dynamically

Controls can be added in WinForms manually and dynamically (using code). Refer this Windows Forms - Adding Controls Manually to add controls manually.
In this article, we are gonna make a windows form application and add some controls to it.
1. Create a new project in visual studio. File->New Project->Windows Forms Application (Rename it as you want) . I named it CodeModeWinForms->Click Ok.
2. After the project is created, a blank Form appears. Right click on form and click on View Code to open code view
3. Now, we are gonna add buttons, labels, text fields etc in the form.
  • A control can be created like this
 Button button = new Button();
  • SetBounds can be used to set location and width, height or Location can be used to set location
 button.Location = new Point(100,200); //Or
 button.SetBounds(170, 40,100,100);
  • A control can be added to a form like this
 Controls.Add(button);
4. Complete code
using System;
using System.Drawing;
using System.Windows.Forms;
namespace CodeModeWinForms
{
    public partial class Form2 : Form
    {
        private Button btOrder;
        private ComboBox comboBox;
        private TextBox txtOrder;
        private Label l1, l2, l3, labelCost;

        double totalCost = 0; //variable to hold total cost 
        public Form2()
        {
            InitializeComponent();
            addControls(); //Method to add controls to form
            initializeItems(); //Method to fill combo box
        }

        private void addControls()
        {
            l1 = new Label();
            l1.Text = "Items and Cost";
            l1.Location = new Point(40, 10); //set label's location using Location

            l2 = new Label();
            l2.Text = "Ordered Items";
            l2.Location = new Point(190, 10);

            l3 = new Label();
            l3.Text = "Total:";
            l3.SetBounds(20,140,40,20); //set label's location and size using SetBounds

            labelCost = new Label();
            labelCost.Text = "$0";
            labelCost.Location = new Point(70, 140);

            comboBox = new ComboBox();
            comboBox.Location = new Point(20,40);
            //Add event handler to handle selected index changed property of combo box
            comboBox.SelectedIndexChanged += new EventHandler(comboBox_SelectedIndexChanged);

            txtOrder = new TextBox();
            txtOrder.Multiline = true; //Set textBox to be multiLine
            txtOrder.ScrollBars = ScrollBars.Vertical; //Show  a vertical scroll bar
            txtOrder.SetBounds(170, 40,100,100);

            btOrder = new Button();
            btOrder.Text = "Place Order";
            btOrder.Location = new Point(100,200);
            //Add event handler to handle click events of button
            btOrder.Click += new EventHandler(btnOrder_Click);
            
            //Add controls to form
            Controls.Add(l1);
            Controls.Add(l2);
            Controls.Add(l3);
            Controls.Add(labelCost);
            Controls.Add(comboBox);
            Controls.Add(txtOrder);
            Controls.Add(btOrder);
        }

        private void initializeItems()
        {
            //Add items and their cost to combo box
            comboBox.Items.Add("Phone 1000");
            comboBox.Items.Add("Laptop 4500");
            comboBox.Items.Add("Camera 5000");
            comboBox.Items.Add("Walkman 300");
            comboBox.Items.Add("Kindle 500");
            comboBox.Items.Add("Watch 450");
            comboBox.Items.Add("HeadPhones 150");
            comboBox.Items.Add("Mouse 30");
            comboBox.Items.Add("Keyboard 50");
            comboBox.Items.Add("PenDrive 60");

        }

        //This method will be called when an item is selected from combobox
        private void comboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            //Store selected item in item variable
            string item = comboBox.SelectedItem.ToString();

            //Split the string on the basis of space, to get item name and its cost
            string[] itemAndCost = item.Split();

            //Store name in itemName
            string itemName = itemAndCost[0];

            //Store cost in cost variable
            double cost = Convert.ToDouble(itemAndCost[1]);

            //Add cost to total
            totalCost += cost;

            //Add item to mulitline text box
            txtOrder.Text += itemName + "\r\n";

        }

        private void btnOrder_Click(object sender, EventArgs e)
        {
            //On placing order, show total cost 
            labelCost.Text = "$" + totalCost.ToString();
        }
    }
}
5. Run the project


Comments

Popular Posts