c#只能输入数字控件

实现机理

主要是通过复写

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日

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值