Solved

Right alignment in CString::Format

Posted on 1997-05-29
1
863 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
Comment Utility
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 run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
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 clear a vector as well as how to detect empty vectors in C++.
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.

762 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

9 Experts available now in Live!

Get 1:1 Help Now