Results 1 to 4 of 4

Thread: Why when I add a space to the Label string heterogeneous length of the string ?

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2008
    Posts
    517

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

    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 ?
    http://www.mediafire.com/view/0t4b8j...Space.jpg/file

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: Why when I add a space to the Label string heterogeneous length of the string ?

    Unless you're using a fixed-width font, e.g. Courier New, two Strings containing the same number of characters will almost certainly be different physical widths. If you want to stick with a variable-width font then just use two different Labels for the initial text and the colon. As long as all the Labels containing the colons have the same Left value, they'll all be aligned. You could even add the Labels to a TableLayoutPanel to allow auto-sizing. Here's a couple of screenshots demonstrating that:

    Name:  TLP1.png
Views: 134
Size:  6.9 KB

    Name:  TLP2.png
Views: 105
Size:  6.3 KB

  3. #3
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,988

    Re: Why when I add a space to the Label string heterogeneous length of the string ?

    Just to expand on that a bit, most fonts are designed to look good, rather than to have all characters take up equal space. The true width of the character at any font size is not guaranteed to be the same, and likely won't be. Just take a look at the word "likely". You have three letters that are very slender (two l's and an i), one letter that is very round (e), and two letters that are not round, but are pretty wide for their height (k and y). If each letter was the same width, the first l, i, and k would be spread out fairly wide, with a fair amount of whitespace between the letters, because both the l and the i would be surrounded by a good amount of white space. Of course, if you are viewing this with an equal-sized font, then it will look that way anyhow. For most fonts, the l and i will be taking up about the same amount of space, or even less, than the k.

    This just means that the length of the string in pixels is not going to be calculated solely from the length of the string in characters. It often also means that spaces are a really poor way to pad strings, because just how big is a space? One space is about the width of the k, but is two spaces twice that, or is the second space smaller than the first? Also, is the width of a space the same for all fonts? It doesn't have to be, but I would guess that it probably is.
    My usual boring signature: Nothing

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2008
    Posts
    517

    Re: Why when I add a space to the Label string heterogeneous length of the string ?

    I have found out the reason before, because the size of each character is not identical at n rows and m columns, this phenomenon occurs even though the length of the string is equal. Thank you for answering my questions

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width