Click here to Skip to main content
14,713,689 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
On a button click, I want to call a delegate function.

it shows an error
Cannot assign to 'AddButtonClickDelegateCl' because it is a 'method group' 


is this how delegate use?

on the button click, I want to execute a method on another class.

What I have tried:

class A
public void btn_click(string name)
        {
            AddButtonClickDelegateCl(name);
        }

    public delegate void OrderButtonClick(string name);
    public event OrderButtonClick AddButtonClickDelegateCl; 


class B
     AddButtonClickDelegateCl+= AddButtonClick;

AddButtonClick()
{
//messagebox.show("success")
}
Posted
Updated 7hrs 20mins ago
v3
Comments
Maciej Los 11hrs ago
   
And... What's wrong with your code? What' your issue?

your question is not clear to me, perhaps this might help


public interface IView1
{
    event EventHandler DoStuffOnButton1Click;
    void ShowMessage(string message);
}

public partial class Form1 : Form, IView1
{
    public event EventHandler DoStuffOnButton1Click;

    public Form1()
    {
        InitializeComponent();
        button1.Click += OnButton1Click;
        var presenter = new Form1Presenter(this);
    }

    public void ShowMessage(string message)
    {
        MessageBox.Show(message);
    }

    private void OnButton1Click(object sender, EventArgs e)
    {
        var handler = this.DoStuffOnButton1Click;
        handler?.Invoke(this, EventArgs.Empty);
    }

}


public class Form1Presenter
{
    IView1 _view;
    public Form1Presenter(IView1 view)
    {
        _view = view;
        WireUpEvents();
    }

    private void WireUpEvents()
    {
        _view.DoStuffOnButton1Click += OnDoStuff;
    }

    private void OnDoStuff(object sender, EventArgs e)
    {
        _view.ShowMessage("Button1 was clicked");
    }
}
   
   
For you to add a delegate instance to ClassA from ClassB requires the instance of ClassB to have a reference to an instance of ClassA.

And, the signatures (parameters) must march.

A typical example:
public FormA AInstance;
public FormB BInstance;

private void MainForm_Load(object sender, EventArgs e)
{
    AInstance = new FormA();
    BInstance = new FormB();
}

public  class FormA: Form
{
    public Action<string> OnButtonClick;

    private void btn_click(object sender, EventArgs e)
    {
        if  (OnButtonClick != null) OnButtonClick("some text");
    }
}

public class FormB: Form
{
    public void SetClassAClickHandler(FormA ainstance)
    {
        ainstance.OnButtonClick += OnButtonClick;
    }

    private void OnButtonClick(string text)
    {
        // do something
    }
}
Npte: the use of 'Action in ClassA is a recent C# language alternative syntax for writing a delegate that takes arguments, but does not return a value: [^]
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900
亚洲日韩欧美国产专区_一本二本三本道不卡日本_在线不卡日本v二区三区18