Reputation: 183
I'm trying to make the prices of the products in a textarea to be a straight line. However, as the length of the product names is different, it would require different amounts of "\t" to get the prices to be vertically aligned.
Here's a sample of what I'm trying to do:
string text = "1X " + "ABC Brand Drink \t\t\t\t " + price + "\n" + "2X" +
"BCD Drink that has a longer name \t\t" + price;
My question is, is there a way to make my prices vertically aligned without using all the "\t" escape sequences? I wouldn't know how many "\t" to use the text will be programmatically generated, so I wouldn't know how to gauge the #.
Here's a sample output that I'm working towards: https://i.sstatic.net/KvUQx.jpg
I am trying to use this in the HelloSign API template's to populate the custom text field.
UPDATE: So far I used the String.PadRight property as suggested in the comments, it kind of works but the price still isn't completely aligned. Here's a picture of what the current outcome is: https://i.sstatic.net/Twsvx.jpg. Here are the codes I used:
ArrayList al = new ArrayList();
string prodName = "Try out product A and product B2";
string price = "$620";
string quantity = "1X";
Product a = new Product(prodName, price, quantity);
al.Add(a);
string prodName2 = "Out product A and product Basdasddddddasd";
string price2 = "$650";
string quantity2 = "123X";
al.Add(new Product(prodName2, price2, quantity2));
string str = "";
string abs = "";
int nam, quan = 0;
int asdsd = 0;
foreach (Product p in al)
{
abs = "";
nam = p.ProdName.Length;
quan = p.ProdQuantity.Length;
abs += p.ProdQuantity + " " + p.ProdName;
//if ((nam + quan) % 2 == 0)
//{
// asdsd = (175 - (nam + quan));
//}
//else
//{
// asdsd = (175 - (nam + quan) - 1);
//}
asdsd = (175 - (nam + quan));
str += abs.PadRight(asdsd) + p.ProdPrice + "\n\n";
Please ignore the names of the variables, I will change them after everything is completed.
Upvotes: 0
Views: 340
Reputation: 32780
You dont need tabs, you just need to know the longest text in every column and then simply pad right or left depending on the alignment you want. Pseudo code follows:
foreach value in each column
find longest length
foreach value in each column
add to text: current value padded left longest length - actual length // right alingment
or
add to text: current value padded right longest length - actual length // left alingment
+ whatever addtional spacing you want in between columns.
And you are finished. Of cource if you know beforehand the maximum expected length of each column then you can skip the whole first step.
The methods you are looking for are:
Upvotes: 0