首页 > 解决方案 > 如何从 Windows 窗体中的另一个类修改 PictureBox?C#

问题描述

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

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

所以,在我的表单代码中,我有以下内容:

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

我在 var pressureCenter中得到了正确的值。

这是我在 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; 它跳转到 catch 部分。

你能帮我么?

非常感谢!

标签: c#winforms

解决方案


一些建议:

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

代码

 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

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


推荐阅读