473,383 Members | 1,952 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

Problem with OnPaint in custom label control

Hi there

I have a custom label control (GradientLabel) which enherits from Label and
basically just paints the background of a label in a gradient.

In my code I have the following for the overidden OnResize event:

protected override void OnPaint(PaintEventArgs e)
{
Graphics g = e.Graphics;
using (LinearGradientBrush br = new LinearGradientBrush(e.ClipRectangle,
color1, color2, gradient_mode))
{
g.FillRectangle(br, e.ClipRectangle);
}
base.OnPaint(e);
}

(The Color1 and Color2 and gradient_mode are set using properties to get the
designtime representation to work)

Now the odd part is this. I have two different machines, a desktop and a
laptop, both running .NET 2.0. On my desktop there is no problem when I
resize the control and the gradient follows nicely, also when I maximize the
window etc.
On my laptop however, everything works except when I maximize the form in
which the label resides. The gradient simply cuts off and starts over where
the form was maximized from.
I have tried overriding a lot of different events, including the forms own
OnResize event and then through that calling the labels Refresh() method,
but no luck.

What is the problem? I suspect it for being a machine specific problem, but
it would be nice if I somehow could get a reason :)

Another problem is when I drag another window across the label. It then
won't repaint itself. How do I get it to do that?

Sincerely
Kenneth, Denmark
Mar 7 '06 #1
1 8193
Kenneth,

I think that your logic is wrong. Instead of passing the ClipRectangle,
what I would do is pass a rectangle which has the size of your label (from
0, 0 to length, width). The ClipRectangle is just the area of the screen
that needs repainting, not the whole label, so theoretically, your gradient
can shift depending on what needs to be refreshed.

Then, when you call FillRectangle, you can pass the clip rectangle and
the brush should be applied appropriately.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Kenneth Siewers Møller" <ke*****@siewers.dk> wrote in message
news:44***********************@dreader2.cybercity. dk...
Hi there

I have a custom label control (GradientLabel) which enherits from Label
and basically just paints the background of a label in a gradient.

In my code I have the following for the overidden OnResize event:

protected override void OnPaint(PaintEventArgs e)
{
Graphics g = e.Graphics;
using (LinearGradientBrush br = new
LinearGradientBrush(e.ClipRectangle, color1, color2, gradient_mode))
{
g.FillRectangle(br, e.ClipRectangle);
}
base.OnPaint(e);
}

(The Color1 and Color2 and gradient_mode are set using properties to get
the designtime representation to work)

Now the odd part is this. I have two different machines, a desktop and a
laptop, both running .NET 2.0. On my desktop there is no problem when I
resize the control and the gradient follows nicely, also when I maximize
the window etc.
On my laptop however, everything works except when I maximize the form in
which the label resides. The gradient simply cuts off and starts over
where the form was maximized from.
I have tried overriding a lot of different events, including the forms own
OnResize event and then through that calling the labels Refresh() method,
but no luck.

What is the problem? I suspect it for being a machine specific problem,
but it would be nice if I somehow could get a reason :)

Another problem is when I drag another window across the label. It then
won't repaint itself. How do I get it to do that?

Sincerely
Kenneth, Denmark

Mar 7 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: DraguVaso | last post by:
Hi, I have my custom Inherited DataGrid. I override the OnPaint-event to speed up things and avoid flikkering using Double Buffering. To draw the normal things of the DataGrid I have to call the...
11
by: Sagaert Johan | last post by:
I have made a custom control that draws a rectangle when the mouse is down, and does nothing when the mouse is up. I set/reset a flag in MouseDown/Mouse up and use this to do the drawing in the...
7
by: Jeroen Ceuppens | last post by:
Hi, When I draw a bitmap on a form, the bitmapdrawing disappears when I move the form, how is it possible to lock the drawing so it stays visible when you move or do somethings else.... Thx...
5
by: Ron Vecchi | last post by:
I have a custyom control that has a property which wraps a collection. When I add controls to the collection the OnPaint method of these newly added controls never get fired. But if I take the...
2
by: CMEDIA_SOUND | last post by:
I have a peculiar problem, I have a tabpage with a label control on it. When i set a background image to the tabpage and drag the label around it has paint issues in that it is slow, granted the...
1
by: Dave | last post by:
Hi all, I'm creating a control derived from System.Windows.Forms.Control I override the OnPaint() function The control takes place on a form with a background image. Because I've some problems...
20
by: BB | last post by:
Hello all, I am trying to override OnPaint in a custom textbox control (so I can drawstring a caption, etc.). In the code below, I get the "painting the form" message as expected, but not the...
1
by: Brian Henry | last post by:
I am trying to make a custom user control that gets a list of users from our database and populates the list, its an owner drawn control also, the problem is, I placed the item onto a form and...
7
by: Dino Buljubasic | last post by:
Hi, I am using C# 2.0 (VS2005) to build my user control that contains a number of dynamically loaded ListViews. ListViewItems have their ForeColor properties set to either black or blue to...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.