2011-08-24 41 views
0

我有從childform傳遞布爾標誌,parentform問題。如何從childform傳遞標誌家長嗎?

我知道如何把它從parentform傳遞給childform例如:

上的MainForm:

Camera settings = new Camera(this.fullPath3); 
settings.ShowDialog(); 

在childform:

public partial class Camera : Form 
    { 
     //Zmienne przekazywane - sciezka do zapisu wzorca, 
     string _fullPath3; 

...

public Camera(string fullPath3) 
    { 

     InitializeComponent(); 
     _fullPath3 = fullPath3; 

,它的工作ING。如何添加一個布爾標誌,作爲我的childform的返回?

類似的東西:

在childform:

public Camera(string fullPath3, bool flag) 

上的MainForm:

Camera settings = new Camera(this.fullPath3,this.flag); 
    settings.ShowDialog(); 
    if (flag==true) text2.text="OK!"; 
+0

爲什麼不是你的子窗體上創建一個事件,只是鉤住你的父窗體上該事件。您將能夠像這樣將信息傳遞迴父表單。 – Jethro

+0

您可以使用活動。 [赫雷什](http://stackoverflow.com/questions/6382750/adding-an-event-handler-for-a-control-in-child-form-from-parent-form-in-c/6382869#6382869)我的例子 – Reniuz

回答

1

簡單,Camera是一個形式,所以只是一個公共屬性添加到它。

public class Camera : Form 
{ 
    private string _fullPath3; 
    private bool flag; 
    public Camera(string fullPath3) 
    { 

     InitializeComponent(); 
     _fullPath3 = fullPath3; 
    } 

    // set flag to something somewhere 
    public bool Flag{ get{ return flag; } } 

} 

現在只需:

Camera settings = new Camera(this.fullPath3); 
settings.ShowDialog(); 
if (settings.Flag) text2.text="OK!"; 

記得ShowDialog暫停執行!

+0

相機設置=新相機(this.fullPath3,this.flag)沒有 「this.flag」 工作 - >相機設置=新相機(this.fullPath3)TNX) – Elfoc

+0

@Elfoc - 是啊,我複製你的代碼並應該從ctor中刪除。答案已更新。 – Jamiec

0

就實現你的Camera形式的內部或公共財產,並在其中設置該屬性。

事情是這樣的:

Camera settings = new Camera(this.fullPath3,this.flag); 
    settings.ShowDialog(); 
    if (settings.Flag) text2.text="OK!";