0

I referred to StackOverflow post here: How do I make a textbox that only accepts numbers?

The problem with this style is that I need to add code or a method call to this code in every TextBox's Keypress event.

I want it in a different way. I want to modify the TextBox class itself in such a way that it can be made to accept either a string or a number depending upon criteria, as and when TextBox control is dropped from the ToolBox on the Form.

Community
  • 1
  • 1
RKh
  • 13,080
  • 40
  • 145
  • 251

3 Answers3

1

just create a user control with any of method in the attached ink. or you can extend the textbox server control and use those methods in the extended control

Chamika Sandamal
  • 22,932
  • 5
  • 63
  • 83
1

Yes you can. For more information please read User Control.

KV Prajapati
  • 92,042
  • 19
  • 143
  • 183
1

Don't create a user control, simply derive a new class from TextBox. Add a new class to your project and get started with code like this:

using System;
using System.ComponentModel;
using System.Windows.Forms;

class NumberBox : TextBox {
    // insert code here
}

After you compile, the new control appears at the top of the toolbox.

Hans Passant
  • 897,808
  • 140
  • 1,634
  • 2,455