CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 13 of 13

Thread: CEdit

  1. #1
    Join Date
    Aug 2004
    Posts
    8

    Question 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.

  2. #2
    Join Date
    Sep 2002
    Posts
    924
    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.

  3. #3
    Join Date
    Apr 2002
    Location
    St.Petersburg, Russia
    Posts
    714
    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

  4. #4
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430
    Quote 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

  5. #5
    Join Date
    Aug 2004
    Posts
    8
    Thanks all, you have given me plenty to chew on

  6. #6
    Join Date
    Aug 2004
    Posts
    8
    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.

  7. #7
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360
    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.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  8. #8
    Join Date
    Aug 2004
    Posts
    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

  9. #9
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360
    Quote 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.

    Quote Originally Posted by reng
    Ive done the hex one aswell
    See? Wasn't so hard...
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  10. #10
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430
    Quote 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);
    }

  11. #11
    Join Date
    Aug 2004
    Posts
    8
    "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

  12. #12
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360
    And if you want a fool-proof program you have to take care of the numpad keys.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  13. #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
  •  





Click Here to Expand Forum to Full Width

Featured