Solved

Right alignment in CString::Format

Posted on 1997-05-29
1
868 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Implementing a ResolveEventHandler in C++ 13 115
Would like to move button in a function 3 72
FMX enumerated colours 2 84
object oriented programming comparison 5 54
Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

910 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

16 Experts available now in Live!

Get 1:1 Help Now