15 мая 2023 года "Исходники.РУ" отмечают своё 23-летие!
Поздравляем всех причастных и неравнодушных с этим событием!
И огромное спасибо всем, кто был и остаётся с нами все эти годы!

Главная Форум Журнал Wiki DRKB Discuz!ML Помощь проекту


Placing the caret in an Edit control at the right place

Gururaj Kulkarni -- gururajk@cdc.bflsl.soft.net
Wednesday, February 12, 1997


Hello Everybody,

Environment: MSVC 4.0 Win NT 4.0
     
     I have an Edit Control placed over a Dialog.  I have trapped the EN_UPDATE Notification for the same
Edit Control to do certain validation.  I have a set of characters(like {,},\ , | etc)  which are not to be allowed.
I have been able to do this successfully.  But the problem is with respect to placing the caret.
Say if the currently entered string is Gururaj after which an invalid character is entered, then I am able 
to place the caret at the end of the string entered.  Now if the user enters an invalid character 
after the character 'a' (Refer to the above string).  Still I am able to forcibly place the caret at the 
end.  I am doing this by getting the string length and pass it to the CEdits' SetSel() member function.
But my problem is to place the caret after the letter 'a' itself (Refer to the above string again)

So How do I control the placing of the Caret ?
Can some one help me out.

Cheers and Happy FAQing,
Gururaj





Burt -- blstampfl@software.rockwell.com
Wednesday, February 12, 1997

[Mini-digest: 5 responses]

Hi Gururaj,

    Wouldn't it be acceptable to subclass the edit control and in your
derived CEdit(??) class
implement an override of the OnChar() method.  Then just look for the
invalid characters in the
OnChar() and (for those invalid chars) just NOT call the base class
OnChar() ?  This way the 
cursor should never need to be repositioned (since it never got moved by
the insertion of
an invalid character).

Burt Stampfl     
internal ext. 32398
outside line (414)328-2398
BURT.STAMPFL@software.rockwell.com
BURT.STAMPFL@nconnect.net

>----------
>From: 	Gururaj Kulkarni[SMTP:gururajk@cdc.bflsl.soft.net]
>Sent: 	Wednesday, February 12, 1997 2:46 AM
>To: 	'mfc-l@netcom.com'
>Subject: 	Placing the caret in an Edit control at the right place
>
>
>Hello Everybody,
>
>Environment: MSVC 4.0 Win NT 4.0
>     
>     I have an Edit Control placed over a Dialog.  I have trapped the
>EN_UPDATE Notification for the same
>Edit Control to do certain validation.  I have a set of characters(like {,},\
>, | etc)  which are not to be allowed.
>I have been able to do this successfully.  But the problem is with respect to
>placing the caret.
>Say if the currently entered string is Gururaj after which an invalid
>character is entered, then I am able 
>to place the caret at the end of the string entered.  Now if the user enters
>an invalid character 
>after the character 'a' (Refer to the above string).  Still I am able to
>forcibly place the caret at the 
>end.  I am doing this by getting the string length and pass it to the CEdits'
>SetSel() member function.
>But my problem is to place the caret after the letter 'a' itself (Refer to
>the above string again)
>
>So How do I control the placing of the Caret ?
>Can some one help me out.
>
>Cheers and Happy FAQing,
>Gururaj
>
>
>
-----From: Bruce DeGraaf 

Gururaj,
I think you are going to have to build a new class, derived from CEdit,   
and overload the OnChar method (and maybe the OnKeyDown method too).   
Something like this (highly simplified, use the MFC Wizards rather than   
just type in the code):
class CEditNew : public CEdit
{
 //{{AFX_MSG(CEditVideo)
 afx_msg void OnChar(UINT nChar, UINT repCount, UINT flags);
 //}}AFX_MSG
};

void CEditNew::OnChar(UINT nChar, UINT repCount, UINT flags)
{
if (isalnum(nChar)) // or some other test for acceptability
 CEdit::OnChar(nChar, repCount, flags);
}

In your host dialog, where you have declared the edit box member   
variable, retype it from CEdit to CEditNew. Jeff Prosise in Programming   
Windows 95 with MFC mentions something like this on page 405.

It's not quite as frightening as it first appears.

Be aware that you are almost certainly going to have to overload   
OnKeyDown and maybe OnContextMenu as well. See if you can figure out what   
happens to the escape character keystroke (remember, it is a dialog based   
application).

Best!

 -----Original Message-----
From: owner-mfc-l [SMTP:owner-mfc-l@majordomo.netcom.com]
Sent: Wednesday, February 12, 1997 8:46 AM
To: 'mfc-l@netcom.com'
Subject: Placing the caret in an Edit control at the right place


Hello Everybody,

Environment: MSVC 4.0 Win NT 4.0
       

     I have an Edit Control placed over a Dialog.  I have trapped the
EN_UPDATE Notification for the same
Edit Control to do certain validation.  I have a set of characters(like   
{,},\
, | etc)  which are not to be allowed.
I have been able to do this successfully.  But the problem is with   
respect to
placing the caret.
Say if the currently entered string is Gururaj after which an invalid
character is entered, then I am able
to place the caret at the end of the string entered.  Now if the user   
enters
an invalid character
after the character 'a' (Refer to the above string).  Still I am able to
forcibly place the caret at the
end.  I am doing this by getting the string length and pass it to the   
CEdits'
SetSel() member function.
But my problem is to place the caret after the letter 'a' itself (Refer   
to
the above string again)

So How do I control the placing of the Caret ?
Can some one help me out.

Cheers and Happy FAQing,
Gururaj


-----From: "Benoit D'Oncieu" 

Sublcass the edit control by creating you own CEdit derivative with
ClassWizard,
then use the WM_CHAR message handler (OnChar) to prevend the user to
enter
invalid characters.

afx_msg void CMyEdit::OnChar( UINT nChar, UINT nRepCnt, UINT nFlags )
{
	int nStart, nEnd;
	BOOL bAllow = FALSE;
	CString strPrevText, strNewText;

	GetSel(nStart, nEnd);
	GetWindowText(strPrevText);

	if (nStart != 0)
	{
		if (nStart == -1)
			strNewText = strPrevText;
		else
			strNewText = strPrevText.Left(nStart);
	}
	strNewText += CString((TCHAR)nChar , nRepCnt);
	if (nEnd != -1)
		strNewText += strPrevText.Mid(nEnd);

	// Do your own processing here to determine if new string is valid
	// ...

	if (bAllow)
		CEdit::OnChar(nChar, nRepCnt, nFlags);
}

________________________________________

              _\|/_          Benoit d'Oncieu,
             (o   o)         Senior Developper
    ---oOO-(_)-OOo--------------------------------------------
    -
    ADWAYS - The net for the advertising media
    6 rue Godefroy
    92800 PUTEAUX
    FRANCE
    Tel: +33 (1) 97 56 82
    Fax: +33 (1) 97 56 57
    MailTo:bdoncieu@adways.com
________________________________________



>-----Original Message-----
>From:	Gururaj Kulkarni [SMTP:gururajk@cdc.bflsl.soft.net]
>Sent:	mercredi 12 fevrier 1997 09:46
>To:	'mfc-l@netcom.com'
>Subject:	Placing the caret in an Edit control at the right place
>
>
>Hello Everybody,
>
>Environment: MSVC 4.0 Win NT 4.0
>     
>     I have an Edit Control placed over a Dialog.  I have trapped the
>EN_UPDATE Notification for the same
>Edit Control to do certain validation.  I have a set of characters(like
>{,},\ , | etc)  which are not to be allowed.
>I have been able to do this successfully.  But the problem is with
>respect to placing the caret.
>Say if the currently entered string is Gururaj after which an invalid
>character is entered, then I am able 
>to place the caret at the end of the string entered.  Now if the user
>enters an invalid character 
>after the character 'a' (Refer to the above string).  Still I am able
>to forcibly place the caret at the 
>end.  I am doing this by getting the string length and pass it to the
>CEdits' SetSel() member function.
>But my problem is to place the caret after the letter 'a' itself (Refer
>to the above string again)
>
>So How do I control the placing of the Caret ?
>Can some one help me out.
>
>Cheers and Happy FAQing,
>Gururaj
>
>
-----From: Jim Lawson Williams 

At 08:46 12-02-97 +-5-30, Gururaj Kulkarni 
wrote:
>
>Hello Everybody,
>
>Environment: MSVC 4.0 Win NT 4.0
>     
>     I have an Edit Control placed over a Dialog.  I have trapped the
EN_UPDATE Notification for the same
>Edit Control to do certain validation.  I have a set of characters(like
{,},\ , | etc)  which are not to be allowed.
>I have been able to do this successfully.  But the problem is with respect
to placing the caret.
>Say if the currently entered string is Gururaj after which an invalid
character is entered, then I am able 
>to place the caret at the end of the string entered.  Now if the user
enters an invalid character 
>after the character 'a' (Refer to the above string).  Still I am able to
forcibly place the caret at the 
>end.  I am doing this by getting the string length and pass it to the
CEdits' SetSel() member function.
>But my problem is to place the caret after the letter 'a' itself (Refer to
the above string again)
>
>So How do I control the placing of the Caret ?
>Can some one help me out.
>
>Cheers and Happy FAQing,
>Gururaj
>
G'day!
You might consider subclassing the edit and examining the characters as
they go by, rejecting those which are unacceptable.  That way you will have
no need to worry about the caret:  it won't move. See Mike B.'s "nabbed
edit" from Chapter 7 of "The Revolutionary Guide...".  The style ES_NUMBER
would be better described as "ES_POSITIVE_INTEGER", so that example helped
me deal with signed numbers, possibly containing fractions.  A bit similar
to your case: + or - only in the lead position;  one-only decimal
place-signifier;  bounce other non-digits.
Regards,
Jim LW 


>From the BBC's "Barchester Chronicles":

    "I know that ultimately we are not supposed to understand.
    But I also know that we must try."

       -- the Reverend Septimus Harding, 
          tax-consultant, crypt-analyst, clog-dancer, C++ programmer
-----From: splinta@cix.compulink.co.uk (Philip Beck)

In-Reply-To: <01BC18C1.5B523440@plutonium.bfl.soft.net>
>So How do I control the placing of the Caret ?

I used the following to place the caret at the beginning :

pMyEdit->SetSel(0,0,FALSE);

Phil.





Become an MFC-L member | Вернуться в корень Архива |