2014-02-20 38 views
0

功能我試圖創建下面的代碼的功能,在的WinForms。 你看到的是一個名爲ytplayerSearch的函數,而不是ytplayer。 目前的代碼工作,但我一直在努力使它更乾淨。創造的功能

private void btPlayMin_Click(object sender, EventArgs e) 
     { 
      ytplayer.playLink(sender, e, link); 
      ytplayer.miniMax("Normal", new Size(300, 24), new Size(400, 95), false, "", FormBorderStyle.FixedToolWindow, true); 
      ytplayer.TopMost = true; 
      ytplayer.BringToFront(); 
      ytplayer.TopMost = false; 
      int x = Screen.PrimaryScreen.WorkingArea.Width - 375; 
      ytplayer.Location = new Point(x, 0); 
      ytplayer.Show(); 
      this.Close(); 
     } 

     private void btPlayNorm_Click(object sender, EventArgs e) 
     { 
      ytplayer.playLink(sender, e, link); 
      ytplayer.miniMax("Minimal", new Size(300, 240), new Size(400,335), true, "YoutubePlayer", FormBorderStyle.FixedSingle, false); 
      ytplayer.TopMost = true; 
      ytplayer.BringToFront(); 
      ytplayer.TopMost = false; 
      ytplayer.Show(); 
      this.Close(); 
     } 

問題是我不太清楚如何發送函數到另一個函數。較低的代碼可能會讓我的問題更加清晰。

private void settingsYTP(.....problem.....) 
{ 
} 
+0

移動到參數所有不同的設置 – Grundy

回答

0

如果我理解正確的,你需要像這樣

private void btPlayMin_Click(object sender, EventArgs e) 
{ 
    settingsYTP(sender, e, link, "Normal", new Size(300, 24), new Size(400, 95), false, "", FormBorderStyle.FixedToolWindow, true); 
    int x = Screen.PrimaryScreen.WorkingArea.Width - 375; 
    ytplayer.Location = new Point(x, 0); 
    ytplayer.Show(); 
    this.Close(); 
} 

private void btPlayNorm_Click(object sender, EventArgs e) 
{ 
    settingsYTP(sender, e, link, "Minimal", new Size(300, 240), new Size(400,335), true, "YoutubePlayer", FormBorderStyle.FixedSingle, false); 
    ytplayer.Show(); 
    this.Close(); 
} 

private void settingsYTP(object sender, EventArgs e, string link, string minmax,Size size1, Size size2, bool b1, string text,FormBorderStyle bs, bool b2) 
{ 
    ytplayer.playLink(sender, e, link); 
    ytplayer.miniMax(minmax, size1, size2, b1, text, bs, b2); 
    ytplayer.TopMost = true; 
    ytplayer.BringToFront(); 
    ytplayer.TopMost = false; 
} 
+0

完美,這正是它 – Bjorn9000

+0

@ Bjorn9000也看到更多關於重構:提取方法 – Grundy

+0

(發送者,e,鏈接);'然後將正確的變量添加到調用'settingsYTP(sender,e,link,「Minimal」,new Size(300,240),新的Size(400,335),true,「YoutubePlayer」,FormBorderStyle.FixedSingle,false);'正如你所看到的那樣,它顯然缺少對象和發送者。偉大的幫助@Grundy – Bjorn9000

0

您可以使用代表。

delegate ReturnType YourFunctionDelegate(argType yourArgument); 

http://msdn.microsoft.com/en-us/library/ms173171.aspx

例如,說你要傳遞的功能是這樣的:

void MyFunction(int arg1, int arg2); 

可以聲明一個委託它:

delegate void MyFunctionDelegate(int arg1, int arg2); 

你可以將委託視爲表示具有特定簽名的函數的類型。

現在,回功能,要通過MyFunction。您可以通過指定代表YourFunctionDelegate的另一個參數來完成此操作。

private void settingsYTP(otherArguments, MyFunctionDelegate function) 
{ 
    // Some code ... 

    // Use the delegate function 
    function(1, 2); 

    // Some other cose... 
} 

最後,只是

MyFunctionDelegate functionDelegate = MyFunction; 
settingsYTP(otherArguments, functionDelegate); 
+0

相當新的代表,而不是一個聰明的程序員你可以啓發我? – Bjorn9000

+0

我編輯答案:) – elnigno

+0

感謝您在寫這篇的麻煩去,但我認爲生病以上回答去,好像我沒有得到正確的技能潛入代表,只是還沒有。但是,你知道任何好的教程可能有幫助嗎? – Bjorn9000

2

您可以使用委託的提議,但我寧願看着Func鍵<>和/或動作<>

示例從MSDN:

public class GenericFunc 
{ 
    public static void Main() 
    { 
     // Instantiate delegate to reference UppercaseString method 
     Func<string, string> convertMethod = UppercaseString; 
     string name = "Dakota"; 
     // Use delegate instance to call UppercaseString method 
     Console.WriteLine(convertMethod(name)); 
    } 

    private static string UppercaseString(string inputString) 
    { 
     return inputString.ToUpper(); 
    } 
}