2013-04-03 58 views
0

我有2種窗體,From1和Form2。通過單擊一個按鈕圖像發送到Form2將顯示在圖片框在表格2.更新窗體2中的數據而無需打開新窗體2

每次我點擊按鈕一個新的Form2打開和圖像顯示,但這不是我以後。

我希望Form2中pictureBox中的圖像得到更新,並且只有一個Form2(在第一次打開時)保持不變。

這是我在Form1的代碼:

Image<Bgr, byte> imResult =DrawMatches(imColor,Points1, imColorPrv,Points2,Indices, new Bgr(System.Drawing.Color.Yellow), new Bgr(System.Drawing.Color.Red); 
Form2 frmDrawMatchPoints = new Form2(imResult); 
frmDrawMatchPoints.Show(); 

這是我在Form2的代碼:

Image<Bgr, byte> imResult; 
public Form2(Image<Bgr, byte> imResult) 
{ 
    InitializeComponent(); 
    this.imResult = imResult; 
} 

private void Form2_Load(object sender, EventArgs e) 
{ 
    picBoxMatches.Image = imResult.ToBitmap(); 
} 

回答

2

你可以添加一個新的方法到窗體2:

public UpdateImage(Image<Bgr, byte> imResult) 
{ 
    this.imResult = imResult; 
    picBoxMatches.Image = imResult.ToBitmap(); 
} 

然後在Form1中,你可以這樣做:

private Form2 form2; // Some private field 

// Inside the event handler 
if (form2 == null) 
    form2 = new Form2(imResult); 
else 
    form2.UpdateImage(imResult); 
+0

在Form 1,首次form2應該分配在某處: form2 = new Form2(imResult); 否則在行「如果(窗口2 == NULL)」我得到的錯誤: 錯誤未分配的局部變量 – 2013-04-03 21:51:33

+0

@farzinparsa正如我在代碼中指出的使用,窗口2應該是含蓄地被設定成磁場空值。如果你的form2是一個對方法來說是局部變量的變量,這是行不通的,因爲只要方法結​​束它就會失去它的值。 – itsme86 2013-04-03 21:54:39

+0

我將Form2 form2 = null設置爲全局,它現在可以工作 – 2013-04-03 21:59:27

1

將這個全球範圍

public Form2 frmDrawMatchPoints = new Form2(); 

在窗體2結交新功能有相同的參數預覽圖片

public void PreviewPicture(Image<Bgr, byte> imResult) 
{ 
    this.imResult = imResult; 
} 

在Form1,你應該只給有:

Image<Bgr, byte> imResult =DrawMatches(imColor,Points1, imColorPrv,Points2,Indices, new Bgr(System.Drawing.Color.Yellow), new Bgr(System.Drawing.Color.Red); 

frmDrawMatchPoints.PreviewPicture(imResult); 
frmDrawMatchPoints.Show();