Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Events
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.3K

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 at 21-Oct-12 13:24pm
   
So How could I call the method name "methodinform" from form1?
ProgramFOX at 22-Oct-12 11:29am
   
_nuForm.methodinForm();
Ali_100 at 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
0 OriginalGriff 505
1 Maciej Los 325
2 Richard MacCutchan 265
3 Mathew Soji 220
4 BillWoodruff 210
0 OriginalGriff 8,804
1 Sergey Alexandrovich Kryukov 7,457
2 DamithSL 5,689
3 Maciej Los 5,279
4 Manas Bhardwaj 4,986


Advertise | Privacy | Mobile
Web04 | 2.8.1411028.1 | Last Updated 21 Oct 2012
Copyright © CodeProject, 1999-2014
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