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?
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.
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With