Share via

How to write code without lambda?

nean 40 Reputation points
2023-05-07T13:43:08.99+00:00

How to write code without lambda? 'this' is a Window in WPF.

            this.Closing += (_sender, _e) =>
            {
                this.Top = 0;
            };
Developer technologies | Windows Presentation Foundation
0 comments No comments

Answer accepted by question author

Bruce (SqlWork.com) 84,086 Reputation points
2023-05-07T15:59:08.13+00:00

define an event handling method on the window class (this)

void OnClosing(object sender, CancelEventArgs e)
{
     this.Top = 0;
}

Then register event handler

this.Closing += OnClosing;

Was this answer helpful?

1 person found this answer helpful.
0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Hui Liu-MSFT 48,711 Reputation points Microsoft External Staff
    2023-05-11T03:47:41.1966667+00:00

    Hi,@nean. Welcome Microsoft Q&A. As Bruce (SqlWork.com) said, you could define a new method called OnWindowClosing that takes two parameters, a sender and a CancelEventArgs object. Then attach the method to the window's Closing event using the += operator. When the window is closed, the OnWindowClosing method is called and the window's Top property is set to 0.

    It also needs to import the System.ComponentModel namespace to use the CancelEventArgs class.

    this.Closing += OnWindowClosing;
    
    private void OnWindowClosing(object sender, CancelEventArgs e)
    {
        this.Top = 0;
    }
    
    

    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Was this answer helpful?

    1 person found this answer helpful.
    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.