2010-02-02 27 views
21

Kullanıcı Denetimi özel olayları vermenin ve etkinliği kullanıcı denetimi içindeki bir olayda çağırmanın bir yolu var mı. (Çağırmak doğru terim olup olmadığından emin değilim)Winforms kullanıcı denetimleri özel olayları

public partial class Sample: UserControl 
{ 
    public Sample() 
    { 
     InitializeComponent(); 
    } 


    private void TextBox_Validated(object sender, EventArgs e) 
    { 
     // invoke UserControl event here 
    } 
} 

Ve MainForm:

public partial class Sample: UserControl 
{ 
    public event EventHandler TextboxValidated; 

    public Sample() 
    { 
     InitializeComponent(); 
    } 


    private void TextBox_Validated(object sender, EventArgs e) 
    { 
     // invoke UserControl event here 
     if (this.TextboxValidated != null) this.TextboxValidated(sender, e); 
    } 
} 

Ve sonra:

public partial class MainForm : Form 
{ 
    private Sample sampleUserControl = new Sample(); 

    public MainForm() 
    { 
     this.InitializeComponent(); 
     sampleUserControl.Click += new EventHandler(this.CustomEvent_Handler); 
    } 
    private void CustomEvent_Handler(object sender, EventArgs e) 
    { 
     // do stuff 
    } 
} 
+0

Sen http://stackoverflow.com/questions/2151049/net-custom-event-organization-assistance –

cevap

29

e Steve'in yayınladığı xample, etkinliği kolayca iletebilen sözdizimi de vardır. Bu bir mülk oluşturarak benzer:

class MyUserControl : UserControl 
{ 
    public event EventHandler TextBoxValidated 
    { 
     add { textBox1.Validated += value; } 
     remove { textBox1.Validated -= value; } 
    } 
} 
27

Ben ne istediğini böyle bir şey olduğuna inanıyoruz formunuzda:

public partial class MainForm : Form 
{ 
    private Sample sampleUserControl = new Sample(); 

    public MainForm() 
    { 
     this.InitializeComponent(); 
     sampleUserControl.TextboxValidated += new EventHandler(this.CustomEvent_Handler); 
    } 
    private void CustomEvent_Handler(object sender, EventArgs e) 
    { 
     // do stuff 
    } 
} 
+0

1 faydalı yararlı bu soruya bu ilk cevabı bulabilir Cevap – Kevin

+0

Başar. Hiçbir hile ile hemen hile yaptı. :) – Almo

İlgili konular