title: C# Control Two Motors according to the Mouseclicks on a Picturebox
body: <p>I am developing a C# win form GUI for controlling a two-motor XY stage. I have drawn a 100 x 100 square grid pattern on a picturebox in which each square, when clicked, represents a coordinate that the two motors must move to. I have studied this link
  <a href="https://stackoverflow.com/questions/12711843/picturebox-grid-and-selecting-individual-cells-when-clicked-on">PictureBox Grid and selecting individual cells when clicked on</a> and this <a href="https://stackoverflow.com/questions/12737103/picturebox-grids-and-filling-in-squares-game-of-lifefor">PictureBox- Grids and Filling in squares (Game of Life)</a> for drawing a grid and marking the clicked positions.</p>
  
  <p>Now I have to transform the series of randomly clicked points to actual movement of the two motors.</p>
  
  <p>How shall I translate the click coordinates programmatically to give commands to control the motors? </p>
  
  <p>I know how to move and control the motors without referring to the screen coordinates, i.e. by using eyes.  </p>
  
  <p>Thank you very much for your kind help.</p>
  
  <p>Update1:
  Hello... I think I am thinking too much in a confusing way to move the motors from one point to another despite Sebastien's great help. I wanted to try some logic below but I appreciate if somebody can enlighten me how best to implement this. </p>
  
  <pre><code>    private void pictureBoxGrid_MouseClick(object sender, MouseEventArgs e)
      {
          //int x = e.X;
          int x = cellSize * (e.X / cellSize);
          int y = cellSize * (e.Y / cellSize);
          int i = x / 8; // To limit the value to below 100
          int j = y / 8;
  
          // Reverse the value of fill_in[i, j] - if it was false, change to true,
          // and if true change to false
          fill_in[i, j] = !fill_in[i, j];
  
          if (fill_in[i, j])
          {
              //Save the coordinate in a list
              filledSq.Add(new Point(i, j));
  
              using (Graphics g = Graphics.FromImage(buffer))
              {
                  g.FillRectangle(Brushes.Black, x + 1, y + 1, 7, 7);
              }
  
          }
          else
          {
              //Delete the coordinate in a list
              filledSq.Remove(new Point(i, j));
              Color customColor = SystemColors.ControlLightLight;
  
              using (Graphics g = Graphics.FromImage(buffer))
              using (SolidBrush shadowBrush = new SolidBrush(customColor))
              {
                  g.FillRectangle(shadowBrush, x + 1, y + 1, 7, 7);
              }
  
          }
          //pictureBoxGrid.BackgroundImage = buffer;
          pictureBoxGrid.Invalidate();
      }
  
      private void buttonSavePoints_Click(object sender, EventArgs e)
      {
          // to be implemented...
      }
  
      private void buttonRun_Click(object sender, EventArgs e)
      {
  
          var noOfDots = filledSq.Count;
          filledSq = filledSq.OrderBy(p =&gt; p.X).ThenBy(p =&gt; p.Y).ToList();
          var motor = new Motor();
          for (var i = 0; i &lt; noOfDots; i++)
          {
            motor.Move(filledSq[i].X, filledSq[i].Y); //call the motor to move to X,Y here?
           //do sth at each position
          }
      }
  </code></pre>