Click here to Skip to main content
15,895,557 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a form1 ,& i wantto open the form2 by using delegate & events.

Im facing an error.
"
VB
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

1 solution

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.
 
Share this answer
 
Comments
Ali_100 21-Oct-12 13:24pm    
So How could I call the method name "methodinform" from form1?
Thomas Daniels 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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900