string format

T

Tony

Hello!

Here I add two string. This first one FaultCode has a length between 1 and 3
and the second could be any length.
Now in the list box I want the Description text to start at the same
position. Here is an example what I mean
12 This is a text for 12
123 This is a text for 123
4 This is a text for 4

this.faultCodesListBox.Items.Add(row["FaultCode"].ToString() + " " +
row["Description"].ToString());
How could I format to accomplich this.

//Tony
 
M

Martin Honnen

Tony said:
Here I add two string. This first one FaultCode has a length between 1
and 3 and the second could be any length.
Now in the list box I want the Description text to start at the same
position. Here is an example what I mean
12 This is a text for 12
123 This is a text for 123
4 This is a text for 4

I don't see how the descriptions texts starts at the same position. It
seems to end at the same position.
 
J

Julia M

Hello!

Here I add two string. This first one FaultCode has a length between 1 and 3
and the second could be any length.
Now in the list box I want the Description text to start at the same
position. Here is an example what I mean
12    This is a text for 12
123  This is a text for 123
4      This is a text for 4

 this.faultCodesListBox.Items.Add(row["FaultCode"].ToString() + "  " +
row["Description"].ToString());

Try
this.faultCodesListBox.Items.Add( string.format("{0,3} {1}",row
["FaultCode"].ToString(),row["Description"].ToString()) )
 
P

Peter K

"Julia M" <[email protected]> skrev i en meddelelse
Here I add two string. This first one FaultCode has a length between 1
and 3
and the second could be any length.
Now in the list box I want the Description text to start at the same
position. Here is an example what I mean
12 This is a text for 12
123 This is a text for 123
4 This is a text for 4

this.faultCodesListBox.Items.Add(row["FaultCode"].ToString() + " " +
row["Description"].ToString());

Try
this.faultCodesListBox.Items.Add( string.format("{0,3} {1}",row
["FaultCode"].ToString(),row["Description"].ToString()) )

Don't you have to take the font width into consideration?
 
F

Family Tree Mike

Julia M said:
Hello!

Here I add two string. This first one FaultCode has a length between 1 and 3
and the second could be any length.
Now in the list box I want the Description text to start at the same
position. Here is an example what I mean
12 This is a text for 12
123 This is a text for 123
4 This is a text for 4

this.faultCodesListBox.Items.Add(row["FaultCode"].ToString() + " " +
row["Description"].ToString());

Try
this.faultCodesListBox.Items.Add( string.format("{0,3} {1}",row
["FaultCode"].ToString(),row["Description"].ToString()) )

It appears Tony wants the number left-justified. It should actually be
{0,-3} in that case.

Mike
 
J

Jeff Johnson

I don't see how the descriptions texts starts at the same position. It
seems to end at the same position.

The description is the text beginning with the word "This." The intention is
to have it begin at the same x coordinate every time. Unless the OP is using
a fixed-width font, this will be impossible with simple string formatting.
 
C

CY

I don't see how the descriptions texts starts at the same position. It
The description is the text beginning with the word "This." The intention is
to have it begin at the same x coordinate every time. Unless the OP is using
a fixed-width font, this will be impossible with simple string formatting.

Hmm, havent tried this but char 9 (tab) comes to mind...
//CY
 
J

Jeff Johnson

Hmm, havent tried this but char 9 (tab) comes to mind...
//CY

Put it right out of your mind. What happens when the number in front is
larger than a tab stop (which is a nebulous concept in the first place)?
Then that line's description gets shunted to the NEXT tab stop. Bottom line:
text alignment is complicated.
 
C

CY

Hmm, havent tried this but char 9 (tab) comes to mind...
Put it right out of your mind. What happens when the number in front is
larger than a tab stop (which is a nebulous concept in the first place)?

there where 1,2 or 3 chars, or am I missing something?
Then that line's description gets shunted to the NEXT tab stop. Bottom line:
text alignment is complicated.

uhh, fonts changing, or tabstops?
well try adding a small char like 32 at the end and see if it will cut
it from " 0" to "999" if there should be a concern, I dont think
so....

if the font size change from 4 to 72, from terminal to wingdings in
the middle of filling up the listbox (and it impact differently on
each row),
then hopefully you got other worries with the UI to distract you from
my suggested solution ;)

//CY
 
C

CY

Put it right out of your mind. What happens when the number in front is
larger than a tab stop (which is a nebulous concept in the first place)?

there where 1,2 or 3 chars, or am I missing something? (nebol...
what=)

Then that line's description gets shunted to the NEXT tab stop. Bottom line:
text alignment is complicated.


uhh, fonts changing, or tabstops?
well try adding a small char like 32 at the end and see if it will
cut
it from " 0" to "999" if there should be a concern, I dont think
so....

if the font size change from 4 to 72, from terminal to wingdings in
the middle of filling up the listbox (and it impact differently on
each row),
then hopefully you got other worries with the UI to distract you from
my suggested solution ;)

Dont know if tab works in listbox though.

//CY
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top