Sayse
Sayse

Reputation: 43330

Update form from shared event

I've got a form I've created with 4 buttons that will modify a listView, each with their own click event. I'm wondering as a way to avoid adding a method call in each of these buttons if it their is an event available for after a button has finished executing its click event?

The reason for this is so I can update the listView which will in turn update a webBrowser

Upvotes: 0

Views: 71

Answers (1)

Nathan
Nathan

Reputation: 6216

Here's an example which results in "1" and then "all" being displayed to the user when button 1 is clicked, "2" and then "all" when button 2 is clicked etc etc.

Note: you don't want to do it like this - see comments

    public Form1()
    {
        InitializeComponent();

        foreach(var b in Controls.OfType<Button>())
            b.Click += new EventHandler(AnyButton_Click);
    }

    void AnyButton_Click(object sender, EventArgs e)
    {
        MessageBox.Show("all");
    }

    private void button1_Click(object sender, EventArgs e)
    {
        MessageBox.Show("1");
    }

    private void button3_Click(object sender, EventArgs e)
    {
        MessageBox.Show("3");
    }

    private void button2_Click(object sender, EventArgs e)
    {
        MessageBox.Show("2");
    }

Upvotes: 1

Related Questions