我想要獲取點擊事件Button
,該事件位於我的UserControl
中,並且我在表單中動態添加UserControl
。我希望事件在Form
中提出,我在其中添加UserControl
。如果任何人都可以建議我適當的方式,那麼它會非常有幫助。從UserControl的動態添加按鈕中獲取事件
0
A
回答
0
將您自己的活動添加到您的自定義用戶控件中。
在你的客戶用戶控件裏面,一旦你添加了按鈕,你還可以附加你的(內部)事件處理程序,然後用某種方式告訴事件處理程序你的(公開的)事件處理程序,可能需要你自己的代表)。
完成後,表單可以添加自己的事件處理程序,就像添加標準控件一樣。
重讀你的問題,這可能不是確切的結構(該按鈕是固定的,而是動態地添加用戶控件?)。無論如何,它應該幾乎相同,只是在創建時添加事件處理程序的地方不同。
有了一個靜態的按鈕,它是一個更容易做 - 假設你使用正在Windows窗體:
在您的自定義用戶控件:
public event EventHandler ButtonClicked; // this could be named differently obviously
...
public void Button_OnClick(object sender, EventArgs e) // this is the standard "on button click" event handler created using the form editor
{
if (ButtonClicked != null)
ButtonClicked(this, EventArgs.Empty);
}
在您的形式:
// create a new user control and add the event
MyControl ctl = new MyControl();
Controls.Add(ctl);
ctl.ButtonClicked += new EventHandler(Form_OnUserControlButtonClicked); // name of the event handler in your form that's called once you click the button
...
private void Form_OnUserControlbuttonClicked(object sender EventArgs e)
{
// do whatever should happen once you click the button
}
0
當你是加入你的
usercontrol
您form
,註冊click事件(如果是public
)usercontrol.button.Click += new EventHandler(usercontrolButton_Click);
內的
usercontrol
+1
這假設你的按鈕控制是公開的... –
2
註冊按鈕的Click
事件中,我猜你正在使用的WinForms指你的標題。
你可以做什麼來轉發你的Click
事件。
所以在您的用戶控件
public class MyUserControl
{
public event EventHandler MyClick;
private void OnMyClick()
{
if (this.MyClick != null)
this.MyClick(this, EventArgs.Empty);
}
public MyUserControl()
{
this.Click += (sender, e) => this.OnMyClick();
}
}
3
的構造函數你需要在你的用戶控件暴露事件,然後訂閱它,當您添加用戶控件到窗體。例如: -
public partial MyUserControl:Control
{
public event EventHandler ButtonClicked;
private void myButtonClick(object sender, EventArgs e)
{
if (this.ButtonClicked != null)
this.ButtonClicked(this, EventArgs.Empty);
}
}
public partial MyForm:Form
{
private void MethodWhereYouAddTheUserControl()
{
var myUC = new MyUserControl();
myUC += myUC_ButtonClicked;
// code where you add myUC to the form...
}
void myUC_ButtonClicked(object sender, EventArgs e)
{
// called when the button is clicked
}
}
相關問題
- 1. 如何獲取動態添加的html按鈕上的事件
- 2. 添加事件動態創建按鈕
- 3. 添加鏈接按鈕事件動態
- 4. js從動態模式中的按鈕獲取事件
- 5. 從滾動條按鈕獲取事件?
- 6. 將onclick事件添加到動態添加的單選按鈕
- 7. 如何爲動態添加的按鈕添加點擊事件
- 8. 添加事件動態添加的按鈕
- 9. 從動態添加的按鈕觸發事件
- 10. 從動態添加的按鈕獲取buttonbackground圖像
- 11. 從外部UserControl中的WPF按鈕捕獲事件
- 12. 如何添加onclick事件動態添加按鈕
- 13. 添加onclick事件來動態添加按鈕?
- 14. 如何將動態添加事件添加到動態創建的按鈕
- 15. 從UserControl的Parent調用UserControl的listview按鈕點擊事件
- 16. 識別觸發事件的按鈕(在as3中動態添加按鈕)?
- 17. LinearLayout中的動態添加按鈕,按鈕點擊事件問題
- 18. WPF - 從按鈕事件添加控件
- 19. 動態添加UserControl到日曆控件的DayRender事件
- 20. 添加按鈕在EXTjs中動態點擊事件4
- 21. 如何從listView動態添加按鈕?
- 22. 動態添加帶有onclick事件按鈕的表格行
- 23. VBA:幾個動態添加按鈕的事件
- 24. AngularJS事件在彈出的動態添加按鈕
- 25. 將事件監聽器添加到動態創建的按鈕
- 26. 動態添加的鏈接按鈕不會觸發事件
- 27. 上動態添加按鈕註冊單擊事件的jQuery
- 28. 將onClick事件添加到動態生成的按鈕?
- 29. C#動態添加按鈕,並與發送事件的args它
- 30. 事件無法工作在動態地添加按鈕的HTML
哎馬里奧日Thnx爲replyn你長了什麼,我究竟want.My按鈕固定在我的usercontrol.Only我加入用戶控件dynamically.please你能給我什麼ü解釋了一些例子。 – Deepashri
擴展了答案。沒有對它進行測試,所以可能會出現一些小錯誤,但應該在大方向上提示你。 – Mario
嘿馬里奧我試着實現你的建議,但我得到ButtonClicked爲null在這一行 - 如果(ButtonClicked!= null) ButtonClicked(this,EventArgs.Empty);因此可能是因爲這種形式的事件沒有被調用。有什麼我失蹤....? – Deepashri