Click here to Skip to main content
15,916,949 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
c# windows form:

I'm trying to create a little drawing program.

I can't send the Form1_MouseClick string variable and Form1_MouseUp string variable to the DrawLinesPoint. How do I send the variables ?


C#
public void DrawLinesPoint(object sender, PaintEventArgs e)
      {

          // Create pen.
          Pen pen = new Pen(Color.Black, 3);
          // Create array of points that define lines to draw.
          Point[] points =
          {
          new Point(point1),
          new Point(point2)
          };
          //Draw lines to screen.
          e.Graphics.DrawLines(pen, points);
      }

      private void Form1_MouseClick(object sender, MouseEventArgs e)
      {

          string point1 = e.X + "," + e.Y;

      }

      private void Form1_MouseUp(object sender, MouseEventArgs e)
      {
          string point2 = e.X + "," + e.Y;

          this.Paint += new PaintEventHandler(DrawLinesPoint);
          Invalidate();
      }
Posted

"Sending" a string to pass actually a couple of integer parameters is a big, big abuse. It's hard to explain why so many beginners show this dead-end trend to work with strings representing data instead of data.

And passing parameters is one of the basics of programming, even before OOP. I'll tell you seriously: don't wast time on developing any UI, Graphics or any other advanced elements of programming before getting comfortable with the fundamentals.

It should be something like
C#
void MyHandler(x, y) { /* ... */ }
// or
void AnotherHandler(System.Drawing.Point point) { /* ... */ }

//...

MyHandler(e.X, e.Y);
AnotherHandler(new System.Drawing.Point(e.X, e.Y));

And so on…

—SA
 
Share this answer
 
You pass either a reference to a variable, or the value in a variable from one method/context/object to another. Whether a reference is passed, or a value, will depend on the Type of the object that is passed, reference, or value.

There is a special syntax for indicating passing a variable by reference in a way that its value(s) can be changed by the object it is passed to ('out, 'ref), the default case being that the Value Type contents of a variable passed by reference will be copied.

The first thing for you to realize is that when you declare a variable inside a Method, that variable "exists" only inside that Method; technically, this is expressed as saying the variable goes out of scope when the Method terminates. The remedy is to declare the variables at the level (the scope of) the Form.

Second, you are declaring a new PaintEventHandler every time you get a 'MouseUp: unlike declaring a variable this will create a new Event Object and add it to the Invocation List of the EventHandler, so you are going to end-up with a very bloated EventHandler ! The remedy is to assign the EventHandler once to the Paint Event, either at design-time, or, at run-time in the Form Load Event.

So, the 'point1, and 'point2 variables you declare and set the values of in the 'MouseClick and 'MouseUp EventHandlers can never be used outside those Methods.

Finally, your use of 'MouseClick, rather than 'MouseDown is a mistake; why the creators of WinForms made the MouseClick not register its location in the same way that MouseDown does ... in the case where you click, then drag, then mouse-up ... you better ask Microsoft about that.

Try this:
C#
private void Form1_Load(object sender, EventArgs e)
{
    this.Paint += Form1_Paint;
}

private Point[] twoPoints = new Point[2];

private Pen pen = new Pen(Color.Black, 3);

private void Form1_Paint(object sender, PaintEventArgs e)
{ 
    //Draw lines to screen.
    e.Graphics.DrawLines(pen, twoPoints);
}

private void Form1_MouseDown(object sender, MouseEventArgs e)
{
    twoPoints[0] = e.Location;
}

private void Form1_MouseUp(object sender, MouseEventArgs e)
{
    twoPoints[1] = e.Location;
    Invalidate();
}
 
Share this answer
 
v3
Use class level variables i.e. make point1 and point2 class level.
DrawLinesPoint will have access to point1 and point2.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900