Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Draw lines

Posted on 2014-12-20
9
Medium Priority
?
158 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 400 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 64

Assisted Solution

by:Fernando Soto
Fernando Soto earned 400 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 400 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 800 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

Congratulations! You’re Certified – Now What?

Starting a new career can be overwhelming. Becoming certified in your field of expertise is a great start, but where do you go from here?  Here are some tips to help you on your career journey.

Question has a verified solution.

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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

715 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