Drawing labels and finding widths

08-04-2006, 06:59 AM
I am attempting to draw "labels" on a form using the DrawString method, instead of using the label control. Is this a good idea, assuming that the labels serve no purpose, other then telling the user what the controls(textboxes, combos etc..) are?

And is it possible to find the width of the painted label, so I can align it properly with the rest of the painted labels and controls, regardless of the font or font size that I choose?

08-04-2006, 07:38 AM
This approach is certainly possible, as for whether its a good idea, that is debatable. Alot would depend on how complicated things will get in terms of word wrapping etc. You can use the Graphics objects .MeasureString() method to find the length of a string.

08-04-2006, 08:12 AM

Thanks for the help! I knew there had to be a way to find the width for painted controls. I just need to weigh the pros and cons of designing a form this way, opposed to using the standard label controls.

08-04-2006, 08:56 AM
Using standard labels is far easier as you can use the designer to place them. Unless you have an extremely large amount of controls on the page page you are unlike to need the slight performance gains you could probably get over using the label control. It is worth noting however that if you are looking for performance gains DrawString() is really too slow to consider anyway and you would have to use APIs such as DrawTextEx (or something like that, its off the top of my head). In my placement year at University I developed an application for the Pocket PC that was entirely GDI+ based as this offered better flexibility and performance than using controls did.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum