0

I have followed this Wordpress tutorial which works great. I have used a listview and when i try to format the string it doesn't recognise the \t (but does recognise \n). It also won't recognise String.Format etc.

Is there anyway that I can format the string using tabs or something similar?

Cheers

EDIT

for( i = 0; i < lstView.Items.Count;i++) 
{ 
    name = lstView.Items[i].Text; 
    state = lstView.Items[i].SubItems[1].Text; 
    country = lstView.Items[i].SubItems[2].Text; 
    line += name + "\t" + state + "\t" + country + "\n"; 
}  
StringReader reader = new StringReader(line); 

When line is used to print the string is joined together so the \t doesn't work. The \n for a new line does work though. Does anyone know any way that I can format the string without using spaces.

The result is like this

NameStateCountry

LongernameStateCountry

anotherNameAnotherStateAnotherCountry

Where I would like them lined up (like in a table) with name one column, state another column and country then third

Any suggestions greatly appreciated

5
  • 4
    Could you provide a small code sample? Or clarify the question a bit? Commented May 11, 2011 at 6:47
  • How do you know it's not recognizing the tabs? In some contexts, tabs are not printed. In other words, the string "a\tb" appears as "ab" rather than "a b" Commented May 11, 2011 at 6:57
  • DO you want to remove the \t and \n from the text that comes from Listveiw.. Please elaborate on what u want, especially what do u mean by formatting the string and where u want it to be formatted and how. Commented May 11, 2011 at 7:01
  • how do I get it formatted like code in this comment box? Commented May 11, 2011 at 8:34
  • Put it in an answer instead. The help link to the right of the text edit box (when editing a comment) describes mini-markup used in comments. Commented May 11, 2011 at 10:57

1 Answer 1

1

Well, it is a bit odd that tabs are lost, but on the other hand, tabs will probably be problematic if the individual string elements (name, state etc.) varies in length.
What you could do instead is use string.Format() and use fixed column widths.
To get nice visual output this would include a parse step to determine the correct column width.
When this is done, use something like this to use spaces instead of tabs.

string line = string.Format("{0,-20}{1,-20}{2,-20}", "name", "state", "country");

EDIT: Saw that you did not want to use spaces.
In this case, you will probably need to handle this in the printing algorithm itself. You could still separate items with tabs, then for each line split it on tabs, creating an array of items (columns) per line.
For each item, print it using Graphics.DrawString() with a suitable X-position offset.

See the documentation for Graphics.DrawString.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. I had tried this originally but I obviously did something wrong, but works great now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.