I am trying to implement the ability for users to specify which string fields will appear in an invoice richtextbox and what order they will appear in the line of text. This string is an invoice line and there can be multiple lines so the formatting needs to also specify field widths for alignment.
I am trying to use String.Format so I can pass the filed position and format specifiers (as shown below) but am just totally stuck as how to pass the field name parameters since the order and number of fields will vary.
I have 10 fields so the user can select from 1 to 10 of the fields and then specify the order they want them to appear.
The end result String.Format call needs to look something like this.
// format specifer info: {field_number, width/alignment}
String.Format("{0,-10}, {1,-35}{2,-10}", sDate, sDesc, sPrice);
or this...
String.Format("{0,-10}, {1,-10}{2,-35}", sDate, sPrice, sDesc);
or this
String.Format("{0,-10}, {1,-10}{2,-35}{3,-15}{4,-10}", sDate, sPrice, sDesc, sSalesman, sAcct);
or any other combination of fields and field order.
I have the code to build the formattting specifier all done, but....
I am at a loss on how to build and pass the list of my string variable names.
I bet this is something so stupid, but I give up. I have been overthinking this.
I appreciate any help, assistance, etc....
Loading
Sam HobbsPosted Nov 10, 2009, 5:38 PM
Bill AzPosted Nov 9, 2009, 7:38 PM
Sam HobbsPosted Nov 9, 2009, 1:58 PM
You can format each of the 10 or so items separately, then put them together in whatever order that is needed. I might be able to help you better but you I do not see an answer to the question: will there always be the same fields, except just in varying orders; in other words, if there are 10 fields, will there always be the same 10 fields except in a different order?
Bill AzPosted Nov 9, 2009, 7:25 AM
Thanks for your posts.
Sam - I tried to tellyou everything. I guess I tried a single String.Format() call because that is all I could figure out. I am not sure how you are saying to use multiple calls. I am trying to avoid hard coding each possible parameter scenario (there are 100 combinations).
Niki - How would I call your example?
thanks
Sam HobbsPosted Nov 9, 2009, 12:07 AM
Nilanka DharmadasaPosted Nov 8, 2009, 11:31 PM
Why do you try to build a dynamic String.Fomrat, when you can keep it fixed and pass the parameters dynamiclly.
You write a function like this.
As you have said, 10 is the max number of parametrs. So you can create a string array and assign variables dynamically depending upon your logic.
If you have less number of parameters than 10, then you can pass empty strings.
private string GetText(string[] para)
{
if((para == null) || (para.Length < 10))
return "";
//Here para is an array with 10 items
string mystring = String.Format("{0}{1}{2}{3}{4}{5}{6}{7}{8}{9}", para[0],para[1],para[2],para[3],para[4],para[5],para[6],para[7],para[8],para[9]);
return mystring ;
}
string[] para = new string[10];
para[0] = ....;
para[1] = ....;
para[2] = ....;
.......
Then you can call
string finatext = GetText(para);
If my answer helps you accept my answer. Otherwise let me know.
Bill AzPosted Nov 8, 2009, 8:56 PM
All I am trying to figure out is how to dynamically build the String.Format function call.
Also....the number of parameters being passed to String.Format() will vary - anywhere from 1 to 10 and their order is also unknown until runtime.
The 1st parameter is the format string which is easy - it is just a string and I have the code for that working. The part I am wrestling with is how to dynamically list the remaining parameters which specify the strings to be passed to String.Format().
I tried creating a string and added the variable names in double quotes separated by commas to mimic the function call, but could not get this to work (see below). The issue for me is finding out a way to dynamic way to pass parameters that are unknown until runtime. I guess I have never run into something like this before. I can't imagine that it hasn't been done and I don't seem to be thinking straight today.
sInvoiceFieldList = "\"sDate\",\"sPrice\",\"sDesc\",\"sComment\""
My hope was that this string could be used in place of an actual list of arguments/parameters.
Here is more code.
sInvoiceFieldFormat = "{0,-15}{1,-10}{2,-25}{3,-25}\n"
string.Format(sInvoiceFieldFormat, sInvoiceFieldList);
Unhandled exception of type 'System.FormatException' occurred in mscorlib.dll
Additional information: Index (zero based) must be greater than or equal to zero and less than the size of the argument list.
Sam HobbsPosted Nov 8, 2009, 6:27 PM
Perhaps you can create an array of instances of an object representing each item, and then the order could consist of another array of indexes. The order in the array of each index of the item in the item list could determine the formatted order.
For example:
1 field1 formatfor1
2 field2 formatfor2
.
.
.
10 field10 formatfor10
Order array:
5
4
6
3
1
2
10
9
7
8