Tuesday 4 December 2012

How to create the Usercontrol and How to use this usercontrol in Realtime C#.net

Working with usercontrol is very efficient to the application for that follow below steps.
step1:How to create the user control
Goto visual studio-->click on file-->click on new-->click on project-->select windows from visual c#
left side and select windows forms control library template and type the Application name(textbox).
step2:
 Goto .cs file and try with this following code
write in the key_press event
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 textbox
{
    public partial class UserControl1 : UserControl
    {
        public UserControl1()
        {
            InitializeComponent();
        }
        private void UserControl1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (char.IsDigit(e.KeyChar) == false)
            {
                MessageBox.Show("Digits Only Can Accept");
                e.Handled = true;
            }

        }
        public override string Text
        {
            get{ return base.Text;}
            set{ base.Text = value}
        }
    }
}
Build the Application,It creates DLL for user control
step3:How to use that usercontrol
Consuming or Adding  DLL to our our IDE.
Create a new windows forms application,Goto ToolBox-->Rightclick on toolbox-->ADD tab-->Type usercontrols-->
Right click-->click on choose items-->selet .net framework component tab page-->click on browse-->goto DLL location-->
select the textbox DLL-->click on open-->click on ok.This wiil add the user control to the toolbox.
you can use it as textbox.