0

My program has several Label controls that are updated to have different text every so often. I have a few icons that I want to reference within the text. I figured that instead of just displaying "(E)" in the Label, there should be a way to replace that with the corresponding image that I have that looks like: e. I figure that I need to override the Label.Paint event, but I'm not too sure how to do that properly. Every occurrence of "(E)" needs to be replaced with the image inline.

Example

Look for the (E) icon on the top.Look for the e icon on the top.

Community
  • 1
  • 1
crait
  • 156
  • 1
  • 4
  • 15
  • What are you targetting: Winforms? WPF? ASP? ...?? __Always__ tag your question accordingly! – TaW Jan 20 '16 at 21:27
  • I see two ways to do this: Either by owner-drawing the whole Label or by adorning it with a second label that shows the icon. See here for an [example](http://stackoverflow.com/questions/29756038/add-a-badge-to-a-c-sharp-winforms-control/29757940#29757940) - Some measuring will be needed in both cases, see Übercoder's answer! – TaW Jan 20 '16 at 21:28
  • @TaW - I'm sorry about that. Yes, it is Winforms! – crait Jan 20 '16 at 21:45

1 Answers1

0

This is not a trivial exercise.

  1. You'd have to provide a property on your derived label class to attach the image(s) to the text.
  2. Embed some sort of token(s) in the text to represent the image(s).
  3. In the OnPaint you have to parse the text for the token(s)
  4. Do a graphics.MeasureString() for each bit of text between the tokens. And then render it with graphics.DrawString() move to the right by the width of the text, render the image based on the token using one of the many graphics.DrawImage() overrides - move to the right by the width of the image and repeat.
Jens Meinecke
  • 2,904
  • 17
  • 20
  • I don't see any OnPaint event for Label controls. I only see the Paint event. Is that the one you mean? The steps that you have listed are pretty much the ones I expected, but I'm not too sure where to start with the OnPaint event, like I said about. – crait Jan 20 '16 at 21:45
  • @crait - you can either use the Paint event, or your can override the OnPaint member function. I usually do the latter. – Jens Meinecke Jan 20 '16 at 21:54
  • Like I said, I figured you'd have to do that, but I don't know how to do that. – crait Jan 21 '16 at 07:47