views:

174

answers:

2

I have a custom control derived from a label. I need to change the native control's location origin from upper left to lower left.

Is there a method or property to do this?

A: 

The TextAlign property should do what you need.

label.TextAlign = System.Drawing.ContentAlignment.BottomLeft;

If you're trying to make the Location property reflect the bottom-right corner of the control rather than the top-left, then this isn't possible. You can, however, create your own property:

public Point BottomLeft
{
    get { return new Point(Left, Bottom); }
    set { Location = new Point(value.X, value.Y - Height); }
}

Bear in mind, though, that this won't remain true if the Height property changes (you'll have to set it again).

Adam Robinson
I don't think that changes the control location origin.
AC
A: 

If you want to change the behavior of the Label.Location property so that it refers to the bottom-left corner of the label, you could override InitLayout() in your custom label:

class myLabel : Label
{
    protected override void InitLayout()
    {
        base.InitLayout();
        Location = new Point(Location.X, Location.Y - Height);                
    }
}

This will shift the control up based on the height of the control. So if you start with (100,100) and the label height is 13, you will end with (100,87), which puts the bottom-left corner at (100,100).

But this will only happen when the label is added to a container. If you change the Location of the label after adding it to a container, it will go back to the top-right corner.

The other thing you might try is overriding LayoutEngine { get; } of the container which the label is in, so that you have full control over how the Location property of any custom label placed in the container is interpreted.

Charles
Not sure that will help, you are changing the location. I need the location of the control to point to the lower left. So if I add the control to the form at Location (100,100) I need location 100,100 to be the lower left corner of the control.
AC
You're right, I added when I should have subtracted. Fixed.
Charles
I see what you mean now. You don't want the actual value of **Location** to change. My suggestion will change **Location.Y**. If that's a deal-breaker, you might have to implement your own LayoutEngine, or mess around with the label's **OnPaint()**.
Charles
But I'm not sure that you can paint outside the bounds of the control.
Charles