Label and TextAlign

M

mick

I have a window with height of 24. I have a label on the said window also
with a height of 24. I have set the Flatstyle to Flat so I can set the
background to transparent. Now if I use a smallish font (8 or so) it always
displays in the top left of the label irrespective of what the TextAlign is
set to. The only way the text responds to TextAlign is if I set the
Flatstyle to System (which is no good as I can no longer have a transparent
background).

Anyone have an idea what gives?

mick
 
M

Morten Wennevik [C# MVP]

Hi Mick,

I'm afraid I am unable to reproduce this. Using a WinForm dialog with
BorderStyle.None and Height 24 I had no problem adjusting the TextAlign
property on a label with FlatStyle.Flat and Background.Transparent. Using an
image as the form background the label was clearly transparent and properly
aligned.

If you still have the problem, can you give us some more details and
possibly code that will reproduce the issue?
 
M

mick

Morten Wennevik said:
Hi Mick,

I'm afraid I am unable to reproduce this. Using a WinForm dialog with
BorderStyle.None and Height 24 I had no problem adjusting the TextAlign
property on a label with FlatStyle.Flat and Background.Transparent. Using
an
image as the form background the label was clearly transparent and
properly
aligned.

If you still have the problem, can you give us some more details and
possibly code that will reproduce the issue?

Realised what the problem is. I had created a class derived from the Label
class and had overriden the OnPaint method as below

protected override void OnPaint(PaintEventArgs e)
{
Graphics g = e.Graphics;
g.SmoothingMode = SmoothingMode.AntiAlias;
g.DrawString(this.Text, this.Font, new
SolidBrush(this.ShadowColor),displacement);
g.DrawString(this.Text, this.Font, new
SolidBrush(this.ForeColor),new Point(0,0));
}

The new class has two new properties - ShadowColor and Displacement. All it
does is print the text twice to give a shadow effect. Problem is my new
class now ignores the TextAlign. I just assumed that if I didnt handle it it
would be handled by the parent class. It seems not.

I`m new to this so if anyone could give me a nudge as to what I should be
doing it would be appreciated. I imagine it`s something along the lines of
reading the TextAlign property and working out the x and y from that?

mick
 
P

Peter Duniho

[...]
The new class has two new properties - ShadowColor and Displacement. All
it does is print the text twice to give a shadow effect. Problem is my
new class now ignores the TextAlign. I just assumed that if I didnt
handle it it would be handled by the parent class. It seems not.

Nope. That's not how C#, or any of the mainstream OOP languages for that
matter, work. Your parent class isn't going to do _anything_ for you in
an override, unless you specifically call the parent class's
implementation.
I`m new to this so if anyone could give me a nudge as to what I should
be doing it would be appreciated. I imagine it`s something along the
lines of reading the TextAlign property and working out the x and y
from that?

You will probably have better results if you use the TextRenderer class,
with its DrawText() method. It's basically what the Forms controls use to
draw text, so it's a lot easier to get identical results to the built-in
controls if you use that.

You'll have to check each applicable property in the class that affects
drawing and translate that to the appropriate flags for the DrawText()
method.

Of course, since you _aren't_ calling the parent implementation, it begs
the question as to why you are bothering to subclass Label at all. You're
not _enhancing_ the Label class, you're simply replacing its functionality
with something else. You might as well just make your own Control-derived
class instead.

This has the added benefit that then you can include only properties that
will actually affect the drawing of the text. Subclassing Label, you're
going to have all these properties that Label respects, but which won't
have any effect in your sub-classed control until you explicitly handle
them.

Either you're going to go through each and every one and duplicate the
functionality in Label, or you're not. If you do, then you've basically
just re-written Label, and it might as well be its own class. If you
don't, then you've broken Label by implying to a user some property they
might set would work, even though it won't. In that class also you would
be better off with a completely independent custom class, so that there
are only properties in the class that actually affect the way the control
behaves.

Pete
 
P

Peter Duniho

[...]
I`m new to this so if anyone could give me a nudge as to what I should
be doing it would be appreciated. I imagine it`s something along the
lines of reading the TextAlign property and working out the x and y
from that?

You will probably have better results if you use the TextRenderer class,
with its DrawText() method. It's basically what the Forms controls use
to draw text, so it's a lot easier to get identical results to the
built-in controls if you use that.

You'll have to check each applicable property in the class that affects
drawing and translate that to the appropriate flags for the DrawText()
method. [...]

Sorry...I meant to, somewhere in all that, point out that no...you don't
need to "work out the x and y". Whether you are using
Graphics.DrawString() or TextRenderer.DrawText(), you can pass appropriate
flags to the method, along with a rectangle within which the text should
be drawn, and the method itself will handle issues like alignment for you.

Pete
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top