2016-08-02 5 views
0

コードは動作しますが、送信者はSpecialTextBoxで、TextBoxはSpecialTextBoxではありません。なぜなら、TextBox.Leaveイベントが発生したときに、SpecialTextBoxから「IdCode」を取得する必要があるからです。私のコンテナは、それが含まれているコントロールによってトリガされたイベントの送信者になります

このように、送信者はSpecialTextBox内のTextBoxのようです。テキストボックスが含まれています

希望、これは理にかなって...

マイパネル...私は簡単な例を維持するための機能のほとんどを削除した

class BigPanel: Panel 
{ 
    SpecialTextBox stb = new SpecialTextBox(); 

    public BigPanel() 
    { 
     BorderStyle = BorderStyle.FixedSingle; 
     stb.SpecialTBLeave += Stb_SpecialTBLeave; 
     Controls.Add(stb); 

    } 

    private void Stb_SpecialTBLeave(object sender, EventArgs e) 
    { 
     SpecialTextBox s = (sender as SpecialTextBox); 


    } 
} 

私の「特別」テキストボックス。

class SpecialTextBox : Panel 
    { 
     TextBox tb = new TextBox(); 
     public string IdCode {get; set;} 
     public SpecialTextBox() 
     { 

      Controls.Add(tb); 
      BorderStyle = BorderStyle.FixedSingle; 
      Left = 30; 
     } 



     public event EventHandler SpecialTBLeave 
     { 
      add { this.tb.Click += value; } 
      remove { this.tb.Click -= value; } 
     } 
    } 

あなたのSpecialTextBoxの内側にインナーTextBoxのイベントを処理し、外部から消費される新しいイベントを提供する必要が私のメインフォーム...

BigPanel bp = new BigPanel(); 

      Controls.Add(bp); 
+0

なぜだけではなく、テキストボックスが含まれているSpecialTextBoxの代わりにテキストボックスから継承させますか? –

+0

他のものも同様に実行するので、私は単純な例を保つためにすべてのコードを削除しました。 – user3755946

答えて

2

上のコード:

class SpecialTextBox : Panel 
{ 
    TextBox tb = new TextBox(); 
    public string IdCode {get; set;} 

    // simple event, don't register to the inner TextBox! 
    public event EventHandler SpecialTBLeave; 

    public SpecialTextBox() 
    { 
     Controls.Add(tb); 
     BorderStyle = BorderStyle.FixedSingle; 
     Left = 30; 

     // register to inner TextBox' event to raise outer event 
     tb.Leave += (sender, e) => SpecialTBLeave?.Invoke(this, e); 
    } 
} 

Panelから継承されているため、電子Panel年代にはすでに代わりに新しいものを作成するのでLeaveイベントを既存:

public SpecialTextBox() 
{ 
    tb.Leave += (sender, e) => base.OnLeave(e);  
} 
+0

完璧に感謝します – user3755946

関連する問題