2013-03-16 39 views
2

我想獲得一個值回到父窗體,下面是我用於此的代碼,它工作正常,直到開始在面板控件中加載子窗體避免彈出窗口。在MainForm的在C#中傳遞Winforms值

代碼其中包含面板

MainMovement child = new MainMovement(new_dat, required_time, number); 
child.TopLevel = false; 
this.pnlmain.Controls.Add(child); 
child.Show(); 
child.BringToFront(); 
///Obtaining value back from the child form 
string updatingc = child.updatestatus; //This is not working, I am proceeding with some more functions depending on this value, but code does not work here after 

兒童形式所具有的公共價值作爲updatestatus並關閉​​子窗體之前設置的值。

請告知如何獲取此值。我相信這是將child.ShowDialog()更改爲child.Show()。 (爲了將表單加載到面板中,我不得不改變它,在此之前它工作正常)。

+2

在子窗體關閉時引發事件並在該事件中傳遞updatestatus的值。在主窗體上捕獲該事件並獲取該值。請參閱http://stackoverflow.com/questions/977326/propagating-events-from-one-form-to-another-form-in-c-sharp – 2013-03-16 07:29:46

回答

1

問題是.ShowDialog()在繼續之前等待DialogResult,而Show()只是顯示窗體並繼續。如果不知道你的孩子表格是如何工作的,那麼很難說,但我的猜測是你的孩子表格中的任何更新或設置updatestatus在你的代碼到達該行之前不會更新。

一個可能的解決方案涉及到代碼的重大重構。您可以將一個事件添加到您的MainMovement表單中,該表單在更改updatestatus時觸發。
請注意,我改變了你對updatestatusUpdateStatus把它變成一個屬性

public MainMovement : Form 
{ 
    public event EventHandler Updated; 
    private void OnUpdateStatus() 
    { 
     if (Updated != null) 
     { 
      Updated(this, new EventArgs()); 
     } 
    } 

    private String updatestatus; 
    public String UpdateStatus 
    { 
     get { return updatestatus; } 
     private set 
     { 
      updatestatus = value; 
      OnUpdateStatus(); 
     } 
    } 

    // rest of your child form code 
} 

public ParentForm : Form 
{ 
    public void MethodInYourExample() 
    { 
     // other code? 
     MainMovement child = new MainMovement(new_dat, required_time, number); 
     child.Updated += ChildUpdated; 
     child.TopLevel = false; 
     this.pnlmain.Controls.Add(child); 
     child.Show(); 
     child.BringToFront(); 
    } 

    void ChildUpdated(object sender, EventArgs e) 
    { 
     var child = sender as MainMovement; 
     string updatingc = child.UpdateStatus; 
     //rest of your code 
    } 

} 
+0

嗨,謝謝。但我得到follwong錯誤錯誤不能分配給'更新',因爲它是一個'方法組' – user2176442 2013-03-16 09:02:01

+0

@ user2176442並沒有告訴我很多,但你顯然是試圖分配一些東西到'更新'。什麼是導致問題的代碼行? – psubsee2003 2013-03-16 09:15:21

+0

對不起,這是我得到錯誤「child.Update + = ChildUpdated;」的代碼 – user2176442 2013-03-16 09:20:05

2

你可以通過構造函數傳遞主窗體的對象爲子窗體。如果你傳遞你的對象,你將有權訪問你的孩子的父表單的所有方法。你可以調用主類的任何公共方法來更新你的價值。

MainMovement child = new MainMovement(this,new_dat, required_time, number); 
child.TopLevel = false; 
this.pnlmain.Controls.Add(child); 
child.ShowDialog(); 
child.BringToFront(); 

把你的主要形式一個公共方法,

Public void UpdateValue(String pString) 
{ 
     // Update your value 
} 

在你的孩子的形式,你必須趕上「這個」全球對象。現在

private oMainForm as MainForm 

public void MainMovement(MainForm pObject,String new_dat, String required_time, Int number) 
{ 
    oMainForm = pObject; 
    // Your Code 
} 

您也可以叫你 'UpdateValue' 從子窗體的方法。

oMainForm.UpdateValue("Updated String"); 
+0

嗨,thanx,但ShowDialog()在將窗體加載到面板時不起作用。 – user2176442 2013-03-16 09:41:04

+0

你也可以用show()來嘗試相同的方法。試一試:-) – 2013-03-16 09:42:59

+0

不,將表單加載到面板時出錯 – user2176442 2013-03-16 09:54:38