What is the difference between SetBounds and SetBoundsCore

In WinForms, you can set the bounds of a control using SetBounds , or if you created a custom control, you can override SetBoundsCore . I was wondering if they both do the same or are there any differences?

+4
source share
3 answers

SetBounds not virtual in the first place, so if you need to run additional code when boundaries are set, your only options are to override SetBoundsCore .

SetBounds does some parameter checking and contains logic regarding the call to SetBoundsCore with the correct size based on the BoundsSpecified parameter. Furthermore, it should not be called SetBoundsCore , if the size is virtually unchanged.

In other words, SetBoundsCore is the meat of implementation, and SetBounds is a public entry point that contains validation and other housekeeping logic.

+3
source

SetBounds () is a public method that changes the location and size of a control. It calls the protected virtual SetBoundsCore () method under the hood.

This means that if you override SetBoundsCore() , you can change its algorithm (for example, add restrictions on location and size), and these changes will be applied even if some code differs from your own SetBounds() calls.

+3
source

I assume the user calls SetBounds , which translates to SetBoundsCore to implement the actual behavior. SetBoundsCore should never be called by your code.

0
source

Source: https://habr.com/ru/post/1389888/


All Articles