Search code examples
c#winformslabel

Label with an Image on the left - preventing the text to come over the picture?


I want to report status of an operation in a WinForm application written in C#. To make it more user-friendly, I want to show an icon on the left depending on the status.

  • Animated GIF during the process
  • Ok or Error icon depending on the result.

I wanted to use the native WinForms Label control which works well with animated GIFs and looks as standard as it can get.

My problem however is that text comes is written over the picture. There does not seem to be any property to set a margin for the text. I tried the most obvious thing, which is to prefix it with spaces, which works, except when the text wraps to the next line, as shown below.

enter image description here

I would prefer not spend too much time writing/testing/debugging derived control for this if possible... I could put a quick and dirty user-control, with a picturebox on the left of a label, but it doesn't feel very clean.

Is there any trick to get around this quickly and elegantly, or can someone point me to a Label derived class supporting this that is relatively lightweight? (I had a look at CodeProject but couldn't find much).

Thank you.


Solution

  • The quick-and-dirty usercontrol with an image and a separate label is your best option. Just add a public string property to set the label's text and you're pretty much done.