Click here to Skip to main content
15,891,529 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I've Googled but it's not easy to find a solution.

I want to get the exact distance between the baseline of the text and the bottom border of a label in C#. I want this because I want to draw a line under the text (don't want to use underlined font, because it's so tight/close to the text).

Here is my try:

C#
//This is placed in the custom label class
int dy = (int)((ClientRectangle.Height - Font.GetHeight())/2);


But it's not exact, the dy returns about 3 and the line drawn to the label is too far from the baseline of the text.

Your help would be highly appreciated.

Thanks.
Posted

1 solution

I think the correct answer should be: you never can rely on this value. Instead, you should review your design the way independent on this parameter.

Do you see the point? This detail of the control layout is not documented and will hardly be ever documented. Why? Because Microsoft, as well as any other party, want to give it some design freedom. The library System.Windows.Forms relies on the Windows controls available in the present-day raw Windows API. The fine detail of layout of these control is a subject of change. What if the style of Window presentation changes once again? It may require the change in the location of the base line. You design would break like a playing card house.

What to do? You have too opposite approaches.

First, make your design tolerant to fine layout changes. Use only the basic assumptions on the layout. Make your design fluid.

The opposite approach is: if you care about exact position of the base line, you should have a reason for that. It means you want to match this coordinate with something else. What, exactly? This geometrically matching element should not be in another control, as it would not be reliable, as I explained above. Hence, you should create your own, custom control, put all coordinated graphical elements together and render it all in a custom way. This way, you will introduce a design element fully independent of Windows styles.

Good luck,
—SA
 
Share this answer
 
Comments
supernorb 18-Apr-13 21:35pm    
I don't think this design is fluid, it's for Windows Forms, a wrapper of Win32 and it won't never change, maybe WPF has a different design. In fact I've found at least 2 solutions for this. A one is exact and the other is nearly exact (the difference from the exact one is very small). The exact solution has some lines of code while the other has only 1 line, simply I've tried using Font.SizeInPoints instead of Font.GetHeight() in the code I posted in my original question. Maybe you have switched to WPF for a long time and already forgot Windows Forms, I'll never get rid of Windows Forms except its .exe can't run on the new Windows OSs in future. However I can't also stand jumping into the world of WPF. Thank you!
Sergey Alexandrovich Kryukov 18-Apr-13 22:42pm    
What do you mean "I don't think"? Make it fluid if you need it. Good form design is usually fluid, and bad is usually rigid. It does not matter WPF or Forms. I have no idea why you mentioned WPF or "jumping" into it at all, it wasn't my advice. Chances are, the "exact" and "nearly exact" solutions a fake by definition, because nothing can guarantee it will always work the same way.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900