C# Label: How to Calculate Default space before Text Inside Label Control?

Unanswered C# Label: How to Calculate Default space before Text Inside Label Control?

  • Monday, August 20, 2012 6:00 AM
     
     

    Hi,

    I just want to calculate dynamically the gap between Label boundary and Text. as shown below image.

                   

    I  want to calculate how this Default empty space(shown in fig 1,2 with *question mark) changes with variation in font size,font family,font style. is there any fixed of formula which will tell me the space before text and after text ends. 

    for e.g.

               1)If the Font size is 18pt then empty space(*marked with question mark)before text = 9 px & empty space after text = 12px (which will vary in specific proportion with specific set of styles,font family)

               2)If the Font size is 24pt then empty space before text = 12 px & empty space(*marked with question mark) after text  = 14px (which will vary in specific proportion with specific set of styles,font family)


    Can any one help me regarding this default c# Label behavior?


    Thanking you in anticipation


All Replies

  • Monday, August 20, 2012 6:43 AM
     
     

    If you know for sure that the required width equals the space width, you could use Graphics.MeasureString method:

    http://msdn.microsoft.com/en-us/library/6xe5hazb.aspx

  • Monday, August 20, 2012 8:53 AM
     
     

    Hi Sergey,
    Thanks for your reply. I already tried Graphics.MeasureString and TextRenderer.MeasureText, but this is not what i m expecting. i just want to calculate the blank space in front and before text in label.

    This gap between label boundary and text is varying as per Label size, Alignment, Font Size, Font Family and Font Style. so i m not sure about the required space width. i mentioned one scenario other scenarios will be different as we change one of the above parameters.

    can you plz help regarding this ?

    Thanking you in anticpaition. 

  • Monday, August 20, 2012 9:08 AM
     
      Has Code

    Not sure what exactly you are trying to achieve, but you can try to get the required widths by subtracting the text's length from the label width, something like:

    (myLabel.Width - Graphics.MeasureString(myLabel.Text, myLabel.Font))/2
    Would this help?
  • Monday, August 20, 2012 10:42 AM
     
     

    Hi Sergey,

    Thanks for your reply,
    but while working on Graphics.MeasureString and TextRenderer.MeasureText i did the same as you explained, but in most of the scenarios it conflicts. as your see from attached image Left space and right space are not similar in both images.

    your functionality will be accurate for middle center alignment but not for other 8 alignments. also In word wrap condition it will conflict.

    Thanking you in anticipation.

  • Monday, August 20, 2012 9:08 PM
     
     

    Sorry, do not quite know the answer to that - I can see that my answer would only work for center alignment.

    May be try to come to your problem from a different perspective - what is it that you are trying to achieve by calculating the widths? - usually there is more than one way to skin a cat...

  • Tuesday, August 21, 2012 4:18 AM
     
     

    Hi Sergey,
    Thanks for your reply.

    I am trying to achieve standard label control behavior into web environment. as you see from following image. 

    both the views having all properties similar but still they have some difference. i want to achieve c# labels view in web using html controls. if i got the formula to calculate the gap between Label boundary and text considering all the parameters such as Label area, Font Size , Font Family, Font style , Alignment etc. using that formula i'll calculate and assign padding to html control so that both the views will appear similar.

    can you plz help me regarding this ?

    Thanking you in anticipation. 


    • Edited by aressDev Tuesday, August 21, 2012 4:22 AM
    •  
  • Tuesday, August 21, 2012 6:49 AM
     
     

    Hi aressDev,

    "I am trying to achieve standard label control behavior into web environment."

    You'll need to post it in the dedicated ASP.Net Forum http://forums.asp.net for more efficient responses.

    Thank you for your understanding.


    Bob Shen [MSFT]
    MSDN Community Support | Feedback to us

  • Tuesday, August 21, 2012 7:33 AM
     
     

    Hi Bob Shen,
    Thanks for your reply.

    I already posted the same on  http://forums.asp.net , but no clues there. here is the link for the same 

    C# Label Default Padding How to Calculate that ?

    can you plz help regarding the same ?

    Thanking you in anticipation.

  • Wednesday, August 22, 2012 2:33 AM
     
      Has Code

    Hi aressDev,

    This is really a CSS question - as most of the formatting in the web environment should be done with using styles. If, for example, you do not want your label text to "touch" the border, you can set something like the following:

    label, span {padding-left: 5px; padding-right: 5px;}
    
    As there are quite a few styles that can be applied, as well as "cascading" effect of the CSS, it is rather hard to give you a general advice that will cover all the possibilities - you should take it on a case by case basis.
  • Wednesday, August 22, 2012 4:32 AM
     
     
    Hi Sergey,
    Thanks for your reply.

    Yes, you are right. I m using css to assign padding. just finding the formula or possibilities of padding with respect Label area, Font Size, Font Family, Font Style and Alignment ... etc.

    Thanks