views:

752

answers:

4

Hi,

I am having trouble drawing a line within a group box in a simple windows form.

here is my code:

====================================================================================

public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();                        
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);            
            DrawLShapeLine(groupBox1.CreateGraphics(), 10, 10, 20, 40);
        }

        public void DrawLShapeLine(System.Drawing.Graphics g, int intMarginLeft, int intMarginTop, int intWidth, int intHeight)
        {
            Pen myPen = new Pen(Color.Black);
            myPen.Width = 2;
            // Create array of points that define lines to draw.
            int marginleft = intMarginLeft;
            int marginTop = intMarginTop;
            int width = intWidth;
            int height = intHeight;
            int arrowSize = 3;
            Point[] points =
             {
                new Point(marginleft, marginTop),
                new Point(marginleft, height + marginTop),
                new Point(marginleft + width, marginTop + height),
                // Arrow
                new Point(marginleft + width - arrowSize, marginTop + height - arrowSize),
                new Point(marginleft + width - arrowSize, marginTop + height + arrowSize),
                new Point(marginleft + width, marginTop + height)
             };

            g.DrawLines(myPen, points);
        }
    }

====================================================================================

If I attach the DrawLShapeLine method to a button click event, it draws fine, but it does not draw on load of the form.

Please advice.

+3  A: 

Hook up an event handler for the Paint event of the GroupBox and call DrawLShapeLine from within that event handler instead. You should then use the Graphics object supplied by in event arguments:

private void groupBox1_Paint(object sender, PaintEventArgs e)
{
    DrawLShapeLine(e.Graphics, 10, 10, 20, 40);
}

As your code looks now it will attempt to paint in the GroupBox when the form requires painting. The group box may be painted at any other occasion, which will the line you paint disappear.

Fredrik Mörk
A: 

I'm not sure if something else is going on, but you should draw the line on the GroupBox's Paint event, not the Form's.

lc
+2  A: 

Quick & dirty:

How about creating a panel with the width of 1 pixel and give it a backgroundcolor?

Natrium
This doesn't do diagonal lines tho.
lc
Good tip if you don't *want* diagonal lines and want to avoid GDI+, though.
Neil Barnwell
A: 

you need to add this.Invalidate(); in OnPaint method override:

    protected override void OnPaint(PaintEventArgs e)
 {
  base.OnPaint(e);
  DrawLShapeLine(groupBox1.CreateGraphics(), 10, 10, 20, 40);
  this.Invalidate();

 }
Andrija
You mean that on each paint, we first paint the form and then invalidate to force it to be painted again? Or do I miss something?
Fredrik Mörk
yes, I did this long time ago I'm don't even remember why does it have to be called in Paint method again, but try it out code won't go into recursion.
Andrija