tags:

views:

42

answers:

4

I'd like to be able to have a Label such that it appears something like

[Some bit of text here] [ICON]

i.e. the icon follows the text, fairly straightforward.

I don't know what the text will be at design time, so I have AutoSize set to true on the Label control, but this means the image just gets drawn on top of the text. If I add Padding to the right hand side, it doesn't behave as I want (a la CSS, where background images are drawn inside the padding region). Is it possible to do this in C# Winforms? Or am I going to have to measure the text and then change the control width myself?

Thanks.

EDIT: Just to be clear, I wasn't proposing two controls, one after the other. Rather, setting the Label.Image property and have it appear to one side of the label's text. Apparently this is just not built-in functionality for autosized labels which seems pretty weak.

+1  A: 

Could you use a FlowLayoutPanel to achieve this? Put the autosized label before the icon in the ordering, and it should expand as necessary.

nasufara
I don't really want to add two controls - I want to use the Label's Image property. A FlowLayoutPanel seems like overkill for this.
QmunkE
@QmunkE What about the `Label.ImageAlign` property?
nasufara
It doesn't take the text into account. If I set the text align to MiddleLeft and the ImageAlign to be MiddleRight, the image is drawn on top/behind the last characters of the text. If I add Padding to the right hand side, the padding appears to the right of the image as I specified in the question.
QmunkE
A: 

I think you have to set the ImageAlign to right and TextAlign to left, then calculate the width manually,

int gap=10;
myLabel.AutoSize=true;
int autoWidth=myLable.Width;
myLabel.AutoSize=false;
myLabel.Width=autoWidth+gap+myLabel.Image.Width;
Bolu
A: 

If your label is placed inside an appropriate container, you could just use docking property. Set both label and icon docked left and make the label autosized.

P.S. Anyway you need to use an additional control. FlowLayoutPanel seems better solution IMHO. And you could draw your own custom icon-label-control, but it seems more overkill than FlowLayoutPanel.

EDIT: Or just position you controls manually. Icon.Left = Label.left + Label.Width + padding.

Dmitry Karpezo
+1  A: 

You can do this by deriving your own control from Label and overriding the GetPreferredSize() method. Add a new class to your project and paste the code shown below. Compile. Drop the new control from the top of the toolbox onto your form.

using System;
using System.Drawing;
using System.Windows.Forms;

class MyLabel : Label {
    public MyLabel() {
        this.ImageAlign = ContentAlignment.MiddleLeft;
        this.TextAlign = ContentAlignment.MiddleRight;
    }
    public new Image Image {
        get { return base.Image; }
        set {
            base.Image = value;
            if (this.AutoSize) {  // Force size calculation
                this.AutoSize = false;
                this.AutoSize = true;
            }
        }
    }
    public override Size GetPreferredSize(Size proposedSize) {
        var size = base.GetPreferredSize(proposedSize);
        if (this.Image != null) size = new Size(size.Width + 3 + Image.Width, size.Height);
        return size;
    }
}
Hans Passant