Solved

Draw lines

Posted on 2014-12-20
9
154 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 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 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Service Controller 1 23
Hey!! 5 33
Moving code from one TFS branch to another 3 25
Adding Existing Item to vb.net Project causes Errors 4 31
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

786 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