Trouble subclassing CEdit and OnChar

I'm trying to create a subclassed CEdit to do some input filtering.
One thing that I want to do is convert spaces to underscores.  I tried
just modifying the nChar value that I'm passing to CEdit::OnChar, but
the original character (space) goes through instead of the character
I'm substituting.

How do I substitute one input character for another in a Subclassed
CEdit?

Code:
void CMyEdit::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
{
	if (nChar == VK_SPACE) nChar = '_';

	// Limit entry to alphanumeric and the underscore
	if (isalnum(nChar) ||
		(nChar == '_') ||
		(nChar == VK_DELETE) ||
		(nChar == VK_BACK))
	{
		CEdit::OnChar(nChar, nRepCnt, nFlags);
	}
}

0
4/21/2006 9:39:57 PM
vc.mfc 33608 articles. 0 followers. Follow

6 Replies
1217 Views

Similar Articles

[PageSpeed] 40

Just a WAG, but:

replace:
if (nChar == VK_SPACE) nChar = '_';

with:
if (nChar == VK_SPACE){
    SendMessage(WM_CHAR,  '_', 1L);
    return;
}

HTH,
Drew

<aaron.m.johnson@gmail.com> wrote in message 
news:1145655597.305509.255780@i39g2000cwa.googlegroups.com...
> I'm trying to create a subclassed CEdit to do some input filtering.
> One thing that I want to do is convert spaces to underscores.  I tried
> just modifying the nChar value that I'm passing to CEdit::OnChar, but
> the original character (space) goes through instead of the character
> I'm substituting.
>
> How do I substitute one input character for another in a Subclassed
> CEdit?
>
> Code:
> void CMyEdit::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
> {
> if (nChar == VK_SPACE) nChar = '_';
>
> // Limit entry to alphanumeric and the underscore
> if (isalnum(nChar) ||
> (nChar == '_') ||
> (nChar == VK_DELETE) ||
> (nChar == VK_BACK))
> {
> CEdit::OnChar(nChar, nRepCnt, nFlags);
> }
> }
> 


0
4/21/2006 10:37:24 PM
This is due to a piece of brain-dead thinking on the part of Microsoft.  When you call
CEdit::OnChar, it ****IGNORES**** the parameters you pass, and instead passes the
parameters that came in with the original message!  (This is an interpretation of OO
programming and inheritance of which I have been previously unaware).  Read carefully that
disclaimer paragraph that tells you this.  I got that in after beating up the MFC folk
many years ago at a Microsoft conference.  Rather than fix the problem, they documented
the fact that they were clueless about the design of OO systems!

In this case, you can cheat horribly:

void CMyEdit::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
    {
     if(nChar == _T(' '))
        SendMessage(WM_CHAR, _T('_'), MAKELONG(nRepCnt, nFlags));
    else
        ... as per your code

How anyone could make a blunder this mind-numbing escapes me entirely.  Their
justification was that "it makes MFC smaller".  This is optimizing the wrong problem.  An
optimization that produces the wrong answer is not an optimization, it is a bug.
					joe


On 21 Apr 2006 14:39:57 -0700, aaron.m.johnson@gmail.com wrote:

>I'm trying to create a subclassed CEdit to do some input filtering.
>One thing that I want to do is convert spaces to underscores.  I tried
>just modifying the nChar value that I'm passing to CEdit::OnChar, but
>the original character (space) goes through instead of the character
>I'm substituting.
>
>How do I substitute one input character for another in a Subclassed
>CEdit?
>
>Code:
>void CMyEdit::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
>{
>	if (nChar == VK_SPACE) nChar = '_';
>
>	// Limit entry to alphanumeric and the underscore
>	if (isalnum(nChar) ||
>		(nChar == '_') ||
>		(nChar == VK_DELETE) ||
>		(nChar == VK_BACK))
>	{
>		CEdit::OnChar(nChar, nRepCnt, nFlags);
>	}
>}
Joseph M. Newcomer [MVP]
email: newcomer@flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
-- 
NewsGuy.Com 30Gb $9.95 Carry Forward and On Demand Bandwidth
0
newcomer (15972)
4/22/2006 5:41:02 AM
Joseph M. Newcomer wrote:

> This is due to a piece of brain-dead thinking on the part of Microsoft.  When you call
> CEdit::OnChar, it ****IGNORES**** the parameters you pass, and instead passes the
> parameters that came in with the original message!  (This is an interpretation of OO
> programming and inheritance of which I have been previously unaware).  Read carefully that
> disclaimer paragraph that tells you this.  I got that in after beating up the MFC folk
> many years ago at a Microsoft conference.  Rather than fix the problem, they documented
> the fact that they were clueless about the design of OO systems!
> 

Joe:

I absolutely agree with you that this was a stupid piece of design, but 
I also find that warning in the documentation confusing (at least I used 
to). Suppose, for example, I have an SDI application and I handle the 
WM_SIZE message in the CMainFrame(), and that in the handler I call 
CFrameWnd::OnSize() with altered parameters. CFrameWnd::OnSize() is 
coded thus:

void CFrameWnd::OnSize(UINT nType, int cx, int cy)
{
   CWnd::OnSize(nType, cx, cy);    // important for MDI Children
   if (nType != SIZE_MINIMIZED)
     RecalcLayout();
}

I used to think: how can it be that this method does not use the altered 
parameters? This seems to violate the rules of the C++ language.  The 
answer (I came to realize) is that, yes, of course the base class 
CFrameWnd::OnSize() does receive the modified parameters, and it does 
use the modified nType in its own code, and it does pass the modified 
parameters on to CWnd::OnSize(). The trouble is that CWnd::OnSize() 
ignores the parameters passed to it, and uses the original parameters. 
So maybe the warning should read:

"If you call the base-class implementation of this function, and this 
results in a call to the corresponding CWnd implementation, that 
implementation will use the parameters originally passed with the 
message and not the parameters you supply to the function."

David Wilkinson

0
no-reply8010 (1791)
4/22/2006 12:08:44 PM
Yes, and THIS is one of the reasons the Microsoft design decision was totally brain-dead!
It is VERY confusing, because the reference to the "base class" actually refers to the
lowest-level class before DefWindowProc.  So all the way down to the bottom it gives the
illusion of working.

Sometimes I think MFC was designed by some people who had once heard of C++, and had spent
a week learning C++ before they designed and implemented MFC.
					joe

On Sat, 22 Apr 2006 08:08:44 -0400, David Wilkinson <no-reply@effisols.com> wrote:

>Joseph M. Newcomer wrote:
>
>> This is due to a piece of brain-dead thinking on the part of Microsoft.  When you call
>> CEdit::OnChar, it ****IGNORES**** the parameters you pass, and instead passes the
>> parameters that came in with the original message!  (This is an interpretation of OO
>> programming and inheritance of which I have been previously unaware).  Read carefully that
>> disclaimer paragraph that tells you this.  I got that in after beating up the MFC folk
>> many years ago at a Microsoft conference.  Rather than fix the problem, they documented
>> the fact that they were clueless about the design of OO systems!
>> 
>
>Joe:
>
>I absolutely agree with you that this was a stupid piece of design, but 
>I also find that warning in the documentation confusing (at least I used 
>to). Suppose, for example, I have an SDI application and I handle the 
>WM_SIZE message in the CMainFrame(), and that in the handler I call 
>CFrameWnd::OnSize() with altered parameters. CFrameWnd::OnSize() is 
>coded thus:
>
>void CFrameWnd::OnSize(UINT nType, int cx, int cy)
>{
>   CWnd::OnSize(nType, cx, cy);    // important for MDI Children
>   if (nType != SIZE_MINIMIZED)
>     RecalcLayout();
>}
>
>I used to think: how can it be that this method does not use the altered 
>parameters? This seems to violate the rules of the C++ language.  The 
>answer (I came to realize) is that, yes, of course the base class 
>CFrameWnd::OnSize() does receive the modified parameters, and it does 
>use the modified nType in its own code, and it does pass the modified 
>parameters on to CWnd::OnSize(). The trouble is that CWnd::OnSize() 
>ignores the parameters passed to it, and uses the original parameters. 
>So maybe the warning should read:
>
>"If you call the base-class implementation of this function, and this 
>results in a call to the corresponding CWnd implementation, that 
>implementation will use the parameters originally passed with the 
>message and not the parameters you supply to the function."
>
>David Wilkinson
Joseph M. Newcomer [MVP]
email: newcomer@flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
-- 
NewsGuy.Com 30Gb $9.95 Carry Forward and On Demand Bandwidth
0
newcomer (15972)
4/22/2006 3:18:56 PM
Wow, thanks for the help guys.  This has to be the single dumbest thing
I've ever run into in the Windows API (I'm sure that simply betrays
some inexperience).

Again, thanks.  This would have taken a while to figure out on my own.

0
4/24/2006 1:40:27 PM
Actually, this isn't the Windows API.  It is the MFC class implementation, a completely
separate issue.
					joe

On 24 Apr 2006 06:40:27 -0700, "Fish in a Barrel" <aaron.m.johnson@gmail.com> wrote:

>Wow, thanks for the help guys.  This has to be the single dumbest thing
>I've ever run into in the Windows API (I'm sure that simply betrays
>some inexperience).
>
>Again, thanks.  This would have taken a while to figure out on my own.
Joseph M. Newcomer [MVP]
email: newcomer@flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
-- 
NewsGuy.Com 30Gb $9.95 Carry Forward and On Demand Bandwidth
0
newcomer (15972)
5/1/2006 4:37:50 PM
Reply:

Similar Artilces:

Email trouble with New User in Active Directory
Below is a trail of posts that have been made. My problem has yet to resolved and I need some help quickly. The only other thing I can think of is to reinstall Exchange Server 2003. Hopefully someone will know the answer. Thanks! Subject: Re: Email not Setup when new user created in Active Directory From: "Bryan Hill" <bthill@comcast.net> Sent: 8/15/2004 12:30:21 PM We have other clients that are working fine. Just when we try to set up new ones - it will create the SMTP address but not the X400 address. I tried manually forcing the X400 but it will...

Trouble with the template.
I want to reset the 'normal' template to default to "Times New Roman" 10point font, and the pages numbered centre top. This seems to work when I set and reopen Word2003 from the Administrator Template vis: C:\Documents and Settings\Administrator\Application Data\Microsoft\Templates. But it will not work when I open Word2003 normally from the desktop. I have deleted the "Normal.dot" from the following and copied in the "Normal.dot" from the Administrator above: C:\Documents and Settings\All Users\Application Data\Microsoft\Templates...

trouble with CrmIsapi.dll
I was experiencing the same problem with the SDK as many have posted to this group ... "the WhoAmI() call results in a http error 405 when running sample code" I followed some of the advice; checked my .srf mapping to CrmIsapi.dll and re-registered the CrmIsapi.dll. when I re-register CrmIsapi.dll I get an error 80004005 - unspecified. ??? I copied it to various places and retried it ... same result. I got the original from the disk and retried ... same result. unmapped it from .srf, restarted and retried to register it ... same result. Anyone experienced this. What's up with m...

unexpected conversion trouble
I wanted to open an Excel file I hadn't been working on for a while, an Excel couldn't do it. After some investigations, I found that even if the file still had th .xls extension, it seemed to be conveted in .doc format (in fact, whe I open it in notepad, I can see at the end some words that look like MSWord signature). I don't know how this happened, but I'm reall embarassed beacause i really need the data in this file. I tried to change the extension to .doc, open the file in Word and the import in Excel, but I can hardly see part of the file. What's more, i is a 3-she...

Trouble with Outlook 2003 and PST File
Hi, We have a user who is using Outlook 2003 and moves most emails to a PST file which is considered archived email data. They have reached the Max limited on this PST file of 1.99GBs and its not letting them open the PST file. It is trying to repair it when they try to open the PST file. Here is the message they receive: "Can't move the items. The file <path>\<filename>.pst has reached its maximum size. To reduce the amount of data in this file, select some items that you no longer need, and then permanently delete them." I can't even get it open to delete...

Trouble with Pivot Tables & Named Ranges
Can I use a dynamic named range setup in one file as a range in pivot tables in another file? When I have the data source file (with the named range) and the pivot report files open, the pivot reports update just fine. But the moment I close the source data, the pivot reports no longer can update and I get a message that the source can't be found. If I reopen the source, the pivot tables work fine again - but as soon as I close it up, the reports no longer work. The source file is very large and I don't want to open it every time I have to update all the reports tha...

I am in trouble
Hi, I am a student in Cambodia.I don't have any girlfriend. I tried everything. I looked at them wrote poem to them it doesnt work. I got an account from facebook, hi5 friendster myspace. I have 0 girl friend. What should I do? I am so desperate. I am good looking and 250 kg only. Please help me or at least send me some foood I am hungry Best Regards Hungry Guy __________ Information from ESET NOD32 Antivirus, version of virus signature database 4944 (20100314) __________ The message was checked by ESET NOD32 Antivirus. http://www.eset.com You're tr...

Trouble sorting a table???
Using Excel 2002) I am relatively new to Excel and can only really perform simple functions in a basic spreadsheet. The spreadsheet I'm having trouble with is pretty basic, but I'm having trouble sorting some of the results from a table. (I'll try and explain as best as I can) I have 10 people that I'm tracking 5 different things for (dates, debts, credits, items, number of transactions, and their balance as calculated in this table by simple SUM functions) I then have another table with these same 10 people with their total balance. This is the table I'm having t...

trouble
I have been using my MS Publisher 2000 for over 3 years. Just recently I attempted to access the Publisher program and got the following message: "This application must be installed to run. Please run setup fom the location where you originally installed the application." I have done everything I know to fix this including uninstalling and reinstalling the program. I don't understand what happened overnight nor do I understand the instruction "run from the location you originally installed the program". I can not access the numerous publicaions that I have cr...

Trouble with "time" in formulas
I have a time sheet that looks something like this: A1=time...A2=time...A3=A2-A1+if(A1/24,1)...A4=if(or(A3<time(0,15,0),A3>time(4,0,0)),A3,time(4,0,0)) A1 and A2 are set up as dropdown lists and the times in the list are i increments of 15 minutes. WHat I'm trying to accomplish is (in cell A4 if A3 is between 15 minutes and 4 hours, then return 4 hours. if A3 i 0 or greater than 4 hours, return A3. If I enter enter times in A1 around 00:00 and A2 a little after 00:0 it works fine... example: A1=22:00...A2=01:00...A3=03:00...A4=04:00 A1=20:30...A2=00:15...A3=03:45...A4=04:00 A1=00...

Having trouble
I have a set of numbers, a-e. A usually equals e but can be less. B remains constant. C is the sum of A and B. C cannot exceed D. D remains constant I have tried the following formula in the box for A, but C ends up being less than D when I want it to equal D =IF(C>D,E-(C-D)) What am I doing wrong? Uhh, I'm a little slow, but; If you are putting this formula in A and it's the same "A" that is part of the "C=Sum A & B" then I think you have a circular reference. In other words, you're asking xl to resolve "A" by asking "If C> D&quo...

Print Preview troubles
When I do print preview, the information in the scratch area gets overlaid on the document, and there are text boxes where there are none in the work area. In the work area mode, the docs look correct and the stuff I have in the scratch area is OK. It is a scrambled mess. I have used "bring to front" and its' mates-no help. Using Publisher 2002 to create a 8 page booklet on 11x17 stock. I may add: this document had been working but something happened to it, most likely my fault, but I do not know what. Thanks, Jim This was a bug in 2002. Drag the objects to the top or bottom in ...

Trouble with a domain with a
I'm trying to send email to the domain PCM-LWV.COM and I keep getting this NDR: The message could not be delivered because the recipient's destination email system is unknown or invalid. Please check the address and try again, or contact your system administrator to verify connectivity to the email system of the recipient. < ylpvm25.prodigy.net #5.1.2> Everything seems fine with our exchange 2003 server otherwise. We are receiving email from this domain, and when we reply we get this NDR. We can send email to any other domain.. is it the - in the domain name?? I tes...

Are there MFC subclasses that give a different "look and feel" to the UI?
I was wondering if there are free implementations of MFC subclasses of windows and controls that give you a different look and feel? I like the Windows 2000 look, but it's quite a bit dated now, and the XP look is hideous for my taste, so I'm looking for something new, more modern looking, sleek. Thanks in advance for any suggestions. Franco See if this has something your looking for. http://www.prof-uis.com/ "Franco" <cuminato@yahoo.com> wrote in message news:1149800640.987272.304540@c74g2000cwc.googlegroups.com... >I was wondering if there are free implementa...

CDHTLMDialog troubles
Hi I'm justing to use the CDHTMLDialog with the following code IHTMLElement* pLinkElement = NULL; if (GetElement(_T("ctl00_ContentPlaceHolder1_tb1"), &pLinkElement) == S_OK && pLinkElement != NULL) { BSTR current; pLinkElement->get_innerText(&current); pLinkElement->get_id(&current); CString tt=_T("Hello from Callidus DHTML"); BSTR bstr=tt.AllocSysString(); pLinkElement->put_innerText(bstr); pLinkElement->Release(); // Thanks Heath } Sorry I posted too soon :) ----------------------------------------- I'm just ...

Having trouble connecting to Hotmail?
Add your comments here! Maybe someone at MS will see them and actually DO something. ____________________ Outlook 2007 on Vista Ultimate 32 bit........ I have a paid hotmail account that I have been happily using for 2 years now. I always check it via outlook (2003 and 2007). In 2007, for about the past 45 days, I would get intermittent dialog boxes asking me to confirm my password. I would, and I would select remember this password and things would continue - until about 3 days ago. Then the dialog boxes would not go away in Outlook 2007. It constantly asked me for my password and wo...

What is the difference between CEdit and CRichEdit control?
hi, all What is the difference between CEdit and CRichEdit control? Thanks! >What is the difference between CEdit and CRichEdit control? The edit control is plain text (single font, no font effects) while the rich edit control handles RTF (multiple fonts, font effects, etc). Dave ...

Trouble with manifest...
Hello, I want create a MFC application that uses XP styles. When I use the application wizard in VS 2003 and tick the manifest option, it creates an application with a manifest file. The manifest file looks like the ones you can find in exampels and is named "ApplicationName.manifest", located in the "res" directory. "InitCommonControls();" is called in "InitInstance()". So everything looks alright to me, but I build the application and run it on XP it does not have the files. What I am duing wrong here? Regards, Thomas >I want create a MFC a...

Sudden trouble with Entourage and Word for Mac
Hope somebody can help -- here part of my Chat session with Apple Support to explain the situation. Last night, I downloaded the software update that came through on the automatic alert -- I think it was the latest OS X upgrades. At that time I did not restart the computer as I normally do, because I was working on other things. I simply chose the Shut Down option when I was done. This morning I started up again and since I did, I cannot get any of my MS Office for Mac apps to work properly. Since the only thing that changed was that the update was downloaded and must have been installed when...

Trouble sending word document to PC
Version: 2008 Operating System: Mac OS X 10.4 (Tiger) Processor: Intel I am trying to send word documents to a PC and it opens as a zip drive for the PC and is unreadable. I am using Microsoft word for mac and saving the file as a: Word Document (.docx). What can I do to make the file compatible? The problem is the system and/or the user at the other end. It could be either of several things but without knowing what their software version is or how it's being delivered it's hard to say exactly what they're doing wrong. The OXML (.docx) file is basically a zipped form...

Pasting a chart from Excel 2007 to Word 2007 trouble
This seems like it should be simple!! Why is half of my pie chart disappearing when I try to paste it from excel to word? I have no idea of what is going on. Any ideas? Thanks! Vegas - For me, the best way to paste a chart from Excel 2007 into Word is to select the chart, choose Home | Paste | As Picture | Copy as Picture | etc., select an insertion point in Word, and Edit | Paste. Try the various Copy as Picture options to see which works best for you. - Mike http://www.MikeMiddleton.com "Vegas" <Vegas@discussions.microsoft.com> wrote in message news:C13CFD60-DAA...

trouble accessing written document
trouble accessing written document saved in file that can not access Is this a cryptic crossword clue? Pleaae explain exactly what you are trying to do in which Word version and what happens when you try. -- <>>< ><<> ><<> <>>< ><<> <>>< <>><<> Graham Mayor - Word MVP My web site www.gmayor.com Word MVP web site http://word.mvps.org <>>< ><<> ><<> <>>< ><<> <>>< <>><<> "terrell" <ter...

trouble downloading quotes
Is anyone else having trouble downloading quotes today (1/5/04)? In microsoft.public.money, quest wrote: >Is anyone else having trouble downloading quotes today (1/5/04)? > Yes. Try at a less busy time. Thank-you for your reponse. I did try between 1am - 3am and still saw the same problems. I wonder if it is something specific to me? Thanks again!! "Cal Learner-- MVP" <via_newsgroup@please.tnx> wrote in message news:b00lvv09d9png114immcs6vqq0sb4h3fcd@4ax.com... > In microsoft.public.money, quest wrote: > > >Is anyone else having trouble downloading...

IMF Trouble
I'm running a SBS 2003 sp1 W/ Exchange 2003 sp2 / IMF / update IMF has not created the ucearchive folder, and is not filtering UCE. I keep finding the following entry in my event log. Event Type: Error Event Source: MSExchangeTransport Event Category: SMTP Protocol Event ID: 7514 Date: 1/24/2006 Time: 6:14:27 PM User: N/A Computer: BLUESKY Description: An error occurred while loading Microsoft Exchange Intelligent Message Filter. The error code is 0x80070570. For more information, click http://www.microsoft.com/contentredirect.asp. 99% you installed IMFv1 on top of SP2 http://w...

Syncing troubles
I am not able to sync purchased i tunes songs to device via Media player 11. I am getting an error message every time. Ripped CD's are syncing perfectly OK. On Tue, 13 Apr 2010 22:03:01 -0700, Learner <Learner@discussions.microsoft.com> wrote: >I am not able to sync purchased i tunes songs to device via Media player 11. >I am getting an error message every time. >Ripped CD's are syncing perfectly OK. I'd be surprised if media player can play them at all ! Apple iTunes stores are protected by FairPlay DRM, which has only one way of playback and Sync...