|
-
August 16th, 2004, 02:45 AM
#1
CEdit
Hi, Im coding an application where the input to a edit box needs to be restricted to base 16 and another to base 2.. After a good look around google i cant see how you do this in mfc ? +is there a way to get a keypress event upon a charateur being submitted into the edit control
Thanks for any help.
-
August 16th, 2004, 03:06 AM
#2
You can derive your own class from CEdit and handle the OnChar function (WM_CHAR message). In that function you can check which key was pressed and and then do the appropriate action. i.e. To pass the key on unchanged call the base class CEdit::OnChar function. To change the character to a different character, call DefWindowProc with WM_CHAR and the character you would like to change it to, etc. If you would like to ignore the key, do nothing (or play a beep, etc, but do not call the base class function or DefWindowProc). Do a search for CEdit and OnChar and you should be bale to find examples.
-
August 16th, 2004, 03:08 AM
#3
Edit control sends notification message to it's parent window each time user presses a key. You can handle EN_CHANGE or EN_UPDATE notifications and check contents of editbox. If there are illegal symbols in it - just delete them. The most simple way to do it is to use CString class. It has function SpanIncluding() that returns string, consisting only from specified characters. You can use SpanIncluding("01") for a base of 2 and SpanIncluding("0123456789abcdefABCDEF") for a base of 16.
With best wishes, Alexander Hritonenkov
-
August 16th, 2004, 03:09 AM
#4
 Originally Posted by reng
Hi, Im coding an application where the input to a edit box needs to be restricted to base 16 and another to base 2.. After a good look around google i cant see how you do this in mfc ? +is there a way to get a keypress event upon a charateur being submitted into the edit control
Thanks for any help.
Create your own class derived from CEdit, add WM_CHAR message handler and allow only 0-9, A-F (0,1 - for binary) charactrers to be acceptable.
See also this J.Newcomer's article: A Validating Edit Control
-
August 16th, 2004, 04:09 AM
#5
Thanks all, you have given me plenty to chew on
-
August 16th, 2004, 09:04 AM
#6
Ive chewed and ive chewed, then some after some more chewing i really dont get exactly what i have todo to enable a single char being intercepted through a CEdit control.
I have used onChange to filter it, i can even go through the string and filter the input but its not ideal becuase my edit boxes are hex->binary and binary->hex...
If anyone could give any further tips just to get me started i would be grateful... (btw: i did read that article but he filters the input onChange...)
Thanks.
..
So i created my own class with CEdit as the base class, how do i utilise this now with the CEdit controls ? It must be something stupid im missing... :S
void CEditEx::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
{
CString strLine;
strLine.Format(_T("Char = %i\r\n"), nChar);
MessageBox(strLine,"CEditEx OnChange",MB_OK);
CEdit::OnChar(nChar, nRepCnt, nFlags);
}
Last edited by reng; August 16th, 2004 at 09:20 AM.
-
August 16th, 2004, 09:17 AM
#7
You have to chew less and pay more attention to the indications.
First, I would go with 2 derives of CEdit, one for the hex (CHexEdit) and one for the binary (CBinaryEdit). In these derives create a handle for WM_CHAR. The handle for CHexEdit should allow only 0-9, A-F, a-f, and the handler for CBinaryEdit should allow only 0-1. The other charactes must be dismissed.
Code:
void CBinaryEdit::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
{
if(nChar == 0x30 || nChar == 0x31)
CEdit::OnChar(nChar, nRepCnt, nFlags);
}
In the class you use it, include the header and a using class wizzard (for example) a control variable for your edit. Go to the AFX_DATA map and replace CEdit with CBinaryEdit.
Code:
//{{AFX_DATA(CGeneralTestDlgDlg)
enum { IDD = IDD_GENERALTESTDLG_DIALOG };
CBinaryEdit m_edit;
//}}AFX_DATA
Compile it an run it.
Hope that for the Hex you'll do it yourself.
-
August 16th, 2004, 09:23 AM
#8
Ahhhhhhhhhhhh The penny has well and truly dropped 
Thank you..
Cant believe i struggled with that one.... eek
Ive done the hex one aswell
-
August 16th, 2004, 09:32 AM
#9
 Originally Posted by reng
Ahhhhhhhhhhhh The penny has well and truly dropped 
If this is an enlish expression (totally unknown to me ) please tell me what it means.
 Originally Posted by reng
Ive done the hex one aswell
See? Wasn't so hard...
-
August 16th, 2004, 09:50 AM
#10
 Originally Posted by cilu
You have to chew less and pay more attention to the indications.
First, I would go with 2 derives of CEdit, one for the hex (CHexEdit) and one for the binary (CBinaryEdit). In these derives create a handle for WM_CHAR. The handle for CHexEdit should allow only 0-9, A-F, a-f, and the handler for CBinaryEdit should allow only 0-1. The other charactes must be dismissed.
...........
You should also allow VK_BACK, VK_DELETE and, probably, if you want to use copy/paste/delete/clear/... commands, - Ctrl-A, Ctrl-C,...:
Code:
void CDigit16Edit::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
{
switch (nChar)
{
case _T('0'):
case _T('1'):
case _T('2'):
case _T('3'):
case _T('4'):
case _T('5'):
case _T('6'):
case _T('7'):
case _T('8'):
case _T('9'):
case _T('a'):
case _T('A'):
case _T('b'):
case _T('B'):
case _T('c'):
case _T('C'):
case _T('d'):
case _T('D'):
case _T('e'):
case _T('E'):
case _T('f'):
case _T('F'):
break;
case VK_BACK:
break;
case 0x01:
// Ctrl-A => handle SELECT_ALL
SetSel(0, -1);
return;
case 0x03:
// Ctrl-C => handle WM_COPY
Copy();
return;
case 0x16:
// Ctrl-V => handle WM_PASTE
Paste();
return;
case 0x18:
// Ctrl-X => handle WM_CUT
Cut();
return;
case 0x1A:
// Ctrl-Z => handle ID_EDIT_UNDO (EM_UNDO)
if(CanUndo())
Undo();
return;
case VK_DELETE:
// Ctrl-Z => handle WM_CLEAR
Clear();
return;
default:
MessageBeep(0);
return;
}
CEdit::OnChar(nChar, nRepCnt, nFlags);
}
-
August 17th, 2004, 02:01 AM
#11
"The penny has well and truly dropped"
"If this is an enlish expression (totally unknown to me ) please tell me what it means."
hehe, It means that everything has suddenly become clear and you now understand it..
"You should also allow VK_BACK, VK_DELETE and, probably, if you want to use copy/paste/delete/clear/... commands, - Ctrl-A, Ctrl-C,...:"
Yeah i did to begin with just allow chars under 32 for this but i have since filtered only the correct ones like your example, thanks
-
August 17th, 2004, 02:33 AM
#12
And if you want a fool-proof program you have to take care of the numpad keys.
-
August 17th, 2004, 02:51 AM
#13
a suggestion
isxdigit() function can be used to test if it is a character used to represent a hexadecimal number instead of the big switch case statement in the sample code given
Regards,
Sreedharan
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|