Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Drawing a line in Winforms

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.

like image 713
Joshscorp Avatar asked Jul 03 '09 07:07

Joshscorp


People also ask

How do I draw a line in Visual Studio Form?

To draw a line on a form, you do the following: Set up a Graphics object with CreateGraphics() Set up a Pen object, and specify a colour and line width. Use the DrawLine Subroutine or method using your Pen, and some position coordinates.

How do you draw a line between two points in C#?

Simply use the Graphics. DrawLine() method.

How do you draw a line in C sharp?

To draw a line, an application first creates a Pen object, that defines the color and width. The following line of code creates a red pen with a width of 1: Pen redPen = new Pen(Color. Red, 1);


2 Answers

Quick & dirty:

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

like image 56
Natrium Avatar answered Oct 08 '22 09:10

Natrium


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.

like image 30
Fredrik Mörk Avatar answered Oct 08 '22 09:10

Fredrik Mörk