Help with custom 'clickable label' control

C

chris-s

I've created a basic clickable label derived from
System.Windows.Forms.Control which works fine, BUT, I want to use it
in a situation where it will be 'scrolled' around the screen and this
is where it goes wrong, the 'OnPaint' event is not doing the
background so it ends up being 'smeared' rather than cleanly
displayed.

Below is the 'OnPaint' code, what do I need to do to ensure it is
'cleanly' re-painted?

protected override void OnPaint(PaintEventArgs pe)
{
Graphics gr = pe.Graphics;

base.BackColor = ViewerColors.ViewerBackColor;

if (Autosize)
{
SizeF size = gr.MeasureString(this.Text.ToUpper(),
this.Font);
this.Width = (int)size.Width;
}
else
this.Width = this.Parent.Width - (this.Left +
4);

Color textColor = ForeColor;

SolidBrush b = new SolidBrush(textColor);
gr.DrawString(Text, Font, b, 0, 0);
b.Dispose();
}

Chris
 
P

Paul G. Tobey [eMVP]

As ALWAYS, tell us what version of things you're targeting (.NET CF version,
device target, etc.)

Seems like OnPaintBackground should be getting called for the control. You
didn't override that, did you? You might also call base.OnPaint() from your
OnPaint override.

Paul T.
 
S

Simon Hart [MVP]

Why are you using Control? is this because you are targeting CF 1.0? if using
CF 2.0 or later, use UserControl.
 
A

Arun

Chris,

If you are using CF2.0 and above, use LinkLabel instead of your
control.
Just that you need to change the default font (Tahoma, 10pt,
style=Underline) where the style is set.
So that you won't see the underline and change the ForeColor to Black,
all this can be done in your designer itself.
Then you don't need to worry about this OnPaint.

Hope this helps,

Cheers,
Arun
 
A

Arun

Chris,

If you are using CF2.0 and above, use LinkLabel instead of your
control.
Just that you need to change the default font (Tahoma, 10pt,
style=Underline) where the style is set.
So that you won't see the underline and change the ForeColor to Black,
all this can be done in your designer itself.
Then you don't need to worry about this OnPaint.

Hope this helps,

Cheers,
Arun
 

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