简体   繁体   中英

How to modify a PictureBox from another class in Windows Form? C#

I'm developing a solution where I have a Windows Form, where I've placed a pictureBox. I want to move it through the screen depending on the value of pressure center I have already calculated.

I instantiated a class called pressureCenterManager that makes all the functionalities.

So, in my form code I have the following:

pressureCenterManager.displayPressureCenter(pressureMatrix, this.plot, this.pbox_CoG);

I get a correct value in the var pressureCenter .

This is my code in the displayPressureCenter function:

public void displayPressureCenter(double[,] pressureMatrix, Plot plot, PictureBox pictureBox)
        {
            //Get matrix size
            int xSize = pressureMatrix.GetLength(0);
            int ySize = pressureMatrix.GetLength(1);
            try
            {
                //Get CoP and move pictureBox
                System.Windows.Point centerOfPressure = getCenterOfPressure(pressureMatrix);
                pictureBox.Visible = true;
                pictureBox.Parent = plot.plotView;
                //Calculamos el punto dónde hay que printar utilizando una regla de 3 y descontando la mitad del tamaño de la señal (para que quede centrada)
                System.Drawing.Point displayPositionCart = new System.Drawing.Point((int)Math.Round((centerOfPressure.X * plot.plotView.Width / xSize) - (pictureBox.Width / 2)), (int)Math.Round((centerOfPressure.Y * plot.plotView.Height / ySize) - (pictureBox.Height / 2)));
                //Pasamos a coordenadas de pantalla y aplicamos un offset para quitar el eje
                System.Drawing.Point displayPositionScre = CartesianToScreenCoordinates(displayPositionCart, plot.plotView);
                displayPositionScre.Offset(0, -70);
                pictureBox.Location = displayPositionScre;
            }
            catch
            {

            }

I don't know why, when executing pictureBox.Visible = true; it jumps to the catch section.

Can you please help me?

Many thanks!

Some recommendations:

  1. Throw your exceptions back. New programmers think this is bad to see exceptions. Not doing so just causes confusion. Either rethrow it or use it to log and send back to the caller. You need to know an error occurred.
  2. Windows Forms calls have an InvokeRequired property and Invoke method to handle when you have to do GUI operations and aren't on the main thread. Use them. Here's some changes.

Code

 public void displayPressureCenter(double[,] pressureMatrix, Plot plot, PictureBox pictureBox)
 {

      if ( pictureBox.InvokeRequired )
      {
          this.Invoke(delegate { displayPressureCenter(pressureMatrix, plot, pictureBox)});
          exit;
      }

            //Get matrix size
            int xSize = pressureMatrix.GetLength(0);
            int ySize = pressureMatrix.GetLength(1);
            try
            {
                //Get CoP and move pictureBox
                System.Windows.Point centerOfPressure = getCenterOfPressure(pressureMatrix);
                pictureBox.Visible = true;
                pictureBox.Parent = plot.plotView;
                //Calculamos el punto dónde hay que printar utilizando una regla de 3 y descontando la mitad del tamaño de la señal (para que quede centrada)
                System.Drawing.Point displayPositionCart = new System.Drawing.Point((int)Math.Round((centerOfPressure.X * plot.plotView.Width / xSize) - (pictureBox.Width / 2)), (int)Math.Round((centerOfPressure.Y * plot.plotView.Height / ySize) - (pictureBox.Height / 2)));
                //Pasamos a coordenadas de pantalla y aplicamos un offset para quitar el eje
                System.Drawing.Point displayPositionScre = CartesianToScreenCoordinates(displayPositionCart, plot.plotView);
                displayPositionScre.Offset(0, -70);
                pictureBox.Location = displayPositionScre;
            }
            catch (Exception e)
            {
                 throw e;
            }

  // Rest of your code

References: https://msdn.microsoft.com/en-us/library/system.windows.forms.control.invokerequired(v=vs.110).aspx https://msdn.microsoft.com/en-us/library/zyzhdc6b(v=vs.110).aspx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM