How to create composite custom control and add them into toolbox

Composite control is very important functionality provided by the Microsoft.NET.Using custom control we can make windows forms application very easily. In Custom control application we can combine multiple controls according to our requirements.We can easily merge this custom control in any windows application.

Step 1:- First open your visual studio –> File –>New–>Project–>Select windows Forms control Library–>OK–>Now Drag and Drop Label, TextBox and Button control as shown below:-

login form

Step 2:- Now Double click on Login Button–> and write the following codes:-

usingSystem;
usingSystem.ComponentModel;
usingSystem.Text;
usingSystem.Windows.Forms;
namespaceWindowsFormsControlLibrary1
{
    publicpartialclasscustomcontrol : UserControl
    {
        publicdelegatevoiddel();
        publiceventdel checkdetails;
        publicstringpid
        {
            set
            {
                textBox1.Text = value;
            }
            get
            {
                returntextBox1.Text;
            }
        }
        publicstringpassword
        {
            set
            {
                textBox2.Text = value;
            }
            get
            {
                returntextBox2.Text;
            }
        }
        publiccustomcontrol()
        {
            InitializeComponent();
        }
        privatevoidbutton1_Click(objectsender, EventArgs e)
        {
            if(textBox1.Text == ""&& textBox2.Text == "")
            {
                MessageBox.Show("please enter details");
            }
            elseif(textBox1.Text == "")
            {
                MessageBox.Show("please enter id");
            }
            elseif(textBox2.Text == "")
            {
                MessageBox.Show("please enter password");
            }
            else
            {
                if(checkdetails != null)
                {
                    checkdetails();
                }
                else
                {
                    MessageBox.Show("enter correct dtails");
                }
            }
        }
    }
}
Step 3:-Now Run the Application(press F5) –> and click on Load Button –> copy URL Path,which is as shown below:-

copy url
Step 4:– Now open your visual studio –> File–> New–>project–>Windows forms Application –>OK–>Now open Toolbox–>Right click on Toolbox –>click choose items as shown below:

choose items
Step 5:– Now click browse –> Paste the Url that you have copied before(otherwise Run First Application and get Url again)–>click on Arrow Button –> Select dll file as shown below–>Now click on  open Button.

select dll

Step 6:-Now open your Toolbox and Drag & Drop composite custom control on the Form as shown below:-

Toolbox

Step 7:– Now Double click on Form and write the following codes on the Form Load as given below.

usingSystem;
usingSystem.ComponentModel;
usingSystem.Text;
usingSystem.Windows.Forms;
namespacecomposite_application
{
    publicpartialclassForm1 : Form
    {
        publicForm1()
        {
            InitializeComponent();
        }
        privatevoidForm1_Load(objectsender, EventArgs e)
        {
            customcontrol1.checkdetails += newWindowsFormsControlLibrary1.customcontrol.del(customcontrol1_checkdetails);
        }
        voidcustomcontrol1_checkdetails()
        {
            if(customcontrol1.pid == "user"&& customcontrol1.password == "pass")
            {
          MessageBox.Show("welcome to https://codeworldtechnology.wordpress.com/ share it if possible");
            }
            else
            {
                MessageBox.Show("invalid user");
            }
        }
    }
}
Step 8:– Run the Application(press F5).There are some credentials which i have set in dll file which are given below:-

  • If you leave both Textbox field blank then it will give error”Please enter the password”.
  • If you leave only password field  blank then then it will give error “please enter password”.
  • If you leave only User Name Field blank then it will error “please enter the id”.
  • If you fill both fill both Field correctly then it will give  Message “Welcome to http://msdotnet.co.in ,share it if possible”.
success
  • If you enter both field wrong then it will give error “Invalid user”.
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

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