实现机理
主要是通过复写
protected override void OnKeyPress(KeyPressEventArgs e)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
namespace AnlogTest.Formlib
{
public class AnlogIntegralTextBox : TextBox
{
/
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region 组件设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
components = new System.ComponentModel.Container();
//'this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
}
#endregion
/
//此类用来删除多余行数的,每次添加自动增加换行
public AnlogIntegralTextBox()
{
InitializeComponent();
}
//private override KeyPressEventHandler(object sender, KeyPressEventArgs e)
//{
//}
//protected override void WndProc(ref Message m)
//{
// base.WndProc(ref m);
// if (m.Msg == 0xf || m.Msg == 0x14 || m.Msg == 0x85)
// {
// if (this.BorderStyle == BorderStyle.None)
// {
// if (m_underLine)
// {
// using (Graphics g = Graphics.FromHwnd(this.Handle))
// {
// g.DrawLine(SystemPens.ControlText, 0, this.Height - 1, this.Width - 1, this.Height - 1);
// }
// }
// }
// }
//}
protected override void OnKeyPress(KeyPressEventArgs e)
{
//移动到下一个
//if (e.KeyChar == 13)
//{
// this.SelectNextControl(this.ActiveControl, true, true, true, true);
//}
//base.OnKeyPress(e);
//System.Diagnostics.Debug.WriteLine(" -> " + e.KeyChar.ToString());
if ((e.KeyChar >= '0' && e.KeyChar <= '9') || e.KeyChar == 8)
{
//e.Handled = false;
//System.Diagnostics.Debug.WriteLine(" keyout ------> " + e.KeyChar.ToString());
//base.OnKeyPress(e);
e.Handled = false;
}
else
{
// base.OnKeyPress(e);
e.Handled = true;
}
}
}
public class AnlogFp32TextBox:TextBox
{
protected override void OnKeyPress(KeyPressEventArgs e)
{
if ((e.KeyChar >= '0' && e.KeyChar <= '9') || (e.KeyChar == 8))
{
}
else
{
//可以在首个位置输入 -
if (e.KeyChar == '-' && this.SelectionStart == 0)
{
// return;
}
else
{
//小数点处理 只能输入一个小数点,并且小数点不能在首位
if (e.KeyChar == '.' &&
this.Text.IndexOf(".") == -1 &&
this.SelectionStart != 0)
{
//处理第一位是符号然后紧接着输入小数点
if (e.KeyChar == '.' &&
this.Text.IndexOf("-") == 0 &&
this.SelectionStart != 1)
{
}
else
{
e.Handled = true;
}
}
else
{
e.Handled = true;
}
}
}
}
}
public class AnlogInt32TextBox : TextBox
{
protected override void OnKeyPress(KeyPressEventArgs e)
{
if ((e.KeyChar >= '0' && e.KeyChar <= '9') || (e.KeyChar == 8))
{
}
else
{
//可以在首个位置输入 -
if (e.KeyChar == '-' && this.SelectionStart == 0)
{
// return;
}
else
{
e.Handled = true;
}
}
}
}
}
特此记录
anlog
2023年2月9日