Solved

GDI Drawing Lines triggered off a button click.

Posted on 2004-04-01
4
640 Views
Last Modified: 2010-05-18
I have no problem with the following code:

/***************************
      protected override void OnPaint(PaintEventArgs pe)
      {
            Graphics g = pe.Graphics ;
            Pen L1Pen = new Pen(Color.Red);
            Point L1x = new Point(100, 800);
            Point L1y = new Point(275, 800);
            
            Pen L2Pen = new Pen(Color.Blue);
            Point L2x = new Point(275, 800);
            Point L2y = new Point(625, 800);
            
            Pen L3Pen = new Pen(Color.Orange);
            Point L3x = new Point(625, 800);
            Point L3y = new Point(775, 800);
            
            Pen L4Pen = new Pen(Color.Black);
            Point L4x = new Point(775, 800);
            Point L4y = new Point(950, 800);
            
            
            
            g.DrawLine(L1Pen, L1x, L1y);
            g.DrawLine(L2Pen, L2x, L2y);
            g.DrawLine(L3Pen, L3x, L3y);
            g.DrawLine(L4Pen, L4x, L4y);
      }

***************/

However when I attempt to build the following code to trigger it in a off a button click I receive the following build error: The type or namespace name 'pe' could not be found (are you missing a using directive or an assembly reference?)
.

For example:
/*******************************
private void button1_Click(object sender, System.EventArgs e)
      {
            Graphics g = pe.Graphics ;
            Pen L1Pen = new Pen(Color.Red);
            Point L1x = new Point(100, 800);
            Point L1y = new Point(275, 800);
            
            Pen L2Pen = new Pen(Color.Blue);
            Point L2x = new Point(275, 800);
            Point L2y = new Point(625, 800);
            
            Pen L3Pen = new Pen(Color.Orange);
            Point L3x = new Point(625, 800);
            Point L3y = new Point(775, 800);
            
            Pen L4Pen = new Pen(Color.Black);
            Point L4x = new Point(775, 800);
            Point L4y = new Point(950, 800);
            
            
            
            g.DrawLine(L1Pen, L1x, L1y);
            g.DrawLine(L2Pen, L2x, L2y);
            g.DrawLine(L3Pen, L3x, L3y);
            g.DrawLine(L4Pen, L4x, L4y);
}

**************/
0
Comment
Question by:int200
[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
  • 2
  • 2
4 Comments
 
LVL 5

Accepted Solution

by:
millsoft earned 125 total points
ID: 10736225
Hi int200,

In the first function 'pe' is an argument to the Paint function.  You need to get the Graphics object to the window upon which you want to draw in the Click Event.

Is it the Button's parent window?  
For example:

            private void PaintMe( Graphics g )
            {
                  Pen L1Pen = new Pen(Color.Red);
                  Point L1x = new Point(10, 80);
                  Point L1y = new Point(27, 80);
         
                  Pen L2Pen = new Pen(Color.Blue);
                  Point L2x = new Point(27, 80);
                  Point L2y = new Point(62, 80);
         
                  Pen L3Pen = new Pen(Color.Orange);
                  Point L3x = new Point(62, 80);
                  Point L3y = new Point(77, 80);
         
                  Pen L4Pen = new Pen(Color.Black);
                  Point L4x = new Point(77, 80);
                  Point L4y = new Point(95, 80);
         
         
         
                  g.DrawLine(L1Pen, L1x, L1y);
                  g.DrawLine(L2Pen, L2x, L2y);
                  g.DrawLine(L3Pen, L3x, L3y);
                  g.DrawLine(L4Pen, L4x, L4y);
            }

            private void button1_Click(object sender, System.EventArgs e)
            {
                  System.Windows.Forms.Button b = (System.Windows.Forms.Button) sender;
                  PaintMe( b.Parent.CreateGraphics() );
            }
Cheers!
Brad
0
 

Author Comment

by:int200
ID: 10738275
Thanks for the response, but I just put in your code and can not see any graphics drawn on the window.  I created a simple C# Windows App, created a button to the form and added your code.
Here is the entire code:

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace GDITest2
{
      /// <summary>
      /// Summary description for Form1.
      /// </summary>
      public class Form1 : System.Windows.Forms.Form
      {
            private System.Windows.Forms.Button button1;
            /// <summary>
            /// Required designer variable.
            /// </summary>
            private System.ComponentModel.Container components = null;

            public Form1()
            {
                  //
                  // Required for Windows Form Designer support
                  //
                  InitializeComponent();

                  //
                  // TODO: Add any constructor code after InitializeComponent call
                  //
            }

            /// <summary>
            /// Clean up any resources being used.
            /// </summary>
            protected override void Dispose( bool disposing )
            {
                  if( disposing )
                  {
                        if (components != null)
                        {
                              components.Dispose();
                        }
                  }
                  base.Dispose( disposing );
            }

            #region Windows Form Designer generated code
            /// <summary>
            /// Required method for Designer support - do not modify
            /// the contents of this method with the code editor.
            /// </summary>
            private void InitializeComponent()
            {
                  this.button1 = new System.Windows.Forms.Button();
                  this.SuspendLayout();
                  //
                  // button1
                  //
                  this.button1.Location = new System.Drawing.Point(72, 64);
                  this.button1.Name = "button1";
                  this.button1.Size = new System.Drawing.Size(96, 32);
                  this.button1.TabIndex = 0;
                  this.button1.Text = "button1";
                  //
                  // Form1
                  //
                  this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
                  this.ClientSize = new System.Drawing.Size(292, 266);
                  this.Controls.Add(this.button1);
                  this.Name = "Form1";
                  this.Text = "Form1";
                  this.ResumeLayout(false);

            }
            #endregion

            
            private void PaintMe( Graphics g )
            {
                  Pen L1Pen = new Pen(Color.Red);
                  Point L1x = new Point(100, 1000);
                  Point L1y = new Point(100, 825);
         
                  Pen L2Pen = new Pen(Color.Blue);
                  Point L2x = new Point(100, 825);
                  Point L2y = new Point(100, 475);
         
                  Pen L3Pen = new Pen(Color.Orange);
                  Point L3x = new Point(100, 475);
                  Point L3y = new Point(100, 325);
         
                  Pen L4Pen = new Pen(Color.Black);
                  Point L4x = new Point(100, 325);
                  Point L4y = new Point(275, 325);
         
         
         
                  g.DrawLine(L1Pen, L1x, L1y);
                  g.DrawLine(L2Pen, L2x, L2y);
                  g.DrawLine(L3Pen, L3x, L3y);
                  g.DrawLine(L4Pen, L4x, L4y);
            }

            private void button1_Click(object sender, System.EventArgs e)
            {
                  System.Windows.Forms.Button b = (System.Windows.Forms.Button) sender;
                  PaintMe( b.Parent.CreateGraphics() );
            }

            /// <summary>
            /// The main entry point for the application.
            /// </summary>
            [STAThread]
            static void Main()
            {
                  Application.Run(new Form1());
            }
      }
}

Thanks
0
 
LVL 5

Expert Comment

by:millsoft
ID: 10738334
There are two possibilites.

First, it doesn't appear the button has a event handler wired up.

You need something like
this.button1.click += new ....

I'm not at my C# machine so I don't have the exact syntax handy.

Second, the Y coordinate you are using (825) is probably off the bottom of the window.  That is why I used 80 instead for testing purposes.

0
 

Author Comment

by:int200
ID: 10738362
Hi Brad,

You were correct, I didn't double click on the button from the form which auto-generates the handler.  I just added the button and put the code in.

It works fine now.  Thanks!  By the way, I'm running 1600x1200 so that's why I'm using such large values.

Now, I need to look into how to pass variables a the points so I can use one function I can pass the x and y coorinates to.

Thanks and Best Regards!
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

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
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 …
Suggested Courses

617 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