How call a callback directly from the code

  • Thread starter Thread starter Gianco
  • Start date Start date
G

Gianco

Hello

I'm trying to call a callback function directly from the C# code but I
don't know how to do it... I tried a lot of solutions but the C#
syntax slap me

Practically, I want test if the user hase defined a callback for a
certain control and then call it from the code

example

public void FillMyGrid(System.Windows.Forms.DataGrid Grid)
{
//
// Code to fill the grid
//

//
// If the user has defined the CurrentCellChanged event
// I would call it, this code is just how I dream about
// to do it
//
if (Grid.CurrentCellChanged != null)
{
Grid.CurrentCellChanged(Grid, new System.Eventargs());
}

//
// But unfortunately is not so...
//
}

Can anyone help me?

Thank you
Gianfranco
 
So you want to call a method on a derived class that you don't know exists?
I think you'll have to use reflection lo determine its existence and then
invoke it.

-Chris
 
If this is indeed the goal, the FAQ actually has an example:

7.29. How do I determine if a member exists at runtime?
http://msdn.microsoft.com/mobility/prodtechinfo/devtools/netcf/FAQ/default.aspx#7.29

If you are trying to assign or register a callback function then you will
want to use a delegate. Let me know if any of this helps.

--
Geoff Schwab
Program Manager
Excell Data Corporation
http://msdn.com/mobility
http://msdn.microsoft.com/mobility/prodtechinfo/devtools/netcf/FAQ/default.aspx

This posting is provided "AS IS" with no warranties, and confers no rights.
 
Hello

CurrentCellChanged is not a method, I just find, as methods,
add_CurrentCellChanged and remove_CurrentCellChanged so I tried to
find it in the events list, but nothing

Practically I have a Grid and each time the user select a new cell I
want to make some operations (enable/disable some operative buttons).
The first time my program shows the grid, I would 'enforce' the event
(if defined for the grid), but I don't know how, so I'm not managing a
derived class, I would 'fire' the event from the operative code

Any idea?

Thank you for your help, I appreciate very much

Gianco
 
Hello

I solved in this way:

MethodInfo Method = typeof(DataGrid).GetMethod("OnCurrentCellChanged",
BindingFlags.NonPublic | BindingFlags.Instance);

Method.Invoke(Grid, BindingFlags.NonPublic | BindingFlags.Public |
BindingFlags.Instance, null, new object[] {new EventArgs()},
System.Globalization.CultureInfo.CurrentCulture);

Thank you for the help, as always very precious

Gianco
 
Back
Top