简体   繁体   English

用户控件事件处理程序订阅

[英]User control event handler subscription

I have a windows forms app with a user control. 我有一个带有用户控件的Windows窗体应用程序。 I'm trying to get the UC to call a function on the main form, but when I add the UC, the main form can't see the public event. 我试图让UC在主窗体上调用函数,但是当我添加UC时,主窗体看不到公共事件。

My UC looks like this: 我的UC看起来像这样:

public partial class uctlLogin: UserControl
{
    public delegate void ButtonClickedEventHandler(object sender, EventArgs e);
    public event ButtonClickedEventHandler OnUserControlButtonClicked;

    public uctlLogin()
    {
        InitializeComponent();
        btnLogin.Click += new EventHandler(OnButtonClicked);
    }

    private void OnButtonClicked(object sender, EventArgs e)
    {
        if (OnUserControlButtonClicked != null)
            OnUserControlButtonClicked(this, e);
    }
}

My form code is: 我的表单代码是:

public partial class frmMain : Form
{
    UserControl uctlLogin1 = new TabletUserControls.uctlLogin();

    public frmMain()
    {
        InitializeComponent();
        uctlLogin1.OnUserControlButtonClicked += new EventHandler(OnUCButtonClicked);
    }

    private void OnUCButtonClicked(object sender, EventArgs e)
    {
        MessageBox.Show("Horray!");
    }

    private void frmMain_Load(object sender, EventArgs e)
    {
        this.Controls.Add(uctlLogin1);

    }
}

The problem is in this line: 问题在这一行:

uctlLogin1.OnUserControlButtonClicked += new EventHandler(OnUCButtonClicked);

Generates the following message: 生成以下消息:

'System.Windows.Forms.UserControl' does not contain a definition for 'OnUserControlButtonClicked' and no extension method 'OnUserControlButtonClicked' accepting a first argument of type 'System.Windows.Forms.UserControl' could be found (are you missing a using directive or an assembly reference?) 'System.Windows.Forms.UserControl'不包含'OnUserControlButtonClicked'的定义,并且找不到扩展方法'OnUserControlButtonClicked'接受类型为'System.Windows.Forms.UserControl'的第一个参数(是否缺少using指令或装配参考?)

I've tried putting the UC in the same project and creating the UC in a separate project, then adding a reference to it. 我尝试过将UC放在同一项目中,并在单独的项目中创建UC,然后添加对它的引用。 After Googling for hours, I'm stuck. 在谷歌搜索了几个小时后,我被卡住了。 Any idea what I'm missing here? 知道我在这里缺少什么吗?

BTW, I'm using VS2012 targeting the .Net Framework 4.5. 顺便说一句,我正在使用针对.Net Framework 4.5的VS2012。

That's because in your frmMain , you declared uctlLogin1 as UserControl , rather than uctlLogin . 这是因为在您的frmMain ,您将uctlLogin1声明为UserControl ,而不是uctlLogin The type UserControl doesn't have a OnUserControlButtonClicked event, and the compiler doesn't know that uctlLogin1 is an instance of uctlLogin , since you declared it as UserControl . 该类型的UserControl不具有OnUserControlButtonClicked事件,编译器不知道uctlLogin1是一个实例uctlLogin ,因为你宣布它为UserControl Just specify the actual type of uctlLogin1 and it will work fine. 只要指定uctlLogin1的实际类型,它就可以正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM