Hi,
A varaible that holds a function is called a delegate. The delegate function can be passed as an argument and then called for example.
So if we take LDControls.MenuClicked as an example, there are several bits:
1] An event from the OS or some .Net object, in this case, when a menu item is clicked
This says that when a menu item is clicked the delegate function _MenuClickEvent will be called, this is a private static function, it will pass the object that raised the event and some event arguments (often specific to the event).
2] The event handler delegate function.
In this handler we can get the calling object from sender (in this case a menu), and we can set some parameters that can be passed back to the user about the event as properties, like which menu iten was clicked, and finally we call another delegate function (_MenuClickDelegate, if it has been set) which will call the User's event subroutine.
The e.Handled = true says that the mouse click event does not need to be passed (re-routed) to other events, this is not usually needed for most events.
3] We have to set up the user function (_MenuClickDelegate) to call. Initially this is a null delegate:
Where I define SBCallBack with a using. This is not necessary, you can just use SmallBasicCallback, but I do it this way so I can easily switch between SB and sVB:
4] Finally, if the user wants this event then they set it using an event command:
So when the user clicks a menu item, the delegate _MenuClickEvent is called, and if the user has subsribed to this event, then their Small Basic event subroutine _MenuClickDelegate (set by LDControls.MenuClicked = OnMenuClicked, where OnMenuClicked is their SB event subroutine, asigned to _MenuClickDelegate) is called and they can also get info about the event that was set in _MenuClickEvent like which menu control and item was clicked.
A varaible that holds a function is called a delegate. The delegate function can be passed as an argument and then called for example.
So if we take LDControls.MenuClicked as an example, there are several bits:
1] An event from the OS or some .Net object, in this case, when a menu item is clicked
Code:
menuItem.Click += new RoutedEventHandler(_MenuClickEvent);
This says that when a menu item is clicked the delegate function _MenuClickEvent will be called, this is a private static function, it will pass the object that raised the event and some event arguments (often specific to the event).
2] The event handler delegate function.
Code:
private static void _MenuClickEvent(Object sender, RoutedEventArgs e)
{
MenuItem _item = (MenuItem)sender;
_LastMenuContol = (string)_item.Tag;
_LastMenuItem = (string)_item.Header;
e.Handled = true;
if (null == _MenuClickDelegate) return;
_MenuClickDelegate();
}
In this handler we can get the calling object from sender (in this case a menu), and we can set some parameters that can be passed back to the user about the event as properties, like which menu iten was clicked, and finally we call another delegate function (_MenuClickDelegate, if it has been set) which will call the User's event subroutine.
The e.Handled = true says that the mouse click event does not need to be passed (re-routed) to other events, this is not usually needed for most events.
3] We have to set up the user function (_MenuClickDelegate) to call. Initially this is a null delegate:
Code:
private static SBCallback _MenuClickDelegate = null;
Where I define SBCallBack with a using. This is not necessary, you can just use SmallBasicCallback, but I do it this way so I can easily switch between SB and sVB:
Code:
using SBCallback = Microsoft.SmallBasic.Library.SmallBasicCallback;
4] Finally, if the user wants this event then they set it using an event command:
Code:
/// <summary>
/// Event when a menu item is selected.
/// </summary>
public static event SBCallback MenuClicked
{
add
{
_MenuClickDelegate = value;
}
remove
{
_MenuClickDelegate = null;
}
}
So when the user clicks a menu item, the delegate _MenuClickEvent is called, and if the user has subsribed to this event, then their Small Basic event subroutine _MenuClickDelegate (set by LDControls.MenuClicked = OnMenuClicked, where OnMenuClicked is their SB event subroutine, asigned to _MenuClickDelegate) is called and they can also get info about the event that was set in _MenuClickEvent like which menu control and item was clicked.