0

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.stack.imgur.com/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.stack.imgur.com/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.

John Arc
  • 173
  • 1
  • 17
  • I dont know a way to do this but i might be aable to give you alternate solutions. So,what are you using this for? – Andam Dec 29 '18 at 10:26
  • 1
    If this is html output, forget this kind of formatting for good. A table or grid is much more suitable. And by the way, you wrote _vertically aligned_, but it looks *horizontal* to me. So, what is your requirement exactly? – ZorgoZ Dec 29 '18 at 10:40
  • @Andam I'm using this to display a list of products with their quantity and price in an invoice. – John Arc Dec 29 '18 at 14:47
  • @ZorgoZ My aim is to make the prices vertically aligned with each other. – John Arc Dec 29 '18 at 14:48
  • The answer by @InBetween might be what you are looking for. Try and mark it as answer if its what you want. – Andam Jan 05 '19 at 20:10

1 Answers1

0

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:

InBetween
  • 32,319
  • 3
  • 50
  • 90
  • Works only if the font is monospaced. There is no actual way in html to get the extent of a text without rendering it first. – ZorgoZ Dec 29 '18 at 10:43
  • @ZorgoZ oops, missed the tags... but if this is HTML why would he be rendering it this way? Just use a table type control... – InBetween Dec 29 '18 at 10:46
  • @InBetween Ahh, I'm using the HelloSign API and I want to fill up the custom field, and this is the format I need to put it in. It's not exactly HTML, more of back-end codes (string). I'll remove "html" from the tags. My apologies! – John Arc Dec 29 '18 at 14:50
  • @InBetween I tried out the codes with the guidelines provided by your pseudo code, but it still does not work. Is it possible to provide an example? – John Arc Dec 29 '18 at 15:44
  • @InBetween UPDATE: I used the string.PadRight property but the price still isn't completely aligned. Please look at my edited question on top, it consists of a picture of the current outcome and the back end codes. – John Arc Dec 29 '18 at 16:51
  • @BryanArtic What font are you using? If it isn't [monospaced](https://en.wikipedia.org/wiki/Monospaced_font) then, as ZorgoZ pointed out in comments, the text will not align correctly. – InBetween Dec 29 '18 at 17:12