委托和事件编码问题. [英] delegate and event coding issue.

查看:78
本文介绍了委托和事件编码问题.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个form1,&我想使用委托&打开Form2.事件.

我遇到错误.
"

I have a form1 ,& i wantto open the form2 by using delegate & events.

Im facing an error.
"

Error   2   Cannot assign to 'methodinForm' because it is a 'method group'  G:\Dotnet11\Project\TryDelegateMain\TryDelegateMain\Form1.cs    38  13  TryDelegateMain


"
有我的代码.

表格1:


"
there is my code.

Form1:

namespace TryDelegateMain
{
    public partial class Form1 : Form
    {

        public event NewStaffCreatedHandler NewStaffCreated;
       
       public delegate void NewStaffCreatedHandler(Form2 NewStaffMember);

        public Form1()
        {
            InitializeComponent();
        }
        protected void CreateNewStaffMember()
        {
            Form2 _newStaffMember = new Form2();
            //... Code to create the new StaffMember...
            //Raise the event
            NewStaffCreated(_newStaffMember);
        }
      
        //This is the method that the delegate points to
        void NewStaffForm_NewStaffCreated(Form2 NewStaffMember)
        {
            //RefreshStaffList();
            //NewStaffMember.AddedToList = true; //Do something with the StaffMember object that was passed with the delegate
            //Create an instance of the NewStaffForm and assign a method to the NewStaffCreatedHandler
            Form2 _nuForm = new Form2();
            _nuForm.methodinForm += new NewStaffCreatedHandler(NewStaffForm_NewStaffCreated);
            _nuForm.Show();
        }

    }

}



Form2:



Form2:

namespace TryDelegateMain
{
    public partial class Form2 : Form
    {
        public event NewStaffCreatedHandler NewStaffCreated;

        public delegate void NewStaffCreatedHandler(Form2 NewStaffMember);

        
        public Form2()
        {
            InitializeComponent();
        }

        public void methodinForm()
        {
            //do something
        }
        
      
           
    }
}

推荐答案

methodinForm是一种方法,而您尝试做的是向方法中添加事件处理程序.那是不可能的.您可以向事件添加事件处理程序,但是methodinForm不是事件,而是方法.
methodinForm is a method, and what you try to do is to add a event handler to a method. And that''s not possible. You can add event handlers to events, but methodinForm is not a event, but a method.


这篇关于委托和事件编码问题.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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