Wednesday, September 06, 2006

Label is not JLabel, or Why wont .NET make my job easier?

Once again, I'm struggling with C# .NET 2.0, and finding out that .NET isn't really doing my job easier. My job as a software developer should not involve re-inventing the wheel every time I develop software. MS has always being good on creating tools that are simple to use and build simple things. But every once in a while I see cracks in the facade, sure it is simple to use but it is so much harder to do something advanced.

By "advanced" I mean in this particular case that I want to have a control that displays an icon and a text next to each other. A simple label that has a text AND an image. In Java, it is quite simple; create a JLabel and set horizontal position with setHorizontalTextPosition(). This has existed forever in the Swing framework, and Sun thought of it from the begining. Simple and "advanced".

In .NET it is not quite so simple; my first try was to use a Label for the text and image. The class has both a Text and Image property, so I thought I could set the text and image and it was done.. But unfortunately the control does not have a property to set a alignment relation between the image and the text. So if I set the image and text, the image will be placed behind the text and it is not very pretty. If I set the alignment of the icon and text to Left and Right, the control will still have the image behind the text but now the text looks like crap as it is right aligned. So the result is, I can either set the text OR the image; but not both. C'MON this isn't really advanced, is it?

Last week I had the same problem with a button, but (credit to MS) they actually have implemented a TextImageRelation property that fixes this "problem". The interesting part is that it was introduced in .NET 2.0, so I wonder how people managed to create a button that has text AND an image before December 2005. And the other question is why didn't they think it through and thougt of other .NET Controls that could have this problem and fixed those as well? I got the answer for you, "Because they don't think"

What is the real solution to this problem? Adding a new Label with an Icon, develop my own control?