2012-03-18 11 views
0

私は親フォームからアクションを設定したいuserControl関数を持っています。私はどのように親フォームからuserControl関数のアクションを設定しますか?

私は既にその親フォームからuserControlボタンアクションを設定しています。 、それがこのように働いた:コードアップは素晴らしい作品

public void SetSendButton(Action action) 
    { 
     btnSend.Click += (s, e) => action(); 
    } 

:Form1.csで

:userControl1.csで

public Form1() 
    { 
     InitializeComponent(); 
     fileManagerLocal1.SetSendButton(SendMethod); 
    } 
    private void SendMethod() 
    { 
     //whatever ... 
    } 

。 しかし、私は必要私はファンクションアクションを設定するのですか..ですuserControl1.cs事前に

public void RefreshLocalFM(Action action) 
    { 
     action(); // what should be in here ? 
    } 

おかげでForm1.csを

public Form1() 
    { 
     InitializeComponent(); 
     fileTransfer1.RefreshLocalFM(RefreshFM); 
    } 

public void RefreshFM() 
    { 
     fileManagerLocal1.btnRefresh.PerformClick(); 
    } 

。 :)

+1

あなたは、その後のFunc/FUNC の代わりに、アクション/アクションを使用することを検討して、ボイドの代わりにパラメータを返す関数を取る必要がある場合。 –

+0

@ KasperHoldum私は、その関数が任意の値を返すことを望んでいません。私はRefresLocalFM(アクションアクション)に何を書き込むべきかを知る必要があります。 –

答えて

1

私はForm1.csで..

を解決し、それを考え出した:

public Form1() 
{ 
    InitializeComponent(); 
    fileTransfer1.refreshAction = new Action (RefreshFM); 
    //let's say refreshAction is a public action variable in fileTransfer1 class 
} 

public void RefreshFM() 
{ 
    fileManagerLocal1.btnRefresh.PerformClick(); 
} 
userControl1.csで

public Action refreshAction; 
//then it can be called from any place. 

private void RefreshLocalFM() 
{ 
    refreshAction.Invoke(); //this fires the action that we initialized from form1.cs 
} 
1

「ファンクションアクションを設定する」という意味は私には分かりません。 コントローラのコンテキスト内ですぐにその関数を呼び出すのですか?その場合、あなたが提供したコードは正しいです。 一方、あなたには、いくつかの後のコードで提供アクションを使用するユーザーコントロールを設定したい場合は、UserControl1の

Action externalFunction = null; 
public void RefreshLocalFM(Action action) 
    { 
     externalFunction = action; 
    } 

// later code 
private void someMethod() 
{ 
    externalFunction(); 
} 

を次のように、あなたはそのアクションを格納する必要があり、私は私はあなたを理解願っています正しく..

関連する問題