SIGN UP MEMBER LOGIN:    
ARTICLE

Numeric Textbox-Custom Control

Posted by Sapna Articles | Custom Controls C# October 14, 2011
Here we will see a Numeric Textbox Custom Control which accept only numeric input.
Reader Level:

Introduction:

As much as we know about the Textbox control in which we can enter any type of value as (Numeric type , Char type or any special character) . Here we are going to make a Numeric Textbox which will accept only numeric values; if there are any values except numeric. it will not be entered into the Textbox. Typically, you'll create your control as either as a Class Library Project (for components) or a Windows Control Library (for custom controls).

Whenever we are designing a custom control or component, you could add the control or component class directly to your application project. However, this approach prevents you from reusing your class in multiple applications. A better, more component-based approach is to create a separate project for your custom control or component. You can then use it in any other project by adding a reference to the compiled DLL assembly. This separate control or component project is a library. Here we are providing some steps to create a numeric Textbox Component or control.

Step 1: In this step we have to take a Library Project named as Custom Control as shown in figure given below.

          step_1.gif

Step 2: Now we are going to add a Custom Control into the Library Project.

  • Right click on project name as Custom Control and add a Custom control and give any name.
  • After adding the custom control it will show a gray window which will looks like as

         Add_Custom_Control.gif

Step 3: After adding Custom Control it looks like as.

         after_adding_Cust_cont.gif

Step 4: Now open the class1.cs file and write the code given below.

Code :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Custom_control1
{
 public class NumericTextBox : TextBox
  {
    protected override void OnKeyPress(KeyPressEventArgs e)
      {
       // Ignore all non-control and non-numeric key presses.
       if(!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar))
         {
           e.Handled = true;
         }
       //  to Call the implementation in the base TextBox class,
       // which raises the KeyPress event.
       base.OnKeyPress(e);
    }
  }
}

Code Description: In this code we have to make a class name as Numeric Textbox which inherit the Textbox class and there is a event method name as On Key Press() which is protected which is a key press event on key pressing from the keyboard it will raise an event and check the control value. If key pressing with any non-numeric key or any character digit will not entered into the Textbox. It will accept only numeric value.

Step 5: Now Build the solution of the class library file

Step 6: Now we are going to add the library project into the window form application.

  • Firstly open a window form application
  • Right click on Solution Explorer.
  • Add a new Project named as Custom Control it looks like figure given below.

         addding_both_application.gif

Step 7: Now add some necessary References add a windows form reference to the project Custom Control.

Step 8: Now add the Custom Control.dll reference to the window application and build the solution. You see that control will appear into the Custom component Toolbox.

            after_step_5.gif

Step 9: Now to add a component as a control to the toolbox now follow the step

  • Go to Toolbox.
  • Right Click on any Control.
  • And Choose Item.
  • And select the .NET Framework Component.
  • Choose or mark the click on Component name as NumericTextBox1.
  • Figure shows how it looks when we will open and add it.

    after_step_8.gif

Step 10: Further we are going to use our custom Control so we have to drag and drop it on windows form:

        after_step_9.gif

Step 11: Now we are going to run the application it will always take numeric value

Output Window:

         Output.gif

Login to add your contents and source code to this article
share this article :
post comment
 

Thank You Raanan.......

Posted by Sapna Oct 25, 2011

Tx

Posted by Raanan Oct 20, 2011
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
    Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Team Foundation Server Hosting
Become a Sponsor