I've created a C# windows form application that takes input through textboxes and writes them to a text file on a relevant button click. I have found keeping a clear layout in the resultant text file to be a challenge all the way through development, and where possible have tried to save the data from the textbox along with the label that sits next to the textbox on the console. I decided to try and tabulate the information and read up on the String.Format() method, which gives space within the string {0, -10}, and then lets you list the variable or textbox.Text that should take that place.
This works in theory, but I have found when I've tried it the method only seems to accept the first input and repeat that in all the available spaces...
consoleFile.WriteLine("--------------------------------------------------------");
consoleFile.WriteLine("Patient Name | Owner Name | Surgeon | Anaethetist | Date");
consoleFile.WriteLine(String.Format("{0, -20} | {0, -20} | {0, -20} | {0, -20} | {0, -20}",
txtPatName.Text, txtOwnerName.Text, txtSurgName.Text, txtAnthtstName.Text, txtDate.Text));
consoleFile.WriteLine("--------------------------------------------------------");
consoleFile.WriteLine("\n\n");
In this example, I would think that each slot of {0, -20} would take a value from the relevant textbox in the list on the next line of the code. However, each slot prints out the text from the first argument of 'txtPatName.Text', meaning that the same string is written five times. So, how do I get the method to recognize the other inputs?
If I have made an obvious mistake, please have patience as I am new to the String.Format() method as of today, any help is greatly appreciated!
Mark
You need to use
{1, -20}
and so on. It is index of parameter passed intoString.Format
: