How to Add Controls at Run Time in .NET

By
Today i am going to make a  Login Form at Run Time using C# Language.In this tutorial i will Add some controls at Run Time without Using Drag and Drop action.Which is given below:
  • Label
  • Text Box
  • comboBox
  • Button
There are some steps follow them one by one which are given below:
Step :1 First Open Your Visual Studio-->File-->New-->Project-->Windows Forms Application-->click OK.

Step :2 Now Double click on Form and Write the Following codes which is given below:

using System;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

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

        TextBox tb1 = new TextBox();
        TextBox tb2 = new TextBox();
        TextBox tb3 = new TextBox();
        Label lb1 = new Label();
        Label lb2 = new Label();
        Label lb3 = new Label();
        ComboBox cb = new ComboBox();
        Button bt = new Button();
        private void Form1_Load(object sender, EventArgs e)
        {
            tb1.Location = new Point(411, 103);
            this.Controls.Add(tb1);
            lb1.Location = new Point(350, 103);
            lb1.Text="UserName";
            this.Controls.Add(lb1);
            tb2.Location = new Point(411, 140);
            this.Controls.Add(tb2);
            lb2.Location = new Point(350, 140);
            lb2.Text = "Password";
            this.Controls.Add(lb2);
            cb.Location = new Point(411, 180);
            cb.Items.Add("Admin");
            cb.Items.Add("User");
            this.Controls.Add(cb);
            lb3.Location = new Point(350, 180);
            lb3.Text = "Type";
            this.Controls.Add(lb3);
            bt.Text = "OK";
            bt.Click += new EventHandler(bt_Click);
            bt.Location = new Point(411, 220);
            this.Controls.Add(bt);
        }

        void bt_Click(object sender, EventArgs e)
        {
            if (cb.SelectedItem =="User")
            {
                if (tb1.Text == "User" && tb2.Text == "usr123")
                {
                    MessageBox.Show("Hello"+ " " + tb1.Text);
                }
                else
                {
                    MessageBox.Show("Enter correct User Name or password Again");
                }
            }
            else
            {
                if (tb1.Text == "Admin" && tb2.Text == "adm123")
                {
                    MessageBox.Show("Welcome"+" " + tb1.Text);
                }
                else
                {
                    MessageBox.Show("Enter correct Admin Name or Password Again");
                } 
            }
        }
    }
}
Step :3 Now Run the Program(press F5).
see it:

Step :6 

  • Now Enter the Required Fields and Select Type from comboBox=User--> Click OK. which is shown below:

Output:








  • Now Enter the Required Fields and Select Type from comboBox=Admin--> Click OK. which is shown below:


  • Output:

    Note:- when you will Type wrong UserName and Password then it will give error.You can see after download this application.

    Description:- In this program i have created object of each controls  first which is shown below:
    
    TextBox tb1 = new TextBox();
            TextBox tb2 = new TextBox();
            TextBox tb3 = new TextBox();
            Label lb1 = new Label();
            Label lb2 = new Label();
            Label lb3 = new Label();
            ComboBox cb = new ComboBox();
            Button bt = new Button();
    After that i have draw each controls with the help of this object  in this application.which is shown in the above program.
    After that i have putted some conditions for validation the Login Form.
    For web form control:
    Web Form Controls in Asp.Net(I)

    I hope this is helpful for you.

    Download Whole Attached  Application:-

            Download

    0 comments:

    Post a Comment

    Powered by Blogger.