包含按钮的usercontrol可以在click事件上调用自身吗? [英] Can a usercontrol containing a button call itself on click event?

查看:117
本文介绍了包含按钮的usercontrol可以在click事件上调用自身吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

hi
我创建了一个包含按钮的用户控件。我想知道我是否可以在用户控件中点击按钮来调用此用户控件?如果有,怎么样?



谢谢,

Ananya

hi I have created a user control containing a button. I want to know if I can call this user control on button click in the user control? if yes, how?

thanks,
Ananya

推荐答案

那是不完全清楚,但我会尝试解释你的意思的各种可能性:



您可以单击UserControl中的按钮并在UserControl中引发一个事件它指的是按钮:只需按常规方式添加事件处理程序, sender 参数将包含Button实例。



您可以通过UserControl从Button Click到外部世界发出事件信号,但您必须首先在UserControl中创建一个Event,并且在引发事件时应该传递UserControl instacne而不是Button:

That's not exactly clear, but I'll try and explain the various possibilities for what you mean:

You can click the Button in the UserControl and raise an event in the UserControl which refers to the button: just add an event handler in the usual way and the sender parameter will contain the Button instance.

You can signal the event from a Button click to the outside world from the UserControl, but you have to create an Event within the UserControl first, and you should pass the UserControl instacne rather than the Button when you raise the event:
/// <summary>
/// Event to indicate UserControl Button Clicked
/// </summary>
public event EventHandler Clicked;
/// <summary>
/// Called to signal to subscribers that UserControl Button Clicked
/// </summary>
/// <param name="e"></param>
protected virtual void OnClicked(EventArgs e)
    {
    EventHandler eh = Clicked;
    if (eh != null)
        {
        eh(this, e);
        }
    }
private void button1_Click(object sender, EventArgs e)
    {
    OnClicked(e);
    }





您可以在UserControl中处理自己的事件(但为什么你想要的是另一件事)。只需在构造函数中为上面创建的事件添加处理程序:





You can handle your own events within the UserControl (but why you would want to is another thing). Just add a handler for the event you created above in your constructor:

Clicked += new EventHandler(MyUserControl_Clicked);
    ...
    }

void MyUserControl_Clicked(object sender, EventArgs e)
    {
    ...
    }



如果这不包括您要尝试的内容你需要更详细地解释一下。


If this doesn't cover what you are trying to do, you need to explain in better detail.


你好,



我不清楚要求。



根据我的理解,你有一个用户控制,你有一个控制按钮。



你需要在用户控件内单击按钮时触发事件。



Fired事件应该同时执行Code Behind任务,需要做一些事情其他活动。



如果这是您的要求,请选择代表。



创建一个匿名函数和按钮单击执行正常的代码behenind操作也会触发一个事件来执行匿名函数中的操作。



如果您需要更多详细信息,请告诉我们。







谢谢! C-sharp-event-handler-delegates [ ^ ]


这篇关于包含按钮的usercontrol可以在click事件上调用自身吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆