Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to fire event from child form to parent form but it keeps null value. I have a delegate and event declared on child form, and method that checks if event is not null. That method is called on button click and supposed to notify parent form for some action (refreshing grid data on parent form.)

C#
//Child form
public partial class InventuraForm
{
    public delegate void NekiDelegat();
    public event NekiDelegat MojEvent;  


    public void SendEvent()
    {
        if (MojEvent != null) MojEvent();
    }

    public void tsbSpremiZatvori_Click(object sender, EventArgs e)
    {
       //some code

        SendEvent();
    }
}           


//Parent form
public partial class InventuraFormPregled 
{ 
    InventuraForm _inv = null;

  public InventuraFormPregled()
    {
        InitializeComponent();

        _inv = new InventuraForm();
        _inv.MojEvent += new InventuraForm.NekiDelegat(LoadGridData);          

    }     

 private void LoadGridData()
 {
 //some code
 }

}


Any help is appretiated.

What I have tried:

I tried to search for a similar solution, but no luck.
Posted
Updated 29-Dec-16 2:00am

1 solution

You seem to have created this as a very simplified version, which is missing a load of information. It may be that the important parts have been lost in the "condensing" process - certainly that code is not going to work as you describe on it's own!
Have a look at this: Transferring information between two forms, Part 2: Child to Parent[^] - I know that works!
 
Share this answer
 
Comments
Davor Bursac 29-Dec-16 8:09am    
Of course, this is simplified version, some code is missing, but thrust me this contains everything needed to accomplish what I am try to do, no important part are left out.
OriginalGriff 29-Dec-16 8:26am    
Except the inheritance - since neither of them are inherited; the second form is never displayed; And I have no idea what else. But *something* is: if that code is to be believed, you don't create a second form with attaching an event handler - and you are saying that no handler is ever attached!
Davor Bursac 29-Dec-16 8:43am    
I an not trying to create child form, everything is happening while child form is closing. Thing is, on parent form I have a grid with inventory list. Child form contains each inventory's items. After I am finished with editing the items, I am saving data and closing child form (public void tsbSpremiZatvori_Click() --> "Spremi" means Save and "Zatvori" means Close) and then trying to signal the parent form to refresh the data. PS. I just tried with code you pasted me, same thing goes on, EventHandler eh = null....

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