Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 914
  • Last Modified:

Right alignment in CString::Format

I want to use CString::Format to pack the items and then print it out. The items are not fix in length.
For example:

CString str, item1, item2;
int i;

for (i= 0 ; i<50 ; i++)
{
...
...
str.Format("%6s %15s", item1, item2);
pDC->TextOut(point.x, point.y, str);
...
}

item1 and item2 will change and have different length all the time.
How can I make the items to right alignment in CString::Format?
0
edward95
Asked:
edward95
1 Solution
 
jmmougeolleCommented:
This one is really easy, what you got to do is to use 2 Padded string as following:

#define TOTAL_LENGTH_ITEM1 6
#define TOTAL_LENGTH_ITEM2 15

CString strAddedWhiteSpaceForPaddedItem1(' ',
                     TOTAL_LENGHT_ITEM1-strItem1.GetLength());
CString strAddedWhiteSpaceForPaddedItem2(' ',
                     TOTAL_LENGHT_ITEM2-strItem2.GetLength());
str.Format("%s %s",
        strAddedWhiteSpaceForPaddedItem1 + strItem1,
        strAddedWhiteSpaceForPaddedItem2 + strItem2);


and that's all folks!


May the MFC be with you...
jmmougeolle@calva.net strikes back!






0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now