Email: Password:       Forgot Password 
    .netCodeSG
A Saarsha Group Online Community for dot net codes group like C#, Asp.NET, VB.NET, Sharepoint, JavaScript, JQuery, Ajax, SQL, WCF, WPF.
 
TECHNOLOGIES:
 

Make Custom Control or Component

Article:
Viewed:  927 
Posted On:  08/10/2014 06:36:12 
How we can make our own custom control as per our requirement in C# ? How to make component in c# ? 

In this article I am going to explain how you can make your own custom control in c #.  Here in this example I am going to make a Login Control. 

For this Open Visual Studio -> File -> New -> Select -> Windows Forms Control Library.

1.png

Image 1.

Now I design my control with User Id and Password Label and Text Box.

2.png

Image 2.

Below is my this login control code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
 
namespace MakeOwnCustomControl
{
    public partial class UserControl1 : UserControl
    {
        public delegate void loginOper();
        public event loginOper checkValue;
 
        public string userID
        {            set
            {
                txtUserId.Text = value;
            }
            get
            {
                return txtUserId.Text;
            }
        }

        public string password
        {
            set
            {
                txtPassword.Text = value;
            }
            get
            {
                return txtPassword.Text;
            }
        }
 
        public UserControl1()
        {
            InitializeComponent();
        }
 
        private void btnLogin_Click(object sender, EventArgs e)
        {
            if (txtUserId.Text == "" && txtPassword.Text == "")
            {
                MessageBox.Show("User ID & Password can not be null.");
            }
            else if (txtUserId.Text == "")
            {
                MessageBox.Show("Please Enter User ID.");
            }
            else if (txtPassword.Text == "")
            {
                MessageBox.Show("Please Enter Password.");
            }
            else
            {
                if (checkValue != null)
                {
                    checkValue();
                }
                else
                {
                    MessageBox.Show("Please Enter correct UserId/Password");
                }
            }
        }
    }
}

Now Compile and run this app.

3.png

Image 3

Here Click on Load and copy the dll path.

4.png

Image 4.

Now create a new Window Application.

Open Visual Studio -> New -> Windows Application

5.png

Image 5.

Here select View-> Toolbox -> Right Click anywhere and select -> Choose Items.

6.png

Image 6.

A popup window will open -> Click on Browse.

7.png

Image 7.

Go to User Control dll location (We copied dll path earlier). Select dll -> Click open

8.png

Image 8.

Now you can see your control -> OK.

9.png

Image 9.

Now come to your form see control is displaying in tool box -> drag and drop this control on form.

10.png

Image 10.

Now come to coding part of this form.

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;
using MakeOwnCustomControl;
using System.Data.SqlClient;
 
namespace MyTestApplication
{
    public partial class Form1 : Form
    {
        SqlDataAdapter da;
        DataSet ds;
        SqlConnection con;
 
        UserControl1 myCustomControl;
 
        public Form1()
        {
            myCustomControl = new UserControl1();
            InitializeComponent();
        }
 
        private void Form1_Load(object sender, EventArgs e)
        {
            myCustomControl.checkValue += new MakeOwnCustomControl.UserControl1.loginOper(UserControl1_checkValue);
        }
 
        void UserControl1_checkValue()
        {
            con = new SqlConnection(@"DATA SOURCE=MyPC\SqlServer2k8;INTEGRATED SECURITY=TRUE;DATABASE=TEST");
            da = new SqlDataAdapter("Select * FROM tbl_User Where UserId='" + myCustomControl.userID + "' AND Password='" + myCustomControl.password +
'", con);
            ds = new DataSet();
            da.Fill(ds);
 
 
            if (ds.Tables[0].Rows.Count > 0)
            {
                MessageBox.Show("Welcome !" + ds.Tables[0].Rows[0]["Name"].ToString());
            }
            else
            {
                MessageBox.Show("Please enter correct UserId/Password");
            }
        }
    }
}
 

Here I am authenticating User ID and Password from my Sql Table. Now run the application.

If you don't enter id and password and click on Log In button then error message will appear.

13.png

Image 11.

11.png

Image 12.

12.png

Image 13.

Below is my data table design from which I am authenticating user.

14.png

Image 14.

  Comment:
         HOME   |   Submit Article   |   Contact Us   |   About Us   |   Terms & Condition   |   Advertise With us