如何在Windows窗体中从另一个类修改PictureBox?C#

如何在Windows窗体中从另一个类修改PictureBox?C#,c#,winforms,C#,Winforms,我正在开发一个解决方案,其中我有一个Windows窗体,我在其中放置了一个pictureBox。我想在屏幕上移动它,这取决于我已经计算的压力中心的值 我实例化了一个名为pressureCenterManager的类,该类实现了所有功能 因此,在我的表格代码中,我有以下内容: pressureCenterManager.displayPressureCenter(pressureMatrix, this.plot, this.pbox_CoG); 我在var压力中心中得到了正确的值 这是我在di

我正在开发一个解决方案,其中我有一个Windows窗体,我在其中放置了一个pictureBox。我想在屏幕上移动它,这取决于我已经计算的压力中心的值

我实例化了一个名为pressureCenterManager的类,该类实现了所有功能

因此,在我的表格代码中,我有以下内容:

pressureCenterManager.displayPressureCenter(pressureMatrix, this.plot, this.pbox_CoG);
我在var压力中心中得到了正确的值

这是我在displayPressureCenter函数中的代码:

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
            {

            }
我不知道为什么,当执行pictureBox.Visible=true时它跳转到捕捉部分

你能帮帮我吗

非常感谢

一些建议:

  • 把例外情况扔回去。新程序员认为看到异常是不好的。不这样做只会引起混乱。重新播放或使用它记录并发送回调用者。您需要知道发生了错误
  • 当您必须执行GUI操作且不在主线程上时,Windows窗体调用具有InvokeRequest属性和Invoke方法来处理。使用它们。这里有一些变化
  • 代码

    参考资料:

    您进入捕获时会抛出什么错误?添加catch(异常e)并读取isCross thread operation not valid:控件“pnl_grafica”从创建它的线程以外的线程访问。displayPressureCenter方法中接收到的数据来自UI线程以外的另一个线程上下文,这就是您看到此错误的原因。要解决此问题,您必须使用MSDN文章中描述的调度器:
     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