2017-09-17 5 views
0

別のクラスでは、テキストボックスのイベントを持っている私は、Visual Studioに2015を使用して試みたが、私は複数のコンパイルエラーを投稿しましたが、本質的に興味深いのは、別のクラスのテキストボックスの_Leave()イベントを持つことができる場合です。 `ReusableMethods`が希望` Red`クラスへの参照を持っている、と `txtName`は、公に表示されている場合は</p> <p>....私はこのような何かをすることをお勧めではありません確信しているが、私はそれが可能である<em></em>場合だけ興味があった

public class Red 
{ 
    public TextBox TxtName {get; set; } 
} 

public static class ReusableMethods 
{ 
    public static void WireUp(Red red) 
    { 
     red.TxtName.Leave += txtName_Leave; 
    } 

    private static void txtName_Leave(object sender, EventArgs e) 
    { 
     //Do something here 
    } 
} 

//some other code 

var red1 = new Red() { TxtName = new TextBox() }; 
var red2 = new Red() { TxtName = new TextBox() }; 

ReusableMethods.WireUp(red1); 
ReusableMethods.WireUp(red2); 
+1

:あなたこれを行うことができます – Jonesopolis

+0

@ジョーンズポリス - 私はそれが可能だったのか不思議でした。私はそれが実際にそうすることは恐ろしい練習であると確信しています:) – BellHopByDayAmetuerCoderByNigh

答えて

1

namespace BlueGreenRed 
{ 
    public partial class Red : UserControl 
    { 
     //In this class I have a text box called txtName 
    } 
} 



namespace BlueGreenRed 
{ 
    class ReusableMethods 
    { 
     private void txtName_Leave(object sender, EventArgs e) 
     { 
      //Do something here 
     } 
    } 
} 

以下確かに何かのようにあなたは確かにイベントを配線することができます。しかし、それはちょうどその時点でばかげているようです。
関連する問題

 関連する問題