locked
Why when I add a space to the Label string heterogeneous length of the string ? RRS feed

  • Question

  • Suppose I have 4 Label.Text corresponding to 4 series: Basic salary, Business salary, Responsibility allowance, Attraction allowance. When running the program I can change the string "Responsibility allowance" with another string "Attribution allowance" for example. I take the basic salary series as the standard for other series, from the basic salary series, find the 2-dot character position ":" is position 36 through the function Label1.Text.IndexOf (":"). The other strings that are inserted at the run are "Responsibility allowance" and "Attraction allowance" string, to display four strings uniformly with a 2-dot ":". I insert a space in the string so that the string length at the 2-dot character is equal to the 36th position but when displaying the characters at position 36 as the 2-dot is not equal, I see if the character is different if white characters are less often inserted in string will re-attach, why is that? you see my photo file. How do I write code to overcome this situation ?

    Thursday, October 15, 2020 3:25 AM

Answers

  • Unless your selected font is Monospace type (i.e.: all characters have equal width), widths for different characters are not equal (say "W" is much wider than "i")

    That's why practically all code editors uses monospace fonts by default.

    Usually I'll just settle with colons immediately following the label, but if you insist, you may want to use TableLayoutPanel to manage your layout, and only put your colons on the second (or in your case, third) column. 
    Thursday, October 15, 2020 4:11 AM
    Answerer

All replies

  • Unless your selected font is Monospace type (i.e.: all characters have equal width), widths for different characters are not equal (say "W" is much wider than "i")

    That's why practically all code editors uses monospace fonts by default.

    Usually I'll just settle with colons immediately following the label, but if you insist, you may want to use TableLayoutPanel to manage your layout, and only put your colons on the second (or in your case, third) column. 
    Thursday, October 15, 2020 4:11 AM
    Answerer
  • Thank you for answering my questions
    Friday, October 16, 2020 2:32 AM
  • Hi,

    Has your issue been resolved?

    If so, please click on the "Mark as answer" option of the reply that solved your question, so that it will help other members to find the solution quickly if they face a similar issue.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, October 16, 2020 6:59 AM
  • Just my 5 cents: You can use separate labels for title and another labels for the result

    this will allow you to align them as you like.

    Friday, October 16, 2020 4:51 PM