zoukankan      html  css  js  c++  java
  • MFC控件 Edit,控制只能输入数字和小数点

    照抄这个的

    MFC实现Edit输入限制(只允许输入数字,小数点)

    Edit 控件的属性Number,只能控制只输入数字,不能控制输入小数的情况,实现这个就继承CEdit来写新的类

    .h 代码

     1 #pragma once
     2 
     3 // CEditEx
     4 
     5 class CEditEx : public CEdit
     6 {
     7     DECLARE_DYNAMIC(CEditEx)
     8 
     9 public:
    10     CEditEx();
    11     virtual ~CEditEx();
    12 
    13 protected:
    14     DECLARE_MESSAGE_MAP()
    15 public:
    16     afx_msg void OnChar(UINT nChar, UINT nRepCnt, UINT nFlags);
    17     afx_msg void OnKeyUp(UINT nChar, UINT nRepCnt, UINT nFlags);
    18 };

    .cpp

     1 // EditEx.cpp : 实现文件
     2 //
     3 
     4 #include "stdafx.h"
     5 #include "CEditEx.h"
     6 
     7 // CEditEx
     8 
     9 IMPLEMENT_DYNAMIC(CEditEx, CEdit)
    10 
    11 CEditEx::CEditEx()
    12 {
    13 }
    14 
    15 CEditEx::~CEditEx()
    16 {
    17 }
    18 
    19 BEGIN_MESSAGE_MAP(CEditEx, CEdit)
    20     ON_WM_CHAR()
    21     ON_WM_KEYUP()
    22 END_MESSAGE_MAP()
    23 
    24 // CEditEx 消息处理程序
    25 void CEditEx::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
    26 {
    27     // TODO:  在此添加消息处理程序代码和/或调用默认值
    28 
    29     // 处理小数点
    30     if (nChar == '.') 
    31     {
    32         CString str;
    33         GetWindowText(str);
    34 
    35         // 限制第一位为小数
    36         if (str.GetLength() == 0) 
    37         {
    38             // 第一位输入小数点
    39             MessageBox(_T("第一位不可以是小数点"));
    40             return;
    41         }
    42         // 限制只允许有一个小数点
    43         if (str.Find('.') == -1) 
    44         {
    45             CEdit::OnChar(nChar, nRepCnt, nFlags);
    46         }
    47         else 
    48         {
    49             if (str[0] == '.')
    50             {
    51                 SetWindowText(str.Mid(1, str.GetLength()));
    52                 MessageBox(_T("第一位不可以是小数点"));
    53             }
    54             // 小数点出现第二次
    55             MessageBox(_T("小数点只能输入一次"));
    56         }
    57     }
    58     // 数理数字和退格键
    59     else if ((nChar >= '0' && nChar <= '9') || nChar == 0x08)
    60     {
    61         CEdit::OnChar(nChar, nRepCnt, nFlags);
    62     }
    63     else 
    64     {
    65         // 出现非数字键,退格键
    66         MessageBox(_T("只能输入数字,退格键"));
    67     }
    68 }
    69 
    70 // 修复先输入数字之后,可以在第一位输入小数点
    71 void CEditEx::OnKeyUp(UINT nChar, UINT nRepCnt, UINT nFlags)
    72 {
    73     // TODO: 在此添加消息处理程序代码和/或调用默认值
    74     if (nChar == VK_DECIMAL || nChar == VK_OEM_PERIOD) {
    75         CString str;
    76         GetWindowText(str);
    77         if (str[0] == '.') {
    78             SetWindowText(str.Mid(1, str.GetLength()));
    79             MessageBox(_T("第一位不可以是小数点"));
    80         }
    81     }
    82     CEdit::OnKeyUp(nChar, nRepCnt, nFlags);
    83 
    84 }

    这个代码有个bug,第二次输入 . 时提示 “小数点只能输一次”,这个没问题,但第三次输入 . 时,提示 “只能输入小数,退格键”

    是不是第二次输入的小数点没有处理,第三次输入后nChar的值就不是输入的值了,但不影响功能,就不改了

  • 相关阅读:
    凤凰网CEO刘爽称鄙视微博连说20个屁示不齿
    科技创业网站
    梦想,在路上
    小本创业
    rails relevent
    MBA = married but available
    把UTF8编码转换为GB2312编码[转]
    FSFS和VDFS存储方式的区别
    如何让phpmyadmin输入密码再进入
    dede后台登陆后一片空白的解决办法汇总
  • 原文地址:https://www.cnblogs.com/ckrgd/p/14686680.html
Copyright © 2011-2022 走看看