Carlo
Carlo

Reputation: 107

Form_closing Form_resize EventHandlers

How to write just one code block for this two events ? I would like to execute same code when user minimize, resize or close form.

Upvotes: 0

Views: 81

Answers (3)

Sudhakar Tillapudi
Sudhakar Tillapudi

Reputation: 26209

you can create a function for your common code and call it from whereever you want.

if you want to call it when the form is closed and resized write it as below:

private void Form1_Resize(object sender, EventArgs e)
            {
                myfunction();
            }


            private void Form1_FormClosing(object sender, FormClosingEventArgs e)
            {
                myfunction();
            }

      private void myfunction()
            {
                //function code here
            }

Upvotes: 0

ken2k
ken2k

Reputation: 49013

this.Closing += (sender, e) => this.DoWork();
this.Resize += (sender, e) => this.DoWork();

private void DoWork()
{
    // Your code here
}

Upvotes: 1

Thorkil Holm-Jacobsen
Thorkil Holm-Jacobsen

Reputation: 7676

Write a method for the functionality and call the method from the event handlers.

Upvotes: 2

Related Questions