Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to draw the straight line with DrawLine() method.If someone knows how please help.Thank you.

What I have tried:

public override void DrawSelf(Graphics grfx)
       {
           base.DrawSelf(grfx);

          grfx.FillRectangle(new SolidBrush(FillColor), Rectangle.X, Rectangle.Y, Rectangle.Width, Rectangle.Height);
          grfx.DrawRectangle(Pens.Black,Rectangle.X, Rectangle.Y, Rectangle.Width, Rectangle.Height);

          grfx.DrawLine(Pens.Black, Rectangle.Left, Rectangle.Top, Rectangle.Right, Rectangle.Bottom);
          grfx.DrawLine(Pens.Black, Rectangle.Right , Rectangle.Top , Rectangle.Left, Rectangle.Bottom );
          grfx.DrawLine(Pens.Black, Rectangle.Right, Rectangle.Top, Rectangle.Left+Rectangle.Right, Rectangle.Bottom);

       }
Posted
Updated 13-Jun-20 9:32am
v2

1 solution

I'm not at all sure what you intend that last line to do: but since it starts at the right hand side and goes further right from there it won't be at all visible within the rest of your drawing:
C#
grfx.DrawLine(Pens.Black, Rectangle.Right, Rectangle.Top, Rectangle.Left+Rectangle.Right, Rectangle.Bottom);
Since both left and right will be positive, left + rigth will be greater than right alone (unless left is zero, in which case it's a vertical line you've already drawn).
 
Share this answer
 
Comments
Nadi G. 13-Jun-20 15:53pm    
I drawn the diagonals and now i am trying to draw a line which pass the middle of the rectangle.
OriginalGriff 13-Jun-20 15:57pm    
A vertical line down the middle would be
X = left + (right - left) / 2, Y = top
to
X = same, Y = bottom.
Nadi G. 13-Jun-20 16:05pm    
Thank you.
OriginalGriff 13-Jun-20 17:04pm    
You're welcome!
Nadi G. 14-Jun-20 15:41pm    
How to draw the line only to the center not all diameter?
I need to take the side/2 and take the half line
sorry for asking but my Math thinking is bad.

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