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.

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

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.

Image 3
Here Click on Load and copy the dll path.

Image 4.
Now create a new Window Application.
Open Visual Studio -> New -> Windows Application

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

Image 6.
A popup window will open -> Click on Browse.

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

Image 8.
Now you can see your control -> OK.

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

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.

Image 11.

Image 12.

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

Image 14.