Click here to Skip to main content
15,885,890 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am trying to draw a rectangle on another rectangle on the bottom center of the parent rectangle. I am using some very basic code that I thought would work but it does not display the child rectangle at all I called bottomconnector. Below is some of my code"

C#
[Browsable(false)]
public Rectangle BottomConnector
{
   get
   {
        return new Rectangle(this.ClientRectangle.Width / 2, this.ClientRectangle.Bottom, 30, 30)
   }
}


Then in my onpaint override I have other code but this is what I used for this connector rectangle.

C#
// Draw Bottom Connector
g.FillRectangle(new SolidBrush(this.TitleBarColor), this.BottomConnector);


It may be a problem with my calculation for the position but I just don't see where I am messing up at. Thanks for anyone's help on this!

TheChazm
Posted

1 solution

Try this:

C#
return new Rectangle((this.ClientRectangle.Width / 2) - 15, this.ClientRectangle.Bottom - 30, 30, 30)


They way you were doing it, the top of the new rectangle starts at the exact bottom of the first one, so when it draw, it goes below the bottom of the first, making it so you can't see it.

The -15 part is to center it in the center of the rectangle. Basically newWidth / 2.
 
Share this answer
 
v2
Comments
thechazm 25-Jan-12 14:30pm    
Ok yes I do see the rectangle now but I do want it sticking out below the initial rectangle as this is a connector for an object. Kindof like visio or power point objects. Do you know how I can accomplish this?

Thanks for the info.
Tim Groven 25-Jan-12 14:32pm    
How are you drawing the parent rectangle? Is the area you are drawing on big enough to show it?
thechazm 25-Jan-12 14:50pm    
I am drawing the parent rectangle (or rectangle's) in a panel that fills most of the form. So yes plenty of room for that. The construction of the custom control or parent (as we are calling it) inherits from the Control class. Then I pass it the size with (this.Size = new Size(300, 150);). And in my onpaint override I fill the control with a color (g.FillRectangle(new SolidBrush(this.BackColor), this.ClientRectangle);).
thechazm 25-Jan-12 14:53pm    
So if I understand you correctly with your statement's above is that I cannot draw a rectangle outside of my control? Can I stack onther controls on top of my control that would stick out just a little? The Idea is that I want it to be one object but the connector points will be used for resizing and drawing lines from.
Tim Groven 25-Jan-12 15:05pm    
You cannot draw outside the bounds of the control you are drawing on, no. :(

Is it just a simple panel that you are drawing everything onto? Or you have a custom control that goes on the panel, and is being drawn on there?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900