0

I've created a windows forms application that allows the user to paint/draw on a panel. I've been trying to retrieve the mouse or cursor positions (x, y) relative to the panel while the user is moving the mouse, and store these into an array or list. So when the MouseDown event is triggered, the current mouse position is stored into the first index of the array, and as MouseMove is occurring, the rest of the cursor (X, Y) positions are stored, until MouseUp happens. This is a snippet of my code in the relevant parts of this question:

List<double> mousePosX = new List<double>();
List<double> mousePosY = new List<double>();

bool shouldPaint = false;

private void Painter_MouseDown(object sender, MouseEventArgs e)
{
  if(e.Button == MouseButton.Left)
  {
     shouldPaint = true;
  }
}

protected void Painter_MouseMove(object sender, MouseEventArgs e)
{
    if(shouldPaint)
    {
        Graphics graphics = CreateGraphics();
        graphics.FillEllipse(new SolidBrush(Color.FromName(colorvar)), e.X, e.Y, brushSize, brushSize);

     mousePosX.Add(MousePosition.X);
     mousePosY.Add(MousePosition.Y);
    }
}

private void Painter_MouseUp(object sender, MouseEventArgs e)
    {
      if(e.Button == MouseButton.Left)
      {
         shouldPaint = false;
      }
    }

The issue I have is that my list is empty. Any suggestions?

BBaire
  • 71
  • 3
  • Thanks Thomas, I fixed that typo, and added the MouseUp and MouseDown events to show how I am using shouldPaint – BBaire Nov 26 '17 at 20:10
  • MouseButton.Left does not compile for me. When I change to MouseButtons.Left it compiles and works (I can draw on the screen and the lists get values added to them). Also MousePosition.X will be the screen coordinate e.X will be the position relative to the panel. – Cedar Nov 26 '17 at 21:30

0 Answers0