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,818
  • 46
  • 152
  • 265
  • Yes you can. For more information please read [User Control.](http://msdn.microsoft.com/en-us/library/system.windows.forms.usercontrol.aspx) – KV Prajapati Sep 20 '11 at 04:44
  • 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 Sep 20 '11 at 04:43

1 Answers1

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
  • 922,412
  • 146
  • 1,693
  • 2,536