Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 159
  • Last Modified:

Draw lines

Why this code doesn't draw the line specified?
      private void Form1_Load(object sender, EventArgs e)
        {
            DrawLines(5, 5, 25, 24);
           // DrawLines(100, 181, 542, 181);
           //DrawLines(100, 263, 542, 263);
        }
     //   private void frmCustomerDetails_Shown(object sender, EventArgs e)
     //   {

     //DrawLines(5, 5, 125, 124);
     //   }

        // DRAW LINES SUB

        private void DrawLines(int x, int y, int x2, int y2)
        {
            //'Pen myPen = new Pen(Color.Black, 8);
            System.Drawing.Pen myPen = new System.Drawing.Pen(System.Drawing.Color.Red);
            System.Drawing.Graphics formGraphics = default(System.Drawing.Graphics);
            formGraphics = this.CreateGraphics();
            formGraphics.DrawLine(myPen, x, y, x2, y2);
            myPen.Dispose();
            formGraphics.Dispose();

        }

Open in new window

0
Mike Eghtebas
Asked:
Mike Eghtebas
4 Solutions
 
AndyAinscowCommented:
I think it is drawn.  However when the form is repainted then your line is painted over.
Try putting this code into the Paint event of the form.
0
 
Éric MoreauSenior .Net ConsultantCommented:
it looks like you are creating a line but not attaching it to any controls.

look at http://emoreau.com/Entries/Articles/2011/08/The-poor-man-charting-option.aspx. The last line of my DrawChart method, I do "picChart.Image = objBM" (taking the bitmap I just created and displaying it in a Picture control).
0
 
Fernando SotoCommented:
Hi eghtebas;

What you are experiencing is the following. The Form_Load event gets executed and the line does get drawn on the form. At this point the form is not yet drawn on the display. Next the Form_Paint event gets executed to show the form, when this happens it over writes the lines you placed on the form. One way to over come this issue is to place a flag variable in the form class telling the Form_Paint to redraw your graphics. As long as you have this flag the Form_Paint should continue to re-paint your graphics. The sample code show how this may bee done.

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

    // Initial set up for your graphics line
    private bool showMyFormGraphics = false;
    private int x = 0;
    private int y = 0;
    private int x2 = 0;
    private int y2 = 0;

    private void button1_Click(object sender, EventArgs e)
    {
      // Execute these lines of code when you no longer wish your graphics to be displayed
      showMyFormGraphics = false;
      this.Refresh();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
      // Initialize the line to be drawn
      x = 5;
      y = 5;
      x2 = 25;
      y2 = 25;
      showMyFormGraphics = true;
    }

    // DRAW LINES SUB

    private void DrawLines(int x, int y, int x2, int y2)
    {
        //'Pen myPen = new Pen(Color.Black, 8);
        System.Drawing.Pen myPen = new System.Drawing.Pen(System.Drawing.Color.Red);
        System.Drawing.Graphics formGraphics = default(System.Drawing.Graphics);
        formGraphics = this.CreateGraphics();
        formGraphics.DrawLine(myPen, x, y, x2, y2);
        myPen.Dispose();
        formGraphics.Dispose();

    }

    private void Form1_Paint(object sender, PaintEventArgs e)
    {
      // Test to see if you need to draw your graphics
      if( showMyFormGraphics )
      {
          DrawLines(x, y, x2, y2);
      }
    }
}

Open in new window

0
Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

 
käµfm³d 👽Commented:
Probably just being pedantic, but I believe the painting should be accomplished by overriding OnPaint, not by handling the Paint event. Furthermore, a Graphics object is expensive to create. You should be using one you are provided versus creating one yourself. Conveniently enough, OnPaint (and even when you handle the Paint event) provides you with one by way of its parameter.
0
 
Mike EghtebasDatabase and Application DeveloperAuthor Commented:
Hi Fernando,

Line 6 below never gets fired.


      private void Form1_Paint(object sender, PaintEventArgs e)
        {
            // Test to see if you need to draw your graphics
            //if (showMyFormGraphics)
            //{
                DrawLines(x, y, x2, y2);
            //}
        }

Open in new window

0
 
AndyAinscowCommented:
Did you just copy the code from Fernando or do as I suggested and added a handler for the paint event which would give you a function called eg. Form1_Paint(object sender, PaintEventArgs e) BUT that function is actually called when the form is painted.
0
 
Mike EghtebasDatabase and Application DeveloperAuthor Commented:
Hi Andy,

In code window, while in Form_Load, I looked for paint event I couldn't see any in the events drop box top-right.

After using the solution from Fernando (with paint event in it), I see that it doesn't get fired.

How can I get it fired automatically on load?
0
 
AndyAinscowCommented:
Form designer, properties:
How to get the Paint event handlerDouble click the event you want and it will generate a function stub
0
 
Mike EghtebasDatabase and Application DeveloperAuthor Commented:
There are very good (additional) information on this thread to study later.

Thanks,

Mike
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now