The method OnChar controls the input. Depending on the input it acts in different ways.:
void CHertzEdit::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
{
if (iscntrl(nChar))
{
CEdit::OnChar(nChar, nRepCnt, nFlags);
return;
}
CString str_nChar(nChar);
CString strZiel;
if (m_bNegativAllowed)
strZiel=str_nChar.SpanIncluding("0123456789kKmMhHzZ.-");
else
strZiel=str_nChar.SpanIncluding("0123456789kKmMhHzZ.");
// Ist nChar a valid input (s.a.) ?
if (strZiel.IsEmpty())
return;
// Ist nChar eine number ?
strZiel=str_nChar.SpanIncluding(".kKmMhHzZ");
if (strZiel.IsEmpty()) {
// yes // Base class
CEdit::OnChar(nChar, nRepCnt, nFlags);
return;
}
// Get the content of the control
int nTextLen=GetWindowTextLength();
char* pChar=new char[nTextLen+1]; // incl. abschliessender \0
GetWindowText(pChar, nTextLen+1);
CString strText(pChar);
delete[] pChar;
if (nChar=='.') {
// is there a period?
if (strText.Find('.')== -1) {
// no -> base class
CEdit::OnChar(nChar, nRepCnt, nFlags);
return;
} else {
// no -> ignore
return;
}
}
BOOL wasK, wasM, wasHz;
// remove the unit successive
if (strText.Remove('m') || strText.Remove('M')) wasM=TRUE;
else wasM=FALSE;
if (strText.Remove('k') || strText.Remove('K')) wasK=TRUE;
else wasK=FALSE;
if (strText.Remove('h') || strText.Remove('H')) wasHz=TRUE;
else wasHz=FALSE;
if ((strText.Remove('z') || strText.Remove('Z')) && wasHz)
wasHz=TRUE;
else
wasHz=FALSE;
strText.Remove(' ');
if (nChar=='k' || nChar=='K') {
strText+=" KHz";
SetWindowText(strText);
SetModify();
return;
}
if (nChar=='m' || nChar=='M') {
strText+=" MHz";
SetWindowText(strText);
SetModify();
return;
}
if (nChar=='h' || nChar=='H' || nChar=='z' || nChar=='Z') {
if (wasK) {
strText+=" KHz";
SetWindowText(strText);
SetModify();
return;
}
if (wasM) {
strText+=" MHz";
SetWindowText(strText);
SetModify();
return;
}
strText+=" Hz";
SetWindowText(strText);
SetModify();
return;
}
ASSERT(FALSE); // should never happen
}