在主窗體中處理 UserControl 事件
在使用者介面設計中,通常會為特定功能建立自訂使用者控制項。但是,有時需要在主窗體層級處理使用者控制項內的事件。
要實現此目的,請為使用者控制項建立事件處理程序,該事件處理程序可以在觸發控制項內的事件時引發。這允許事件在鏈上冒泡,使您能夠在表單層級處理它。
範例:
考慮一個帶有數字向上向下的自訂使用者控制項(NUD)控制。當 NUD 的值變更時,您希望主視窗更新顯示視窗。
使用者控制項:
[Browsable(true)] [Category("Action")] [Description("Invoked when user clicks button")] public event EventHandler ButtonClick; protected void Button1_Click(object sender, EventArgs e) { //bubble the event up to the parent this.ButtonClick?.Invoke(this, e); }
Main形式:
UserControl1.ButtonClick += new EventHandler(UserControl_ButtonClick); protected void UserControl_ButtonClick(object sender, EventArgs e) { //handle the event }
註釋:
以上是如何處理主窗體中的使用者控制事件?的詳細內容。更多資訊請關注PHP中文網其他相關文章!