Width and height of working hours Usercontrol

I am trying to create a simple user control (not WPF) in VS2008, which is actually a SplitContainer with a button in Panel1, which when clicked switches the Panel2Collapsed property and resizes the control to the size of Panel1 .

Here is the bare bone control:

 private int _openHeight; private int _closedHeight; public MyUserControl(bool open) { InitializeComponent(); _openHeight = this.Height; _closedHeight = splitContainer1.SplitterDistance; Open = open; } private bool _open; private bool Open { get { return _open; } set { _open = value; splitContainer1.Panel2Collapsed = !_open; this.Height = _open ? _openHeight : _closedHeight; } } private void button1_Click(object sender, EventArgs e) { Open = !Open; } 

The problem is that this.Height in Runtime is the value that the control is in the User Control Designer, and not what is set in Design-time in the main form designer.

Any help would be greatly appreciated.

UPDATE

Following Lucas solution, this method means that _openHeight is installed only once, which leads to the desired effect:

 private int? _openHeight; private int _closedHeight; public MyUserControl(bool open) { InitializeComponent(); //the _closedHeight doesn't change so can be defined in the constructor _closedHeight = splitContainer1.SplitterDistance; //set value Open = open; this.SizeChanged += new EventHandler(MyUserControl_SizeChanged); this.Load += new EventHandler(MyUserControl_Load); } void MyUserControl_SizeChanged(object sender, EventArgs e) { //this event is called BEFORE the _Load event so gets the height set in the designer // and not any changes at run time (eg when you collapse the control) if (_openHeight == null) _openHeight = this.Height; } private bool _open; private bool Open { get { return _open; } set { _open = value; if (_open) { //sets height only if it has been initialized if (_openHeight != null) this.Height = (int)_openHeight; } else { this.Height = (int)_closedHeight; } } } void MyUserControl_Load(object sender, EventArgs e) { //now that control is loaded, set height Open = Open; } private void button1_Click(object sender, EventArgs e) { Open = !Open; } 
+6
source share
1 answer

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; } //the rest of the control code is unchanged ... 

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.

+5
source

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


All Articles