[Webinar] Streamline your web hosting managementRegister Today

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

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!
0
epastoor
Asked:
epastoor
  • 5
  • 5
  • 3
  • +1
1 Solution
 
basantCommented:
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.
0
 
Ernest022699Commented:
Could part of the problem be that you are not using a monospacing font?
0
 
pagladasuCommented:
To convert the integer ID to a string try this:
CString id;
id.Format("%3d",ID);
id will now contain the value of ID as a string.
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
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..
0
 
basantCommented:
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
e.g.
CString str;
str.Format( sz,"%6d %20s",a,s);
0
 
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;
m_wndList.AddString(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?
0
 
basantCommented:
>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.
0
 
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.


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

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


Look the help for format specifier in printf
0
 
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!
0
 
Ernest022699Commented:
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....
0
 
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;
m_wndList.AddString(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.
0
 
basantCommented:
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);
m_wndList.AddString(sz);
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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.

  • 5
  • 5
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now