Solved

Forcing Read-only text field to the right

Posted on 2004-08-24
4
228 Views
Last Modified: 2013-11-20
I have an MFC text box that contains a file path. I'm running short on screen real estate, and the text box is smaller than the path. I want to have the text box display the end of the filepath instead of the beginning. Is there an easy way to do this?
0
Comment
Question by:tcf22
  • 2
4 Comments
 
LVL 13

Expert Comment

by:SteH
ID: 11883812
You need to change it to ownerdrawn. Then you can test the size of the full string and display only the right hand part.  
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 11883994
You can send a WM_SETSEL message to the control passing position of last character. Something like:

CString text = "c:\\your main folder\\your subfolder\\additional folder\\the file.txt";

yourControl.SetWindowText(text);
yourControl.SetSel(text.GetLenght(), text.GetLenght());

0
 
LVL 39

Accepted Solution

by:
itsmeandnobodyelse earned 125 total points
ID: 11885502
>> Is there an easy way to do this?

No, but you can do this:

void CTestDlgDlg::ShowFilePath(const CString& strPath)
{
     CWnd*     pEdit = GetDlgItem(IDC_EDIT);
     CFont*    pFont = pEdit->GetFont();
     CClientDC cdc(this);
     CFont*    pOldFont = cdc.SelectObject(pFont);
     CRect     rectEdit;
     pEdit->GetClientRect(&rectEdit);

     CString str = strPath;
     int        len;
     bool     first = true;
     while ((len = str.GetLength()) > 10)
     {
          CSize sz = cdc.GetTextExtent(str, len);
          if (sz.cx < rectEdit.Width())
               break;
          if (first)
          {
               str = str.Left(3) + "..." + str.Mid(6);  
               first = false;
          }
          else
          {
               str = str.Left(6) + str.Mid(7);
          }
     }
     cdc.SelectObject(pOldFont);

     pEdit->SetWindowText(str);  
}

Call it in OnInitDialog(..) after UpdateData(FALSE) or whenever you have to show a new file name like that

 ShowFilePath("c:\\Documents and Settings\\p602078\\more settings\\anything\\stuff\\mydata.dat",
                     IDC_EDIT);

Regards, Alex




0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 11885522
Correction:

void CTestDlgDlg::ShowFilePath(const CString& strPath, UINT idcEdit)
{
     CWnd*     pEdit = GetDlgItem(idcEdit);
     ...


Regards, Alex
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
conditional code and condition difference 9 65
java ^ examples 8 58
MaxSpan challenge 9 68
SQUD PROXY SERVER, UNIX, SLL/HTTPS 5 50
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

743 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now