Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are WinForms Control.Bottom and Control.Right read only?

I know one can align bottom of a control by manipulating Control.Top and Control.Height. And similarly Control.Right. But why is that .NET decided to not let these properties be modified directly? Its frustrating since last few fays I have been dealing with control positioning.

like image 313
nawfal Avatar asked Mar 06 '26 08:03

nawfal


2 Answers

Short answer:

I believe they have made Bottom and Right read-only, not because it is technically necessary, but as a way of documenting that it is these properties that get re-computed when Top/Height or Left/Width are modified.

Longer answer:

There are two property sets: One for absolute coordinates (Top, Bottom, Left, Right), and one for dimension (Height, Width). The relationship between these is defined through the following two invariants:

  1. Width == Right - Left
  2. Height == Bottom - Top

Now if, for example, Microsoft decides that the Top property should be writable, they need to make one further design decision: Will a change in Top...

  • affect Height and preserve Bottom, or
  • preserve Height and affect Bottom?

Unless they introduce a further "anchor" property of some sort that allows the user to choose, Microsoft must decide on one of these consequences to guarantee invariant (2) above.

Obviously they decided on the latter of the two alternatives. Next, Microsoft would have needed to document their decision. They could either mention the consequences of adjusting Top on their MSDN reference pages... and it turns out they did just that:

Changes made to the Height and Top property values cause the Bottom property value of the control to change. — Remarks section of the MSDN reference page for the Control.Top property

... or they could declare the Bottom property as read-only, suggesting to us programmers that this property depends on the other two (Top and Height)... which they have also done:

The Bottom property is a read-only property. You can manipulate this property value by changing the value of the Top or Height properties [...] — Remarks section of the MSDN reference page for the Control.Bottom property

So they have documented their (arbitrary) design decision both in English, and additionally through code.

like image 168
stakx - no longer contributing Avatar answered Mar 08 '26 09:03

stakx - no longer contributing


To quote Eric Lippert: " You keep on asking "why?" questions, which I find difficult to answer because your question is secretly a "why not?" question. That is, the question you really mean to ask is "I have a notion of how the [feature] obviously ought to have been designed; why is it not like that?" "

The answer to "why not?" question is probably between:

  1. This "feature" (settable Bottom and Right properties) was not proposed/was not considered useful
  2. The behavior of settable Right and Bottom is ambiguous - do I re-size the control in question (changing Height/Width) or re-position (changing Top/Right). Different people will have different opinions about the "correct" behavior here.
like image 39
e71 Avatar answered Mar 08 '26 07:03

e71