我使用 C#.NET 为 Windows 应用程序创建了一个自定义文本框。它必须接受十进制(浮点数),如 8.32 和 16.002。
我构建了以下算法。它只接受纯数字。我不知道如何让它也接受浮动。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
namespace SmartTextBoxLib
{
public partial class SmartTextBox : TextBox
{
public SmartTextBox()
{
InitializeComponent();
}
protected override void OnKeyPress(KeyPressEventArgs e)
{
if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar))
{
e.Handled = true;
}
base.OnKeyPress(e);
}
}
}