AddString for Listbox

I am trying to add some strings to a list box that contains different types of data. All of the data is stored in a linked list and easily accessible.  In the list box i would like to list information such as a first name followed by their last name and then for instance and ID number following their name. the Id number is stored as an integer and the names are stored in the same node as a CString.  I've tried the command AddString(firstName +" "+ lastName + "tab"+ID).   However, I cant get them to lineup evenly throughout the listbox.  Is there a way to control listbox information like there is in a dos windown using the command setwidth and setprecision?  Also, how do i convert the integer "ID" to a CString to be displayed in the listbox?  Thanks!
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

If you want to do formatting inside a Listbox entry, you can use ListView Control that gives a lot of functionality.

Suggestion 2 :
You can manually format the string using sprintf (with Specified Field Width %6d eg) and then call AddString.
It looks to be very simple.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Could part of the problem be that you are not using a monospacing font?
To convert the integer ID to a string try this:
CString id;
id will now contain the value of ID as a string.
PMI ACP® Project Management

Prepare for the PMI Agile Certified Practitioner (PMI-ACP)® exam, which formally recognizes your knowledge of agile principles and your skill with agile techniques.

epastoorAuthor Commented:
Ive never used sprintf before, can you describe a little more how i would use it? For example if I have CString firstName and CString lastName.  and wanted the first name no matter how big or small the name was to take up at least 15 characters and 20 characters for the last name.  i assume this would be the precision length.  How do i go about formatting this?  Thanks..
char sz[80];

// say you want to print and
// integer with max 6 char size
// and a charcter array with max 20 size
int a = 2;
char* s = "abcd"
then use :
sprintf( sz,"%6d %20s",a,s);
CString str = sz;

Then call Addstring
listBox->AddString( str);

Suggestion 2 :
// Optionally you can use
// CString::Format
CString str;
str.Format( sz,"%6d %20s",a,s);
epastoorAuthor Commented:
OK...thanks this works somewhat but its still not doing completely what i need. Here is what i have
CString firstName; //already defined to equal something
CString lastName; //already defined to equal something

int iDnum;

char sz[200];

sprintf( sz,"%0s %35s %6d ",firstName,lastName,iDnum);
CString str = sz;

This does move lastName over 35 characters but it is 35 characters from where firstName ends.  Is there anyway to make it start exactly at the 35th spot, so multiple calls of this would line all the names up in a column?
>sprintf( sz,"%0s %35s %6d ",firstName,lastName,iDnum);

You didn't specify the lenght of firstname %0s ??

>This does move lastName over 35 characters but it is 35 characters from where firstName ends.  

I didn't get ur point.
epastoorAuthor Commented:
Hmm...lemme try to make it a little more clear.
id like the first name to left align itself since it is the first CString.  
so if i change the code to

sprintf( sz,"%50s %35s %6d ",firstName,lastName,iDnum);

it moves the firstname way over to the right.  it also then starts the last name at different places.  for example, the table looks somethign like this
      firstName        lastName
        firstName2       firstName2
       firstName3       firstName3

do you see what i mean? it is spacing things out, but nothing is lining up.  I was trying to get the last name to start at like the 50th character spot no matter what, so that all the last names would be in a nice neat line.
I hope this is a little more clear. Thanks for your help!

You didn't specify the lenght of firstname %0s ??

>This does move lastName over 35 characters but it is 35 characters from where firstName ends.  

I didn't get ur point.

I still think you need a monospacing (i.e., non-proportional width) font, such as Courier.  Anybody else?
Use - for left alignment.

sprintf( sz,"%-50s %-35s %-6d ",firstName,lastName,iDnum);

Look the help for format specifier in printf
epastoorAuthor Commented:
AHH that did it! i did however need to change it back to a monospacing font like ernest had previously listed.  Without the monospacing font, it was still jumbled, but the monospacing font combined with the minus sign did the trick! Thanks a bunch!
You're welcome!  Glad to be of help.  You might want to post some code here for those who pay points for "Previously Answered" questions....
epastoorAuthor Commented:
Sounds like a good idea!

//firstName, lastName and position are all CStrings previously created.
//salary is a double being converted here to a string, and idNumber is an integer.

char *sz=new char[500];

sprintf( sz,"%-10s %-20s %-10s %-6g %-6d ",firstName,lastName,position,salary,idNumber);

CString str = sz;

This formats the string to lineup nice and neatly as long as you have a monospacing font such as courier used for that dialog.
Better use the Format Method of CString that does the same as sprintf.

CString str = sz;
str.Format("%-10s %-20s %-10s %-6g %-6d ",firstName,lastName,position,salary,idNumber);
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.