• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 632
  • Last Modified:

CEdit align right with longer text

Hi, I have problem with align text in CEdit. When I set the long text for example filepath which is longer that CEdit box's length and I wanna make it can be seen the most right hand side of text. I set the property of control when design and try to change style by ModifyStyle too but I didn work.  Who know how can I do that in the approproated way?
0
wasawas
Asked:
wasawas
  • 4
  • 3
  • 2
1 Solution
 
itsmeandnobodyelseCommented:
You can call

   GetDlgItem(IDC_MY_EDIT)->SetSel(n, n-1);

where n is the position within the string that should be shown as first character.
0
 
ZoppoCommented:
There's one problem with that code - to get this work the 'CWnd*' returned by GetDlgItem() has to be casted to a 'CEdit*'.

To show the text right aligned you can do:

>       CEdit* pEdit = (CEdit*)GetDlgItem( IDC_MY_EDIT );
>      int nLen = pEdit->GetWindowTextLength();
>      pEdit->SetSel( nLen, nLen );

ZOPPO
0
 
itsmeandnobodyelseCommented:
>>>> to get this work the 'CWnd*' returned by GetDlgItem() has to be casted to a 'CEdit*'.

Thanks Zoppo ;-)

>>>> pEdit->SetSel( nLen, nLen );

That would position the caret (text cursor) to the right of the text thus scrolling most right horizontally.


0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
ZoppoCommented:
That's how I understood this:
>> I wanna make it can be seen the most right hand side of text.
0
 
itsmeandnobodyelseCommented:
>> I wanna make it can be seen the most right hand side of text.

Yes, I have same understandness ;-)

>>>> ((CEdit*)GetDlgItem(IDC_MY_EDIT))->SetSel(n, n-1);
>>>> where n is the position within the string that should be shown as first character.

It would be much more difficult to determine the n in my suggestion cause the CEdit most probably was using proportional font. To do it rightly you would need to call GetTextExtentPoint32 applying an appropriate device context for increasing substrings to the right until the size calculated is bigger than the size of the CEdit.

A quite different idea to solve the issue is to showing a tooltip withe the full edit string when hoovering with the mouse over the CEdit (requires some free space right of the edit field).
0
 
wasawasAuthor Commented:
Hi,All

Thanks for you all.

SetSel() is  the one that I've been tried before but it cannot work with some way with using OnEnchange so I was really wanna know is there any properties to set that or not by not using trick with SetSel(). As I say "the approproated way".

Do you have any other idea?

0
 
itsmeandnobodyelseCommented:
>>>> cannot work with some way with using OnEnchange so
If you mean DoDataExchange by that you simply could comment the path field in
DoDataExchange function and it wouldn't be changed back to left aligned.

>>>> by not using trick with SetSel(). As I say "the approproated way".

The trick is only needed cause you want to show only the right part of a long string what actually is not the standard behavior.

So it would be better to go back to standard behavior, what means that if you want to show only the relevant parts of the file path you better make a static field and show the path like C:\...\some folder\subfolder\filename.ext which fully fits into the field.
0
 
ZoppoCommented:
>> by not using trick with SetSel().

I agree with itsmeandnobodyelse. Without the SetSel() IMO there's no chance to do what you want with an edit-control. The problem simply is an edit control always scrolls the shown text in a way the cursor is visible. So, without moving the cursor there's no way.

You could use 'DrawText' to get a string like that one shown by itsmeandnobodyelse which fits into the controls rect using the flags DT_MODIFYSTRING and DT_PATH_ELLIPSIS
0
 
wasawasAuthor Commented:
Thanks a lot for the answer and  the good comment. I voted the 2nd comment above cause we really want the length to make it go right.

And for who are seeking for "How to set align right with long text for edit control" the answer is "No way" to do that but you can use SetSel( ) to tricking it easier. You should not try to use ModifyStyle() or other properties changing(As here people's answer and comment) cause you will waste time for that.

If others can find another way really good for this. Please put your comment please.

Thanks again.
0

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

  • 4
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now