Hi,
I have a listbox which displays text files when the user clicks a button, the user then selects (highlights) a file in the list box and clicks another button to display the conents of the text file in a textbox.
How can I format the way in which the text is displayed in the textbox? For example, how do I insert a header or separate strings in the text file output?
If you know how to do this please help!
Loading
AlanPosted Jun 12, 2007, 10:02 AM
If Drew is saying that he's not sure what the file contains - only that it contains lines of numbers which are comma separated - then it's possible for code to react to this to some extent. For example, the following code will read the numbers, no matter how many there are on each line or whether this number is constant for a given file, add suitable headers and then right justify the numbers relative to the headers:
lines = new List();
string line = null;
List
StreamReader sr = new StreamReader("abc.txt");
while((line = sr.ReadLine()) != null)
{
lines.Add(line.TrimEnd());
}
sr.Close();
string text = "";
int max = 0;
for (int i = 0 ; i < lines.Count; i++)
{
string[] items = lines[i].Split(new char[]{','}, StringSplitOptions.RemoveEmptyEntries);
if (items.Length > max) max = items.Length;
line = "";
for (int j = 0; j < items.Length; j++)
{
line += String.Format("{0,4}", items[j]);
if (j < items.Length - 1) line += " ";
}
text += line;
if (i < lines.Count - 1) text += Environment.NewLine;
}
string header = "";
for (int i = 0; i < max ; i++)
{
header += "Col" + (i + 1).ToString();
if (i < max - 1) header += " ";
}
text = header + Environment.NewLine + Environment.NewLine + text;
textBox1.Text = text;
I tested this code on this simple abc.txt file:
1,2,3,4
5,6,7,8
21,2,35
100,72,4,134
Notice that the third line only has three numbers but the others have four.
Incidentally, for large files, you should really be looking at a ListView rather than a TextBox to display them.
Posted Jun 12, 2007, 9:53 AM
Im kind of confused. What do you mean you want to split it by number? Do you want to use a specific number as a string delimeter or a line number?
Maybe you could show us what the text looks like before formatting, and then show us what you want it to look like after its formatted :) You can always use .Split() on a string to split the string and load the split parts into an array but you have to specify a delimeter to tell .Split() where to divide the string. This is most commonly either a comma "," or a tab space.
Brandon
Drew TaylorPosted Jun 12, 2007, 8:25 AM
Thankyou that is very useful, I can now add headers which makes it easier to identify what the information is. However, for large text files containing numbers I'm still not sure how to split them, I'm not sure how it would fit into the code I have. From your example I could prob split the items if the text string contents was specified in the program but my program gets text files from a specified location. Any Suggestions?
AlanPosted Jun 12, 2007, 5:29 AM
To add a header, just concatenate the header text + a new line character + (possibly) a blank line with the rest of your text. For example:
string text = "whatever";
string header = "header";
text = header + Environment.NewLine + Environment.NewLine + text;
As an alternative to using Environment.NewLine, you can use the string "\r\n" on Windows platforms.
Otherwise you need to use the various members of the string class to manipulate the text. If, for example, the text in the file consists of strings separated by commas, you can split out the individual strings into an array using the Split() method:
string text = "Home,My Account,Job Board,Consulting";
string[] items = text.Split(new char[]{','}, StringSplitOptions.RemoveEmptyEntries);
You can now change the content, order or separator of the individual items before recombining them into the text you want to display:
text = items[0] + ":" + items[2].Substring(4) + ":" + items[3] + ":" + items[1];
Another thing you could do is to replace the colons in the above line with newline strings so that each item will display on a separate line:
text = text.Replace(":", Environment.NewLine);
textBox1.Text = text;
Of course, the TextBox's Multiline property needs to be set to true for all this to work.
Although this post has only scratched the surface, I hope it will have given you a flavour of what you can do to manipulate strings in C#. More powerful manipulations can be done using regular expressions but that's a huge topic on its own :)