Creating a C # event wait function

I have a class that instantiates a form with several buttons. I have a function in this class that is designed to wait for a user to click one of the buttons and return different values ​​depending on which button was pressed. I read some materials about using anonymous delegates for this, but I'm not sure how to determine which particular button was clicked. My initial approach was to create a custom event that takes the button number as a parameter, and bind an event handler to this from my class, but again I'm not sure how I could return this function when I get to the delegate .

Is there an easy way to do this?

PM

+3
source share
1 answer

Assuming WinForms, there are several approaches you could take. You can open each button as a property in your form class and have a class that creates a form for subscribing to the Click event for each button. For instance,

In the Form class:

public class MyForm : Form 
{
    // form initialization, etc, etc.

    public Button Button1 
    {
        get { return button1; }
    }
}

In the class that creates the form:

public class MyClass
{
    public Form CreateForm()
    {
        var form = new MyForm();
        form.Button1.Click += HandleButton1Clicked;
        return form;
    }

    private void HandleButton1Clicked(object sender, EventArgs e)
    {
        // do whatever you need to do when Button1 is clicked
    }
}

Alternatively, you can add the ButtonClicked event to the form and determine which button was clicked that way. The form will be subscribed to each of its buttons. Click "Events" and click the "ButtonClicked" button with the button as the sender.

I will probably go with the first one, as this will not allow the if statement to be written to determine which button was pressed.


Edited to adapt to the workflow in the comments:

, , , . , , . , , , .

public class MyForm : Form 
{
    // form initialization, etc, etc.
    private Button button1;
    private Button button2;

    public MyForm()
    {
        InitializeComponent();
        button1.Click += HandleButtonClicked;
        button1.DialogResult = DialogResult.OK;
        button2.Click += HandleButtonClicked;
        button2.DialogResult = DialogResult.OK;
    }

    private void HandleButtonClicked(object sender, EventArgs e)
    {
        ButtonClicked = sender as Button;
    }

    public Button ButtonClicked
    {
        get; private set;
    }
}

:

public class MyClass
{
    public int GetValue()
    {
        var form = new MyForm();
        if(form.ShowDialog() == DialogResult.OK) // this will block until form is closed
        {
            // return some value based on form.ButtonClicked
            // adjust method return type as necessary
        }
        else 
        {
            // do something if the user closed the form without 
            // clicking on one of the buttons
        }
    }
}

, HandleButtonClicked , , .

+4

Source: https://habr.com/ru/post/1784325/


All Articles