473,398 Members | 2,120 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,398 software developers and data experts.

Rectangle/OnPaint Error in 2005

Why do I sometimes get this error; works fine most of the time:
Rectangle '{X=0,Y=0,Width=0,Height=0}' cannot have a width or height equal
to 0
void OnPaint(Object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
Rectangle r = ClientRectangle;
LinearGradientBrush gradientBrush = new LinearGradientBrush(r,
Color.Navy, Color.Chocolate, LinearGradientMode.ForwardDiagonal);
g.FillRectangle(gradientBrush, r);
}
Aug 16 '08 #1
4 2394
On Fri, 15 Aug 2008 17:47:12 -0700, Ryan <Ry**@discussions.microsoft.com>
wrote:
Why do I sometimes get this error; works fine most of the time:
Rectangle '{X=0,Y=0,Width=0,Height=0}' cannot have a width or height
equal
to 0
When do you get the error? What line of code causes it? Can you post a
concise-but-complete code sample that reliably reproduces the problem?

AFAIK Graphics.FillRectangle has no limitation on using an empty
Rectangle. But as near as I can tell, LinearGradientBrush does. If you
pass it an empty rectangle, it will throw the exception you describe.

I don't know why you have a control instance that is empty when it tries
to paint itself, but the solution would be to either not let it have an
empty rectangle as its dimensions, or to check the rectangle in the
OnPaint() method and just not try to draw anything if the ClientRectangle
is empty.

Pete
Aug 16 '08 #2
Peter,
The following line produces the error

LinearGradientBrush gradientBrush = new LinearGradientBrush(r, Color.Navy,
Color.Chocolate, LinearGradientMode.ForwardDiagonal);

Complete code:

void OnPaint(Object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
Rectangle r = ClientRectangle;
LinearGradientBrush gradientBrush = new LinearGradientBrush(r,
Color.Navy, Color.Chocolate, LinearGradientMode.ForwardDiagonal);
g.FillRectangle(gradientBrush, r);
}

"Peter Duniho" wrote:
On Fri, 15 Aug 2008 17:47:12 -0700, Ryan <Ry**@discussions.microsoft.com>
wrote:
Why do I sometimes get this error; works fine most of the time:
Rectangle '{X=0,Y=0,Width=0,Height=0}' cannot have a width or height
equal
to 0

When do you get the error? What line of code causes it? Can you post a
concise-but-complete code sample that reliably reproduces the problem?

AFAIK Graphics.FillRectangle has no limitation on using an empty
Rectangle. But as near as I can tell, LinearGradientBrush does. If you
pass it an empty rectangle, it will throw the exception you describe.

I don't know why you have a control instance that is empty when it tries
to paint itself, but the solution would be to either not let it have an
empty rectangle as its dimensions, or to check the rectangle in the
OnPaint() method and just not try to draw anything if the ClientRectangle
is empty.

Pete
Aug 18 '08 #3
On Mon, 18 Aug 2008 07:24:04 -0700, Ryan <Ry**@discussions.microsoft.com>
wrote:
Peter,
The following line produces the error

LinearGradientBrush gradientBrush = new LinearGradientBrush(r,
Color.Navy,
Color.Chocolate, LinearGradientMode.ForwardDiagonal);
Well, since I'd already guessed that, I can't say that adds much.
Complete code:

void OnPaint(Object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
Rectangle r = ClientRectangle;
LinearGradientBrush gradientBrush = new LinearGradientBrush(r,
Color.Navy, Color.Chocolate, LinearGradientMode.ForwardDiagonal);
g.FillRectangle(gradientBrush, r);
}
That's not complete code. Please see Jon Skeet's discussion on the topic
of code samples, both complete and incomplete:
http://www.yoda.arachsys.com/csharp/complete.html
http://www.yoda.arachsys.com/csharp/incomplete.html

In any case, I believe that my previous reply included enough information
to help you fix the problem. Was there something about that reply you
didn't understand?

Pete
Aug 18 '08 #4
Peter,
Your previous reply had given me enough information but I posted the OnPaint
method for your reference to get moer info.
The error is interminent and very difficult to reproduce; I have added the
follo
Thanks for your help.

I have modified the code as follows:
void OnPaint(Object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
Rectangle r = ClientRectangle;
if (!r.IsEmpty)
{
LinearGradientBrush gradientBrush = new
LinearGradientBrush(r, Color.Navy, Color.Chocolate,
LinearGradientMode.ForwardDiagonal);
g.FillRectangle(gradientBrush, r);
}
}
"Peter Duniho" wrote:
On Mon, 18 Aug 2008 07:24:04 -0700, Ryan <Ry**@discussions.microsoft.com>
wrote:
Peter,
The following line produces the error

LinearGradientBrush gradientBrush = new LinearGradientBrush(r,
Color.Navy,
Color.Chocolate, LinearGradientMode.ForwardDiagonal);

Well, since I'd already guessed that, I can't say that adds much.
Complete code:

void OnPaint(Object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
Rectangle r = ClientRectangle;
LinearGradientBrush gradientBrush = new LinearGradientBrush(r,
Color.Navy, Color.Chocolate, LinearGradientMode.ForwardDiagonal);
g.FillRectangle(gradientBrush, r);
}

That's not complete code. Please see Jon Skeet's discussion on the topic
of code samples, both complete and incomplete:
http://www.yoda.arachsys.com/csharp/complete.html
http://www.yoda.arachsys.com/csharp/incomplete.html

In any case, I believe that my previous reply included enough information
to help you fix the problem. Was there something about that reply you
didn't understand?

Pete
Aug 19 '08 #5

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

Similar topics

3
by: Colin McGuire | last post by:
Hi there. I have written a small procedure to draw various shapes on things. A bit of it is shown below. Private Sub drawShape(ByVal shapeType As Integer, ByRef g As Graphics) Select Case...
4
by: Colin McGuire | last post by:
Hi, this is a really simple question I have been banging my head on a brick wall over. The program below changes the background colour of a form depending on whether the cursor is inside a...
0
by: Colin McGuire | last post by:
Hi, this is a really simple question I have been banging my head on a brick wall over. The program below changes the background colour of a form depending on whether the cursor is inside a...
1
by: Colin McGuire | last post by:
Hi, this is a really simple question I have been banging my head on a brick wall over. The program below changes the background colour of a form depending on whether the cursor is inside a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.