ES_LEFT vs ES_RIGHT CEdit alignment

Posted on 2004-09-14
Last Modified: 2013-12-03
I have an MFC application which creates edit fields.  In certain cases the text is aligned left and in others it is aligned right.  The problem is that when I use ES_RIGHT for right alignment, the text does not fit always fit in the edit field.  For example, if I have an edit field that is only 2 characters wide and it has 2 chars in it, the text displays fine if it is aligned left.  The same edit box aligned left will cut off the text so that only the first character shows.  I am using a monospaced font, so I know that it is not a font/character issue.  Making the edit box bigger is not an option for me.  Is there some padding or border or some property of the font and/or cedit that needs to be changed or set?  Thanks.
Question by:mromeo
  • 4
  • 3
LVL 16

Expert Comment

ID: 12063629
I'm not sure I follow: you have an edit field that is only 2 characters wide and it has 2 chars in it. Does it matter if it's right or left aligned (as long as there can be no more chars) ?
LVL 39

Expert Comment

ID: 12064967
With VC6 i couldn't reproduce the problem.

Both definitions



show only one character. When increasing width by one



both show two characters, either with or without having the focus.

So, either it looks like a font or caret problem.

Regards, Alex


Author Comment

ID: 12065082
Did you try it with a monospaced font?  Try doing:

myFont->CreateFont(14, 7, 0, 0, FW_NORMAL, 0, 0, 0, 0, 1, 2, 1, 34, "Lucida Console");

ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

LVL 39

Expert Comment

ID: 12066326
>> Did you try it with a monospaced font

Yes, but same result as when using a true type font.

What's the string you are using? Maybe it depends on the letters?

Did you set initial focus to that edit field?


Regards, Alex

Author Comment

ID: 12066380
I tried with and without ES_AUTOHSCROLL.  It didn't change anything.  I'm trying to put the word 'OR' in the field.
LVL 39

Expert Comment

ID: 12066800
It works with 'OR' in both cases (or not if i make a width of 12).

Could you post the dialog definition of resource file (open rc file as text file after closing all resource windows)?

Maybe there is another field overlapping? What's your caret (text cursor)? A small vertical 1 pixel bar?

Do you need an edit field? Or is it readonly? Then, try a static text field.

Regards, Alex


Author Comment

ID: 12066830
It's not a dialog box.  I create edit fields on the fly and place editable text in them.  If I'm putting 2 chars in the edit field, I make the edit box 16 pixels wide and use use a 7 pixel wide font.  The font creation is shown above
LVL 39

Accepted Solution

itsmeandnobodyelse earned 150 total points
ID: 12067422
Please post code of edit field creation.

>> I make the edit box 16 pixels

That looks strange as i took the same font as you  but have a width of 20 (13 dialog units) when showing both letters. Maybe the ratio of your rectangle isn't ok as edit fields are sized on base of dialog units rather than on pixels.

Look at that code where i place a dynamic edit field below my edit field of  the resource file:

    CFont* myFont = new CFont;
    myFont->CreateFont(14, 7, 0, 0, FW_NORMAL, 0, 0, 0, 0, 1, 2, 1, 34, "Lucida Console");
    CRect e1, e2, e3;
    DWORD dws = GetDlgItem(IDC_EDIT1)->GetStyle();  
    DWORD dwsex = GetDlgItem(IDC_EDIT1)->GetExStyle();  
    e3 = e1 + e2.TopLeft(); += 30;
    e3.bottom += 30;
    e3.right = e3.left + 16;       // Width is 16 pixels now (instead of 20)
    CEdit* pEdit = new CEdit;
    pEdit->Create(dws | dwsex, e3, this, IDC_EDIT1 + 1);

Regards, Alex

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows how to make a Windows 7 gadget that accepts files dropped from the Windows Explorer.  It also illustrates how to give your gadget a non-rectangular shape and how to add some nifty visual effects to text displayed in a your gadget.…
This article describes how to add a user-defined command button to the Windows 7 Explorer toolbar.  In the previous article (, we saw how to put the Delete button back there where it belongs.  "Delete" is …
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA.…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question