简体   繁体   中英

c# Calling back from a DLL a code in the winform

I am building many winform applications where i will be using alot of vertical menus (and since there is no control already existing, i had to build them dynamicly using code based on RadioButtons), so i thought about building a separated DLL that will take care of the creation of those menus, and use this DLL everytime i need that kind of menus, this is a peace of the code that was used to create dynamicly the menu :

//Creating a menubutton for each item :
foreach (MenuItem menuItem in _allItems)
{
var menuButton = CreateMenuBottons(menuItem);
_allMenuButtons.Add(menuButton);
}

// Dynamicly create the RadioButton and turn it into a menuButton

private RadioButton CreateMenuBottons(MenuItem item)
{
var index = _allItems.IndexOf(item);
var menuButton = new RadioButton();

menuButton.Appearance = Appearance.Button;
menuButton.AutoSize = true;
menuButton.Dock = DockStyle.Fill;
...
...
...
...
menuButton.Tag = item.Value;
menuButton.UseVisualStyleBackColor = true;

menuButton.CheckedChanged += Radio_CheckedChanged; //My problem is here !!!

return menuButton;

}

and this is how i detect which button was clicked on the menu :

private void Radio_CheckedChanged(object sender, EventArgs e)
{
var selectedMenuButton = (RadioButton)sender;
if (selectedMenuButton.Checked)
{
// code that will be excuted when we
// click one menuButton (belongs to the DLL)
// How can i point back to the Exe project
// that is using this DLL !
}
}

my problem is that i will be using this DLL in multiple other projects (winforms), and the code that needs to be excecuted in each project will be diffrent from the rest, so how can i call a methode in the winform project from the DLL (without having to change the DLL and recompile each time i use it) ?

Just a delegate

You can define a delegate specifying the parameters you will give

public delegate void MenuButtonClickCallbackDelegate(TypeOfData data);
public MenuButtonClickCallbackDelegate MenuButtonClickCallback;

Then call it when you need:

private void Radio_CheckedChanged(object sender, EventArgs e)
{
var selectedMenuButton = (RadioButton)sender;
if (selectedMenuButton.Checked)
{
    if(MenuButtonClickCallback != null)
        MenuButtonClickCallback(parameter);
}
}

This is how you assign a function to MenuButtonClickCallback from the exe:

void MenuButtonClicked(TypeOfData data)
{
}
/*
.
.
.
*/
theObject.MenuButtonClickCallback = MenuButtonClicked;

Event

Or you can provide an event (if it is possible that more than one method will be called back). You have to define MenuButtonClickCallback as follow:

public event MenuButtonClickCallbackDelegate MenuButtonClickCallback;

And the assignment of a method is done like this:

void MenuButtonClicked(TypeOfData data)
{
}
void MenuButtonClickedAnotherMethod(TypeOfData data)
{
}
/*
.
.
.
*/
theObject.MenuButtonClickCallback += MenuButtonClicked;
theObject.MenuButtonClickCallback += MenuButtonClickedAnotherMethod;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM