2011-12-21 66 views
5

我想問一下,通行證到父窗口

我有一個名爲MainWindow窗口和另一個叫ImportForm。 在MainWindow我打電話

private void generate_Window(int num_chart) 
{ 
    Window ownedWindow = new ImportForm(num_chart); 
    ownedWindow.Owner = this;  
    ownedWindow.Show(); 
} 

在子窗口我做一些東西,我產生了一些變數。像var1,var2,var3。

我希望在子窗口關閉返回var1var2var3MainWindow並調用一個函數讓我們說import_chart(var1, var2, var3) ..

任何幫助將apretiated。 謝謝

+0

你可以嘗試創建一個事件,並把這些變量裏面 – jclozano 2011-12-21 22:09:43

回答

4

這似乎是一個尷尬的設計選擇。不管怎麼說,這裏是你如何能做到這一點:

MainWindow.cs:

private void generate_Window(int num_chart) 
{ 
    Window ownedWindow = new ImportForm(num_chart, import_chart); 
    ownedWindow.Owner = this; 
    ownedWindow.Show(); 
} 

private void import_chart(int n, string s, bool b) 
{ 
    //Do something 
} 

ImportForm.cs:

private Action<int, string, bool> callback; 
public ImportForm(int num_chart, Action<int, string, bool> action) 
{ 
    InitializeComponent(); 
    Closed += ImportForm_Closed; 
    callback = action; 
} 

private void ImportForm_Closed(object sender, EventArgs e) 
{ 
    callback(0, "Test", false); 
} 

只要改變行動你所需要的參數類型(以及調整ImportForm_Closed (...)也可以使用它們)。

如果有什麼不清楚的地方,請告訴我。

+0

是的,它是真正有用的...謝謝了很多...我欠你......你的回答真的很省時。 – Anaisthitos 2011-12-21 17:29:40

0

如何將事件添加到您的ImportForm「ImportFinished」中,您將值作爲eventargs傳入。此事件在ImportForm的Close或Closing事件中觸發,並在您的MainWindow中處理。 您也可以將ImportForm顯示爲模式對話框,並在ShowDialog方法返回時讀取值。

0

一個簡單的方法來做到這一點是讓var1var2var3實例變量是在父的範圍內可見(如讓他們public),然後在MainWindow,重視Closed事件,從(ImportForm)sender讀取變量。

0

我有我自己的代碼的例子。這是通用和明顯的,我可以在這裏分享。

我已經做了一些沿着這些線。

/// <summary> 
    /// Show an InputBox similar to the pre-.NET InputBox functionality. Returns the original value when Cancel was pressed. 
    /// </summary> 
    /// <param name="OriginalValue">Pre-populated value in the input box</param> 
    /// <param name="PromptText">Prompt text displayed on the form</param> 
    /// <param name="Caption">Window caption</param> 
    /// <returns>InputBoxResult structure containing both the DialogResult and the input text. Warning: The input text will always be returned regardless of the DialogResult, so don't use it if the DialogResult is Cancel.</returns> 
    public static InputBoxResult Show(string OriginalValue = "", string PromptText = "", string Caption = "") { 
     InputBox form = new InputBox { 
      Text = Caption, 
      lblPrompt = {Text = PromptText}, 
      txtInput = {Text = OriginalValue} 
     }; 

     InputBoxResult res = new InputBoxResult(); 
     res.Result = form.ShowDialog(); 
     res.Input = form.txtInput.Text; 

     return res; 
    } 

我創建了一個名爲InputBoxResult這樣的類:

/// <summary> 
    /// Represents the results from an InputBox.Show call, including the DialogResult 
    /// and the input data. Note that the input data is always populated with the 
    /// user-entered data regardless of the DialogResult - this is inconsistent with 
    /// the old InputBox behavior and may change in the future. 
    /// </summary> 
    public struct InputBoxResult { 
     /// <summary> 
     /// Describes the way the dialog was resolved (OK/Cancel) 
     /// </summary> 
     public DialogResult Result { get; set; } 
     /// <summary> 
     /// User-entered text 
     /// </summary> 
     public string Input { get; set; } 

     /// <summary> 
     /// Translate this result into a string for easy debugging 
     /// </summary> 
     /// <returns></returns> 
     public override string ToString() { 
      return "Result: " + Result.ToString() + 
       "\r\nInput: " + Input.ToString(); 
     } 
    }