Solved

Draw lines

Posted on 2014-12-20
9
146 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
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 69

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 62

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
 
LVL 74

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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 33

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 33

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 33

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
visual Basic Net vs c# net 8 54
Hide Tab Page 3 18
Enable copy and pasting even when textbox is disabled 2 16
Getfiles in vb.net 28 18
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

705 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now