[英]Block characters in Edit Control using MFC (Example for only floats)
我试图通过覆盖OnChar和OnKeydown阻止某些类型的字符插入到我的编辑控件中。 我试图阻止多个点“。” 还有不是数字的任何东西
首先,我检查是否已经有一个'。 通过将对话框类中定义的变量设置为false来获得具有焦点的Edit控件中的内容:
void MyMainDialog::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
CWnd * eb1 = GetDlgItem(IDC_EDIT1); //Reference dimension 1 box;
CWnd * eb2 = GetDlgItem(IDC_EDIT2); //Reference dimension 2 box
CWnd * eb3 = GetDlgItem(IDC_EDIT3); //Reference dimension 3 box
CString temp;
CWnd * focusedHand = MyMainDialog::GetFocus(); //Reference edit box being focused
if(focusedHand == eb1)
{
eb1->GetWindowTextA(temp);
if(temp.Find('.') != -1)
checkPoint = true;
else
checkPoint = false;
}
else if(focusedHand == eb2)
{
eb2->GetWindowTextA(temp);
if(temp.Find('.') != -1)
checkPoint = true;
else
checkPoint = false;
}
else if(focusedHand == eb3)
{
eb3->GetWindowTextA(temp);
if(temp.Find('.') != -1)
checkPoint = true;
else
checkPoint = false;
}
CDialogEx::OnKeyDown(nChar, nRepCnt, nFlags);
}
在OnChar,我正在检查要键入的字符。 如果不是一个点,但已经有一个点,那么我就不会从CDialog调用OnChar:
void MyMainDialog::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
{
if(nChar == '.' && checkPoint == false) //Is a point and there is no other point
{
CDialogEx::OnChar(nChar, nRepCnt, nFlags);
}
if((nChar < '0' || nChar > '9')) //Is not a number
{
//Show message to user
}
else //Is a number
{
CDialogEx::OnChar(nChar, nRepCnt, nFlags);
}
}
好吧,我的代码无法正常工作。 它可以编译,并且在编辑控件中键入时不会崩溃,但是它根本不执行任何操作。 我想知道覆盖它的正确方法是防止调用CDialogEx :: OnChar()还是应该使nChar = 0以便显示的字符为null。 但是最重要的是,我尝试在OnChar上显示的消息也没有显示,这意味着甚至没有调用MyMainDialog :: OnChar()。 我应该改写CDialogEx :: OnChar()吗?
感谢您的关注
我找到了解决方案。 代码似乎在我的应用程序中没有起作用的原因是因为它仅对MyMainDialog起作用。 在编辑控件中键入内容时,如果从CEdit中调用了OnChar(),则这是我必须拦截的一个。 您不允许覆盖CEdit :: OnChar()。 解决方案是创建一个从CEdit派生的类,然后从该类中拦截OnChar()。 您还必须使用类而不是CEdit来操作“编辑控件”。
然后将我的代码重新编写为:
OnChar():
void CustomEdit::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
{
if(nChar == '.' && checkPoint == true) //Is a point and there is already a point
{
//Show message
}
else if(nChar == '.' && checkPoint == false) //Is a point but there is no point
{
CEdit::OnChar(nChar, nRepCnt, nFlags);
}
if((nChar < '0' || nChar > '9') && nChar != 8) //Is not a number or backspace
{
//Show message
}
else //Valid
{
CEdit::OnChar(nChar, nRepCnt, nFlags);
}
}
的onkeydown():
void CustomEdit::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
CString temp;
this->GetWindowTextA(temp);
if(temp.Find('.') == -1)
checkPoint = false; //There is no point
else
checkPoint = true; //There is a point
CEdit::OnKeyDown(nChar, nRepCnt, nFlags);
}
希望这对遇到类似问题的人有所帮助。 顺便说一下,互联网上有很多课程和自动化解决方案,我出于学习目的手动进行了此操作。
一种更简单的方法是使用OnChange事件处理您的输入:
// do not add numbers; ascci numbers is 48 - 57
if ((m_strYOURCONTROL[m_strYOURCONTROL.GetLength() - 1]) > 47 &&
m_strYOURCONTROL[m_strYOURCONTROL.GetLength() - 1]) < 58)
{
m_strYOURCONTROL = m_strYOURCONTROL.Mid(0, m_strYOURCONTROL.GetLength() - 1);
}
这将不允许数字。 通过此实现,您可以更轻松地处理对编辑框的输入
另一种解决方案是在PreTranslateMessage中更早地处理WM_CHAR
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.