开发者

By default make TextBox to accept numeric values only

开发者 https://www.devze.com 2023-04-05 20:31 出处:网络
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 ever

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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消