C#将变量从一种方法发送到另一种方法

C#将变量从一种方法发送到另一种方法

本文介绍了C#将变量从一种方法发送到另一种方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

c#windows form:



我正在尝试创建一个小绘图程序。



我无法将Form1_MouseClick字符串变量和Form1_MouseUp字符串变量发送到DrawLinesPoint。如何发送变量?





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 ?


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();
      }

推荐答案

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



等等on ...



-SA


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();
}



这篇关于C#将变量从一种方法发送到另一种方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 19:46