2011-12-21 23 views
5

vorrei chiedere,Passo dalla finestra figlia alla finestra padre

ho una finestra chiamata MainWindow e un altro chiamato ImportForm. Nel MainWindow io chiamo

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

Nella finestra secondaria faccio un po 'di roba e produco alcune variabili. come var1, var2, var3.

che voglio quando la finestra figlio vicino per tornare var1, var2, var3 al MainWindow e chiamare una funzione diciamo import_chart(var1, var2, var3) ..

Qualsiasi aiuto sarà apretiated. Grazie

+0

Si potrebbe provare a creare un evento e mettendo tali variabili al suo interno – jclozano

risposta

4

Sembra una scelta di design scomoda. In ogni modo, ecco come si può fare:

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); 
} 

Basta cambiare l'azione da tipi di parametri necessari (e regolare ImportForm_Closed (...) per usarli pure).

Se qualcosa non è chiaro, fammi sapere.

+0

Sì, è davvero utile ... Grazie mille ... I devo ... La tua risposta è stata davvero un risparmio di tempo .. – Anaisthitos

0

Come aggiungere un evento a ImportFinished "ImportFinished" in cui si passano i valori come eventargs. Questo evento viene attivato nell'evento Chiudi o Chiusura di ImportForm e gestito nella Finestra principale. È anche possibile visualizzare ImportForm come finestra di dialogo Modale e leggere i valori quando viene restituito il metodo ShowDialog.

0

Un modo semplice per farlo è quello di rendere var1, var2, e var3 istanza variabili che sono visibili nel campo di applicazione del genitore (ad esempio renderli public), e poi nel MainWindow, allegare alla manifestazione Closed, e leggi le variabili da (ImportForm)sender.

0

Ho un esempio dal mio codice. È abbastanza generico e abbastanza ovvio che posso condividerlo qui.

Ho fatto qualcosa in questo senso.

/// <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; 
    } 

ho creato una classe chiamata InputBoxResult come questo:

/// <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(); 
     } 
    } 
Problemi correlati