ES_LEFT vs ES_RIGHT CEdit alignment

Posted on 2004-09-14
Medium Priority
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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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");

 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

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 600 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();
    e3.top += 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

Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

Question has a verified solution.

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

zlib is a free compression library (a DLL) on which the popular gzip utility is built.  In this article, we'll see how to use the zlib functions to compress and decompress data in memory; that is, without needing to use a temporary file.  We'll be c…
With most software applications trying to cater to multiple user needs nowadays, the focus is to make them as configurable as possible. For e.g., when creating Silverlight applications which will connect to WCF services, the service end point usuall…
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. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
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…

762 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