ClientRectangle() Results Not Correct?

J

Joe Keller

Hello,

In writing custom components that, among other things, draw a border around
themselves (e.g. Panel component) I rely heavily on the "ClientRectangle()"
call to get the rectangle size of the component. However, I've found
without fail that if I rely solely on the measurements of what
ClientRectangle() returns to me, the right hand side and bottom of the
border will not be visible (it appears it is drawn past the bounds of the
component). Inevitably, I need to decrease the size of the width and the
height by 1 to get the border around the component (a rectangle) to draw
properly.

Is this expected behavior? Am I doing something incorrectly or is this
possibly a bug in how ClientRectangle() returns its results?

Sample code is listed below - just override the OnPaint() method of a Panel
component with the following code to see what I am talking about

protected override void OnPaint(PaintEventArgs e)
{
Graphics g;
Rectangle r;
Pen p;

g = e.Graphics;
r = this.ClientRectangle;
p = new Pen(Color.Black);

// If you comment out the following two lines the component will not draw
the border properly.
r.Width -= 1;
r.Height -= 1;

g.DrawRectangle(p,r);
g.Dispose();

base.OnPaint(e);
}


Thanks!

Joe
 
A

Alex Yakhnin, eMVP

Joe,

That's the way it's supposed to be. The ClientRectange's Width/Height is
equal to the actual width/height of the control. You have to take an account
for the width of the line itself (1 pixel). It means that the right /bottom
lines of the rectangle will be drawn past the boundaries of a control.

-
Alex Yakhnin, .NET CF MVP
http://www.intelliprog.com
 
J

Joe Keller

Ahhh - makes sense - thanks!

Joe

Alex Yakhnin said:
Joe,

That's the way it's supposed to be. The ClientRectange's Width/Height is
equal to the actual width/height of the control. You have to take an account
for the width of the line itself (1 pixel). It means that the right /bottom
lines of the rectangle will be drawn past the boundaries of a control.

-
Alex Yakhnin, .NET CF MVP
http://www.intelliprog.com
 

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