Cedit控制,防止输入除特定号码之外的其他号码 [英] Cedit control, preventing input of other number except a particular number

查看:51
本文介绍了Cedit控制,防止输入除特定号码之外的其他号码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hi All,

How can I write override CEdit control so that control should allow only number 15 or number between 1 to 15.

Can anybody give some idea where I need to write code?

Code snippet would help.





问候,

欢乐


我尝试过:



尚未尝试,因为我不知道。



Regards,
Joy

What I have tried:

Not yet tried as I am not aware of.

推荐答案

最简单的方法是在控件上使用 ES_NUMBER 样式,然后捕获输入每次通过 ON_EN_UPDATE 通知输入字符时。然后,您可以验证用户输入的内容。请参阅 CEdit类 [ ^ ]和编辑控件样式 [ ^ ]。
The easiest way would be to use the ES_NUMBER style on the control, and then capture the input each time a character is entered via the ON_EN_UPDATE notification. You can then validate what the user has entered. See CEdit Class[^] and Edit Control Styles[^].


要仅允许输入数字,您可以设置 ES_NUMBER 样式(请参阅编辑控件样式(Windows) [ ^ ])创建控件时。这可以防止用户输入非数字字符。另请参见使用CEdit控件 [ ^ ]。



然后你必须决定何时检查无效输入:在打字或检索内容时。



后者是最简单的。只需使用 GetWindowText 获取文本,然后使用 _tstoi 转换为整数:

To allow only numbers to be entered you may set the ES_NUMBER style (see Edit Control Styles (Windows)[^]) when creating the control. This prevents the user from entering non-digit characters. See also Using the CEdit control[^].

Then you must decide when to check for invalid inputs: During typing or when retrieving the content.

The latter is the simplest. Just get the text using GetWindowText and convert to integer using _tstoi:
void CMyDialog::OnOK()
{
    CString strNumber = m_NumEdit.GetWindowText();
    int number = _tstoi(strNumber.GetString());
    if (number <= 0 || number > 15)
    {
        // Show error message here
        return;
    }
    CDialog::OnOK();
}





如果要在输入更改后检查输入(用户输入了另一个字符),可以使用 EN_CHANGE 通知。可以像上面的代码一样检查内容。由于内容已在屏幕上更新,因此仅用于设置或清除标记。



要在屏幕更新前检查输入,您可以使用 EN_UPDATE 通知。这可以用来表示有效性。



如果你想忽略按键操作会导致输入无效,你必须处理 WM_CHAR 消息并检查在当前位置插入按下的字符时结果字符串是否会导致无效的数字。



未经测试的示例:



If you want to check the input after it has been changed (the user has entered another character), you can use the EN_CHANGE notification. The content can be checked like with the above code. Because the content has been already updated on screen, this is only useful to set or clear a flag.

To check the input before the screen is updated you can use the EN_UPDATE notification. This can be for example used to indicate the validity.

If you want to ignore key presses when they would result in an invalid input, you must handle the WM_CHAR message and check if the resulting string when inserting the pressed char at the current position would result in an invalid number.

Untested example:

void CMyEdit::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
{
    // First check if digit. This check is already included when using ES_NUMBER.
    if (nChar < _T('0') || nChar > _T('9'))
        return;
    // Get current content
    TCHAR buf[16];
    GetWindowText(buf, 16);
    // Get selection and cursor position
    int start, end;
    GetCurSel(start, end);
    int selLength = end - start;
    // No selection when start == end.
    if (selLength == 0)
    {
        // Return if max. number of digits has been already entered
        if (_tcslen(buf) > 1)
            return;
        // Insert char at begin
        if (start == 0)
        {
            buf[1] = buf[0];
            buf[0] = static_cast<tchar>(nChar);
        }
        // Append char
        else
        {
            buf[1] = static_cast<tchar>(nChar);
        }
        buf[2] = 0;
    }
    else
    {
        int remaining = _tcslen(buf) - selLength;
        buf[start] = static_cast<tchar>(nChar);
        if (remaining > 0)
            memmove(buf + start + 1, buf + end, remaining + 1);
        else
            buf[end] = 0;
    }
    // Check for valid number
    int number = _tstoi(buf);
    if (number <= 0 || number > 15)
        return;
    CEdit::OnChar(nChar, nRepCnt, nFlags);
}</tchar></tchar></tchar>


这篇关于Cedit控制,防止输入除特定号码之外的其他号码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆