You read the Height property of the control in the constructor in it, which means that it probably WANTS while it is displayed on the form. The fact is that the size seems to be adjusted when the control should be displayed on the form. Before this, this value is set in the control constructor that you are currently receiving.
The easiest way to solve this problem is to read the Height
value, if you are sure that the control is already created on the form, that is, you can get the open
parameter from the controlβs constructor, add a new method that initializes open
and _closedHeight
and calls it in the Load
event forms.
Something like that:
public MyUserControl() { InitializeComponent(); } public AdjustControlSize(bool open) { _openHeight = this.Height; _closedHeight = splitContainer1.SplitterDistance; Open = open; }
Then call the AdjustControlSize
method from the Load
form.
Solution with eventing mechanism
You can also use your own control events to read Height
when necessary. Thus, you do not need to change anything in the code of the Form
.
So, in this case, the code might look like this (I have not tested this yet):
public MyUserControl(bool open) { InitializeComponent(); _openHeight = this.Height; _closedHeight = splitContainer1.SplitterDistance; Open = open; this.SizeChanged += new EventHandler(MyUserControl_SizeChanged); } void CustomPictureBox_SizeChanged(object sender, EventArgs e) { _openHeight = this.Height; _closedHeight = splitContainer1.SplitterDistance; }
Thus, the control can be independently configured each time its size is changed.
source share