Solved

Draw lines

Posted on 2014-12-20
9
155 Views
Last Modified: 2014-12-20
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
Comment
Question by:Mike Eghtebas
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 40510518
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
 
LVL 70

Assisted Solution

by:Éric Moreau
Éric Moreau earned 100 total points
ID: 40510693
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
 
LVL 63

Assisted Solution

by:Fernando Soto
Fernando Soto earned 100 total points
ID: 40510812
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
How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

 
LVL 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 100 total points
ID: 40510881
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
 
LVL 34

Author Comment

by:Mike Eghtebas
ID: 40510887
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
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 40510938
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
 
LVL 34

Author Comment

by:Mike Eghtebas
ID: 40510949
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
 
LVL 44

Accepted Solution

by:
AndyAinscow earned 200 total points
ID: 40510955
Form designer, properties:
How to get the Paint event handlerDouble click the event you want and it will generate a function stub
0
 
LVL 34

Author Closing Comment

by:Mike Eghtebas
ID: 40510960
There are very good (additional) information on this thread to study later.

Thanks,

Mike
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Where is this file? 3 33
C# Formatting DateTime String Value 5 50
.NET Enums [Flags] & Bitwise Design Question 6 39
Header Font Size in Grid View 6 25
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question