0

I have a form and within the form are some controls, i want to draw some lines to act as the form boundary. Because some of the controls are dock to the sides of the form, the method below cannot be used as it only draw on the form but is later covered by the controls again.

        int penWidth = 1;
        Pen aPen = new Pen(Color.Red, penWidth);
        e.Graphics.DrawLine(aPen, 0, 0, 0, Height);
        e.Graphics.DrawLine(aPen, Width - penWidth, 0, Width - penWidth, Height);
        e.Graphics.DrawLine(aPen, 0, 0, Width, 0);
        e.Graphics.DrawLine(aPen, 0, Height - penWidth, Width, Height - penWidth);

i saw other solution including using a user control to 'draw' the line in the form, but i do not like that idea at all. I also though of iterate through the controls in the form as long as that are at the edge of the form, i will draw a line in them, however this doesn't work too.

foreach (Control ctl in this.Controls)
        {
            if (ctl.Left == 0) //at left edge
                ctl.CreateGraphics().DrawLine(aPen, 0, 0, Width, Height);
        }

It doesn't help at all or am i actually doing it wrong. Both the above are in the event method Form1_Paint. Is there a direct solution on drawing lines over control without the use of customize user control?

The current closest i had is Form As you can see the border around the dock label box is missing.

ishtarsg
  • 133
  • 10
  • 1
    You need to draw on an [overlay like this](https://stackoverflow.com/a/46633876/3110834) or [this one](https://stackoverflow.com/a/40209045/3110834). – Reza Aghaei Dec 13 '17 at 06:13
  • Actually more of a boundary on the edge of the form without interfering the standard triggering of the controls within it. I have updated the question a bit – ishtarsg Dec 13 '17 at 07:41
  • It is not crystal *exactly* what goes wrong. The Paint event of the Form class is optimized to reduce overhead that is not normally necessary when the form acts as a container. This can make it look like stuff you draw appears to get lost or ghosted when the window is resized. Put this.ResizeRedraw = true; in the form constructor. Forget about the 2nd snippet, it is badly broken. – Hans Passant Dec 13 '17 at 08:05
  • @HansPassant the form was never resized. It's just that the label is 'dock' to the top of the form. Hence the label is actually covering the red line behind it. – ishtarsg Dec 13 '17 at 23:55

0 Answers0