0

I am working application written in C# and I want simple event handling code.

I have two forms, One form contains a simple grid which contain list of customer and one form which have some text boxes and accepts the information of customer which saves data in DB.

Now, I want to use event in this process.

When user save the data using detail form, event should be raised and it will reload all the data in grid(with recently added record)

I have this code which is written in VB.NET, its quite simple but I still unable to understand how to write C# event handling code.

VB Code

Code of Grid form

 Private Sub AddCustomer()
 AddHandler Detailform.DataAdded, AddressOf AddRow
 End sub

 Private Sub AddRow()
        dt = LoadAllCustomer()
        gc.DataSource = dt
        gv.FocusedRowHandle = OCustomerCollection.Count - 1
 End Sub

Code of customer Details form

'Declaration

Public Event DataAdded()

Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.
    SetProperties()
    ' invoke save 
    oCustomer.Save()

    RaiseEvent DataAdded()
End sub
bnil
  • 1,531
  • 6
  • 35
  • 68
  • 2
    oh...seems you want to setup a custom event in C#.. see if this helps: http://stackoverflow.com/questions/6644247/simple-custom-event if not... try to search C# custom event https://www.google.com.hk/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#sourceid=chrome-psyapi2&ie=UTF-8&q=C%23%20custom%20event – User2012384 Oct 17 '14 at 06:05

1 Answers1

0

Some sample Code:

public delegate void SavedEventHandler(/* parameters if you need any information what actually got saved */);

public event SavedEventHandler Saved;

public void OnSaved() {
    var handler = this.Saved;

    if (handler != null) {
        handler();
    }
}

Your DetailForm can than call OnSaved when it's done saving:

this.OnSaved();

Now you can register or unregister for the event:

datailForm.Saved += ReloadData();
datailForm.Saved -= ReloadData();
The-First-Tiger
  • 1,574
  • 11
  • 18
  • Thanks but which code should write in List form and which one on details form ? – bnil Oct 17 '14 at 06:26
  • List Form needs a reference to your detailForm. Than you can call detailForm.Saved += in your List Form. Everything else belongs to DetailForm. – The-First-Tiger Oct 17 '14 at 06:29