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

Re: Help with custom 'clickable label' control by Paul

Paul
Fri Jun 20 10:13:45 CDT 2008

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.

<chris-s@mailcity.com> wrote in message
news:1a6064bf-bbfb-4b19-8266-a76f6721fd47@m36g2000hse.googlegroups.com...
> 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
>



RE: Help with custom 'clickable label' control by srhartone

srhartone
Fri Jun 20 14:00:00 CDT 2008

Why are you using Control? is this because you are targeting CF 1.0? if using
CF 2.0 or later, use UserControl.
--
Simon Hart
Visual Developer - Device Application Development MVP
http://simonrhart.blogspot.com


"chris-s@mailcity.com" wrote:

> 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
>
>

Re: Help with custom 'clickable label' control by Arun

Arun
Fri Jun 20 18:35:34 CDT 2008

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=3DUnderline) 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

On Jun 20, 12:00=A0pm, Simon Hart [MVP] <srhart...@yahoo.com> wrote:
> Why are you using Control? is this because you are targeting CF 1.0? if u=
sing
> CF 2.0 or later, use UserControl.
> --
> Simon Hart
> Visual Developer - Device Application Development MVPhttp://simonrhart.bl=
ogspot.com
>
>
>
> "chri...@mailcity.com" wrote:
> > 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?
>
> > =A0protected override void OnPaint(PaintEventArgs pe)
> > =A0 =A0 =A0 =A0 {
> > =A0 =A0 =A0 =A0 =A0 =A0 Graphics gr =3D pe.Graphics;
>
> > =A0 =A0 =A0 =A0 =A0 =A0 base.BackColor =3D ViewerColors.ViewerBackColor=
;
>
> > =A0 =A0 =A0 =A0 =A0 =A0 if (Autosize)
> > =A0 =A0 =A0 =A0 =A0 =A0 {
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 SizeF size =3D gr.MeasureString(this.Te=
xt.ToUpper(),
> > this.Font);
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 this.Width =3D (int)size.Width;
> > =A0 =A0 =A0 =A0 =A0 =A0 }
> > =A0 =A0 =A0 =A0 =A0 =A0 else
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 this.Width =3D this.Parent.Width - (thi=
s.Left +
> > 4);
>
> > =A0 =A0 =A0 =A0 =A0 =A0 Color textColor =3D ForeColor;
>
> > =A0 =A0 =A0 =A0 =A0 =A0 SolidBrush b =3D new SolidBrush(textColor);
> > =A0 =A0 =A0 =A0 =A0 =A0 gr.DrawString(Text, Font, b, 0, 0);
> > =A0 =A0 =A0 =A0 =A0 =A0 b.Dispose();
> > =A0 =A0 =A0 =A0 }
>
> > Chris- Hide quoted text -
>
> - Show quoted text -


Re: Help with custom 'clickable label' control by Arun

Arun
Fri Jun 20 18:35:39 CDT 2008

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=3DUnderline) 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

On Jun 20, 12:00=A0pm, Simon Hart [MVP] <srhart...@yahoo.com> wrote:
> Why are you using Control? is this because you are targeting CF 1.0? if u=
sing
> CF 2.0 or later, use UserControl.
> --
> Simon Hart
> Visual Developer - Device Application Development MVPhttp://simonrhart.bl=
ogspot.com
>
>
>
> "chri...@mailcity.com" wrote:
> > 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?
>
> > =A0protected override void OnPaint(PaintEventArgs pe)
> > =A0 =A0 =A0 =A0 {
> > =A0 =A0 =A0 =A0 =A0 =A0 Graphics gr =3D pe.Graphics;
>
> > =A0 =A0 =A0 =A0 =A0 =A0 base.BackColor =3D ViewerColors.ViewerBackColor=
;
>
> > =A0 =A0 =A0 =A0 =A0 =A0 if (Autosize)
> > =A0 =A0 =A0 =A0 =A0 =A0 {
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 SizeF size =3D gr.MeasureString(this.Te=
xt.ToUpper(),
> > this.Font);
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 this.Width =3D (int)size.Width;
> > =A0 =A0 =A0 =A0 =A0 =A0 }
> > =A0 =A0 =A0 =A0 =A0 =A0 else
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 this.Width =3D this.Parent.Width - (thi=
s.Left +
> > 4);
>
> > =A0 =A0 =A0 =A0 =A0 =A0 Color textColor =3D ForeColor;
>
> > =A0 =A0 =A0 =A0 =A0 =A0 SolidBrush b =3D new SolidBrush(textColor);
> > =A0 =A0 =A0 =A0 =A0 =A0 gr.DrawString(Text, Font, b, 0, 0);
> > =A0 =A0 =A0 =A0 =A0 =A0 b.Dispose();
> > =A0 =A0 =A0 =A0 }
>
> > Chris- Hide quoted text -
>
> - Show quoted text -