Click here to Skip to main content
12,070,171 members (29,404 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: event
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
"
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:
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
        }
        
      
           
    }
}
Posted 21-Oct-12 7:33am
Ali_1001.4K

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
Comments
Ali_100 21-Oct-12 13:24pm
   
So How could I call the method name "methodinform" from form1?
ProgramFOX 22-Oct-12 11:29am
   
_nuForm.methodinForm();
Ali_100 21-Oct-12 14:57pm
   
I did thanks for the theoritical guidance

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web03 | 2.8.160208.1 | Last Updated 21 Oct 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100