D
D
Dmitry2015-12-23 12:12:21
C++ / C#
Dmitry, 2015-12-23 12:12:21

How to add custom event on button click?

Good afternoon, I read a lesson on adding arguments to an event. But in the example there was an event call when the program was executed.

//addAgent.OnAddAgent += AddAgent2;

I can’t figure out how I can trigger this event when the button is clicked, I tried to call AddAgent2 in the AddAgent1 function, but there is no execution.
It is necessary that when clicked, the AddAgent2 function works
public class AddAgentEventArgs : EventArgs
        {
            public string AddAgentInfo { get; set; }
        }

public delegate void AddAgentE(object sender, AddAgentEventArgs e);

public class AddAgent
        {
            public event AddAgentE OnAddAgent;

            public void AddAgentMain(string test)
            {
                if (OnAddAgent != null)
                {
                    // Создаём объект аргумента события и помещаем в него текст письма
                    var e = new AddAgentEventArgs { AddAgentInfo = test };
                    OnAddAgent(this, e);
                }
            }
        }

AddAgent addAgent = new AddAgent();

private void newAgent()
        {
            Button ButtonAddAgent = new Button();
            ButtonAddAgent.Text = "Добавить агента";
            ButtonAddAgent.AutoSize = true;
            ButtonAddAgent.Location = new System.Drawing.Point(290, 200);
            
            //addAgent.OnAddAgent += AddAgent2;
            addAgent.AddAgentMain(TextBoxSurName.Text);
            ButtonAddAgent.Click += new System.EventHandler(AddAgent1);
            splitContainer1.Panel2.Controls.Add(ButtonAddAgent);

        }

        private void AddAgent1 (object sender, EventArgs e)
        {
            addAgent.OnAddAgent += AddAgent2;
            MessageBox.Show("test1");
        }

        private void AddAgent2(object sender, AddAgentEventArgs e)
        {
            Console.WriteLine("test2");
            MessageBox.Show(e.AddAgentInfo);
            
        }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Mozhaykin, 2015-12-23
@smozhaykin

In AddAgent1 you didn't call AddAgent2, you only subscribed to the OnAddAgent event.
What's going on with you:

addAgent.AddAgentMain(TextBoxSurName.Text);
ButtonAddAgent.Click += new System.EventHandler(AddAgent1);

Here you first call the OnAddAgent event and only then subscribe to the button's Click event. That is, by the time your button handler fires, the OnAddAgent event has already been called, and since no one was subscribed to it, nothing happened.
It's actually hard to tell from this code what you want to do, but if you want to call OnAddAgent on a button click, you can use this code:
private void newAgent()
{
    Button ButtonAddAgent = new Button();
    ButtonAddAgent.Text = "Добавить агента";
    ButtonAddAgent.AutoSize = true;
    ButtonAddAgent.Location = new System.Drawing.Point(290, 200);
            
    ButtonAddAgent.Click += new System.EventHandler(AddAgent1);
    addAgent.OnAddAgent += AddAgent2;
    splitContainer1.Panel2.Controls.Add(ButtonAddAgent);
}

private void AddAgent1 (object sender, EventArgs e)
{
    MessageBox.Show("Before firing OnAddAgent event");
    addAgent.AddAgentMain(TextBoxSurName.Text);
}

private void AddAgent2(object sender, AddAgentEventArgs e)
{
    Console.WriteLine("Inside OnAddAgent event handler");
    MessageBox.Show(e.AddAgentInfo);
}

And advice on how to properly call events:
public void AddAgentMain(string test)
{
    var handler = OnAddAgent;
    if (handler != null)
    {
        // Создаём объект аргумента события и помещаем в него текст письма
        var e = new AddAgentEventArgs { AddAgentInfo = test };
        handler(this, e);
    }
}

Or using C#6 features:
public void AddAgentMain(string test)
{
    // Создаём объект аргумента события и помещаем в него текст письма
    var e = new AddAgentEventArgs { AddAgentInfo = test };
    OnAddAgent?.Invoke(this, e);
}

You can read more about why it is worth calling events this way here .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question