Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between Form.Move and Form.LocationChanged?

If you create a standard C# WinForms application, you fill find that a form has two events: Move and LocationChanged.

Move is raised when the form moves and LocationChanged is raised when the form location property changes.

Surely if the form moves, the location property will change, too?

What is the difference between the two events? In which case will one fire and not the other?

like image 857
pookie Avatar asked Jul 14 '18 21:07

pookie


2 Answers

Both Move and LocationChanged events are interconnected. I believe there is no situation when one if fired and the other is not. The difference is that they belong to different categories of events.

The Move event has [SRCategoryAttribute("CatLayout")] attribute.

The LocationChanged event has [SRCategoryAttribute("CatPropertyChanged")] attribute.

like image 188
L_J Avatar answered Oct 21 '22 22:10

L_J


The Move and LocationChanged events are declared on the Control class, which is then inherited by ScrollableControl, ContainerControl and finally Form.

According to the source code, OnLocationChanged calls OnMove before it invokes the LocationChanged event handler. So, the OnMove event will be raised first and then LocationChanged. You could in theory handle both events knowing that Move will be occur first.

If you look through the source you'll see that LocationChanged is raised when the bounds change (or similar events). You'll also notice that the only thing which actually invokes OnMove is in fact OnLocationChanged.

According to MSDN, the LocationChanged event:

Occurs when the Location property value has changed.... This event is raised if the Location property is changed by either a programmatic modification or through interaction.

It makes no such distinction for OnMove, where it merely states:

Occurs when the control is moved.

Which is curious since the two events are tied to each other.

This is however how one specific class handles these events. I did a bit of searching through the reference source and I couldn't find anything (inheriting from Control) which explicitly called OnMove other than the instance I've already cited. That doesn't mean they don't exist or that one couldn't invoke it separately in their own subclass of Control.

like image 35
pinkfloydx33 Avatar answered Oct 21 '22 22:10

pinkfloydx33