Solved

Right alignment in CString::Format

Posted on 1997-05-29
1
874 Views
Last Modified: 2008-03-03
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
Comment
Question by:edward95
1 Comment
 
LVL 1

Accepted Solution

by:
jmmougeolle earned 0 total points
ID: 1163750
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

820 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