2

I have a problem with the TextAlign property of my labels. I created a new class, based on the label class, to override the OnPaint event. I had to override this because I didn't want the label to change the ForeColer if disabled.

I don't really want to disable the labels because it doesn't make sense but the labels are in a other control which has to be disabled and therefore the labels are also disabled.

I used this OnPaint methode which allows me to set any forecoler I want even if it is disabled:

protected override void OnPaint(PaintEventArgs e)
    {
        TextRenderer.DrawText(e.Graphics, this.Text.ToString(), this.Font, ClientRectangle, ForeColor);
    }

But now the TextAlign property doesn't work because of the new OnPaint methode.

Do you have any suggestions to achieve both requirements? The change of ForeColor while the label is disabled and the option to align the text through TextAlign property.

Humayun Shabbir
  • 2,961
  • 4
  • 20
  • 33
  • It is the Label.OnPaint() method that implements text alignment. Since you overrode it, this is now your job. Use the DrawText() overload that takes the TextFormatFlags and Rectangle arguments. Use Left/HorizontalCenter/Right and Top/VerticalCenter/Bottom as appropriate. – Hans Passant Jul 29 '14 at 10:21
  • Thanks for your comment. I found a source code of the label [link](http://reflector.webtropy.com/default.aspx/Dotnetfx_Vista_SP2/Dotnetfx_Vista_SP2/8@0@50727@4016/DEVDIV/depot/DevDiv/releases/whidbey/NetFxQFE/ndp/fx/src/WinForms/Managed/System/WinForms/Label@cs/1/Label@cs) and I think I shouldn't override the OnPaint because it does a lot of things. Is there any other option of enabling the fontcolor when the control is disabled? – user3883550 Jul 29 '14 at 10:36
  • It makes very little sense to disable a Label control, it is not a control that can get the focus. It makes even less sense to intentionally confuse the user and hide the fact that a mouse click is not going to do anything. Doing things that don't make much sense is supposed to be difficult. – Hans Passant Jul 29 '14 at 10:42
  • In this case I don't have an option to enable/disable the label. I will try to explain: I built an own user control. This user control has a custom number of images which act like buttons. The control is therefore a "multi button" control. Everything works fine because I disabled the single pictureboxes (otherwise I couldn't click the control) but the labels are inside these pictureboxes and therefore they are also disabled. An option would be putting the labels out of the pictureboxes but that would be alot of work because I already implemented many things. – user3883550 Jul 29 '14 at 10:53
  • Just draw the text in the Paint event handler instead of using a Label. Also little point in disabling the picture boxes, just give them a common Click event handler and call this.OnClick() to fire the user control's Click event. Although I'd imagine you'd want to declare your own event since somebody ought to be interested which specific "button" was clicked. Quite an XY question btw. – Hans Passant Jul 29 '14 at 11:00
  • I tried putting text directly in the Paint event of the pictureBox but my programm is basically a UserInterfaceDesigner. Therefore the user must have the option to change the font, size, align, and so on in the property grid. The pictureboxes are disabled because I need the events on the main control (which contains the pictureboxes) because there are many other controls connected to these events (for example gauges, grids, simple buttons) and the events are designed to move and resize the controls. I don't want to resize and move single pictureboxes. I know.. my problem is hard to understand – user3883550 Jul 29 '14 at 11:06

2 Answers2

0

You are missing call the OnPaint method of the base class:

protected override void OnPaint(PaintEventArgs e)
{
    base.OnPaint(e);
    TextRenderer.DrawText(e.Graphics, this.Text.ToString(), this.Font, ClientRectangle, ForeColor);
}

Take a look: http://msdn.microsoft.com/de-de/library/cksxshce%28v=vs.110%29.aspx

To override an inherited event

  1. Override the protected OnEventName method.

  2. Call the OnEventName method of the base class from the overridden OnEventName method, so that registered delegates receive the event.

Leo Chapiro
  • 13,678
  • 8
  • 61
  • 92
  • Thanks for your comment, but thats not what I was looking for. When I call the base class I get the text twice. One of them is greyed out but in the right position with working align, and the other one (which is painted through the TextRenderer.Draw....) in black (or any other color) but align doesn't work. – user3883550 Jul 29 '14 at 10:16
0

I came across this question today, and here is my share on the matter.

Basically, since you are calling

extRenderer.DrawText(e.Graphics, this.Text.ToString(), 
    this.Font, ClientRectangle, ForeColor);

it will render the string from the left edge (notice your rectangle that you are sending, it starts at 0,0.

What I did (in my case I had TextAllign set to ContentAlignment.MiddleRight and was setting TextFormatFlags.PathEllipsis because I was rendering a path on my label - while alligning it to the right):

protected override void OnPaint(PaintEventArgs e)
{
    // Measure text
    var textSize = TextRenderer.MeasureText(Text, Font);
    if (e.ClipRectangle.Width > textSize.Width)
    {
        var x = e.ClipRectangle.Width - textSize.Width;
        var rect = new Rectangle(x, 0, textSize.Width, textSize.Height);
        TextRenderer.DrawText(e.Graphics, Text, Font, rect, ForeColor, 
            TextFormatFlags.PathEllipsis);
        return;
    }

    TextRenderer.DrawText(e.Graphics, Text, Font, e.ClipRectangle, ForeColor, 
        TextFormatFlags.PathEllipsis);
}
Joel
  • 7,401
  • 4
  • 52
  • 58