[英]How to modify a PictureBox from another class in Windows Form? C#
我正在開發一個解決方案,其中有一個Windows窗體,其中放置了一個pictureBox。 我想根據已經計算出的壓力中心值在屏幕上移動它。
我實例化了一個名為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部分。
你能幫我么?
非常感謝!
一些建議:
碼
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.