Ok so I have built a custom control that handles wysiwyg editing. Essentially on 1 control I have multiple instances of my wysiwyg editor. So one might be for editing, let’s say, a recipe. Another might be for notes for that recipe.
I have a button on my wysiwyg editor that uses an interface to do callbacks to the control that holds them both, so I know on my parent control when the button was clicked. How do I find out which control fired the callback?
Example
Main Form
public partial class MyCustomControl: RecipeControl.ISavedButton {
private void SaveButton_Clicked(){
//Do Work but how do I find out which control fired this event?
//Was it RecipeControl1 or RecipeControl2
}
}
My Solution
On my recipe control I did this.
private void RecipeSaveButton_Clicked(object sender, EventArgs e){
if (RecipeSaveButtonListener != null) {
RecipeSaveButtonListener.RecipeSaveButton_Clicked(this, EventArgs.Empty); //This referring to the control, not the button.
}
}
On my main control I did this.
private void RecipeSaveButton_Clicked(object sender, EventArgs e){
if (sender == RecipeControl1){
} else if (sender == RecipeControl2) {
}
}
I have implemented both answers and both are very very good. Sorry I can’t accept both of them.
Most event handlers (like a button click) are built with a standard interface to tell you who performed the action. Take a modified version of your “event handler” for the
RecipeControl'sButton Click event:So when a button is clicked in your
RecipeControl, it should fire an event something like: