How to create simple dynamic window form application?

I have created a window form that will generate two textbox, one combobox, one datepicker, and four label. This form does not connect to any database. The purpose of this application is to show how to create controls at runtime.

Here is my code:

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

namespace DynamicWindowsFormsApplication

{

    public class Person

    {

        string _name;

        string _address;

        string _gender;

        DateTime _dateOfBirth;

        public Person()

        {

            _name = string.Empty;

            _address = string.Empty;

            _gender = string.Empty;

            _dateOfBirth = new DateTime();

        }

        public string Name

        {

            get

            {

                return _name;

            }

            set

            {

                if (_name != value)

                    _name = value;

            }

        }

        public string Address

        {

            get

            {

                return _address;

            }

            set

            {

                if (_address != value)

                    _address = value;

            }

        }

        public string Gender

        {

            get

            {

                return _gender;

            }

            set

            {

                if (_gender != value)

                    _gender = value;

            }

        }

        public DateTime DateOfBirth

        {

            get

            {

                return _dateOfBirth;

            }

            set

            {

                if (_dateOfBirth != value)

                    _dateOfBirth = value;

            }

        }

    }

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

            this.ControlBox = false;

            int x = 0;

            int y = 0;

            Point pt = new Point();

            pt.X = 10;

            pt.Y = 10;

            x = pt.X;

            y = pt.Y;

            Label lbl = new Label();

            lbl.Name = “lblName”;

            lbl.Location = pt;

            lbl.Text = “Name:”;

            lbl.TextAlign = ContentAlignment.TopRight;

            this.Controls.Add(lbl);

            TextBox txt = new TextBox();

            txt.Name = “txtName”;

            txt.Width = 250;

            pt = new Point(lbl.Width + 10, 10);

            x = pt.X;

            y = pt.Y;

            txt.Location = pt;

            this.Controls.Add(txt);

            y = txt.Height + 15;

            lbl = new Label();

            lbl.Name = “lblAddress”;

            pt = new Point(10,y);

            lbl.Location = pt;

            lbl.Text = “Address:”;

            lbl.TextAlign = ContentAlignment.TopRight;

            this.Controls.Add(lbl);

            txt = new TextBox();

            txt.Name = “txtAddress”;

            txt.Width = 250;

            pt = new Point(lbl.Width + 10, y);

            txt.Location = pt;

            this.Controls.Add(txt);

            y += txt.Height + 5;

            lbl = new Label();

            lbl.Name = “lblGender”;

            lbl.Text = “Gender:”;

            lbl.TextAlign = ContentAlignment.TopRight;

            pt = new Point(10, y);

            lbl.Location = pt;

            this.Controls.Add(lbl); 

            x = lbl.Width + 10;

            ComboBox cbo = new ComboBox();

            cbo.Name = “cboGender”;

            cbo.Width = 250;

            cbo.Items.Add(“Male”);

            cbo.Items.Add(“Female”);

            pt = new Point(x, y);

            cbo.Location = pt;

            this.Controls.Add(cbo);

            y += cbo.Height + 5;

            lbl = new Label();

            lbl.Name = “lblDateofBirth”;

            lbl.Text = “Date of Birth:”;

            lbl.TextAlign = ContentAlignment.TopRight;

            lbl.Location = new Point(10, y);

            this.Controls.Add(lbl);

            DateTimePicker picker = new DateTimePicker();

            picker.Name = “dtpDateOfBirth”;

            picker.Width = 250;

            picker.Location = new Point(lbl.Width + 10, y);

            this.Controls.Add(picker);

            y += picker.Height + 5;

            List<Person> personList = new List<Person>();

            Button btn = new Button();

            btn.Name = “btnSave”;

            btn.Text = “Save”;

            btn.TextAlign = ContentAlignment.TopRight;

            btn.Location = new Point(10, y);

            btn.Click += new EventHandler(btnSave_Click);

            this.Controls.Add(btn);

            x = btn.Width + 10;

            btn = new Button();

            btn.Name = “btnReset”;

            btn.Text = “Reset”;

            btn.TextAlign = ContentAlignment.TopLeft;

            btn.Location = new Point(x, y);

            btn.Click += new EventHandler(btnReset_Click);

            this.Controls.Add(btn); 

        }

        void btnReset_Click(object sender, EventArgs e)

        {

            try

            {

                foreach (Control c in this.Controls)

                {

                    if (c.GetType() == typeof(TextBox))

                    {

                        ((TextBox)c).Text = string.Empty;

                    }

                    if (c.GetType() == typeof(ComboBox))

                    {

                        ((ComboBox)c).SelectedIndex = -1;

                    }

                    if (c.GetType() == typeof(DateTimePicker))

                    {

                        ((DateTimePicker)c).Text = string.Empty;

                    }

                }

            }

            catch (System.Exception ex)

            {

                MessageBox.Show(ex.Message + Environment.NewLine + ex.StackTrace);

            }

        }

        void btnSave_Click(object sender, EventArgs e)

        {

            try

            {

                TextBox txt = new TextBox();

                ComboBox cbo = new ComboBox();

                DateTimePicker pick = new DateTimePicker();

                Person _person = new Person();

                foreach (Control c in this.Controls)

                {

                    if (c.GetType() == typeof(TextBox))

                    {

                        txt = new TextBox();

                        txt = (TextBox)c;

                        if (txt.Name.Contains(“Name”))

                            _person.Name = txt.Text.Trim();

                        if (txt.Name.Contains(“Address”))

                            _person.Address = txt.Text.Trim();

                    }

                    if (c.GetType() == typeof(ComboBox))

                    {

                        cbo = new ComboBox();

                        cbo = (ComboBox)c;

                        if (cbo.Name.Contains(“Gender”))

                            _person.Gender = cbo.SelectedItem.ToString();

                    }

                    if (c.GetType() == typeof(DateTimePicker))

                    {

                        pick = new DateTimePicker();

                        pick = (DateTimePicker)c;

                        if (pick.Name.Contains(“DateOfBirth”))

                            _person.DateOfBirth = Convert.ToDateTime(pick.Value);

                    }

                }

                MessageBox.Show(” Name: ” + _person.Name + Environment.NewLine + ” Address: ” +  _person.Address + Environment.NewLine + ” Gender: ” + _person.Gender + Environment.NewLine + ” Date of Birth: ” + _person.DateOfBirth.ToString());

            }

            catch (System.Exception ex)

            {

                MessageBox.Show(ex.Message + Environment.NewLine + ex.StackTrace);

            }

        }

    }

}

Advertisements
This entry was posted in C# 3.5, Visual Studio 2008. Bookmark the permalink.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s