Numeric Textbox-Custom Control


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


Similar Articles