Note that there are some explanatory texts on larger screens.

plurals
  1. POUsercontrol runtime width and height
    primarykey
    data
    text
    <p>I am trying to create a simple User Control (not WPF) in VS2008 which effectively is a <code>SplitContainer</code> with a button in <code>Panel1</code>, which when pressed, toggles the <code>Panel2Collapsed</code> property and resizes the control to the size of <code>Panel1</code>.</p> <p>Here are the bare bones of the control:</p> <pre><code>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; } </code></pre> <p>The problem is that <code>this.Height</code> in Runtime is the value which the control is in the User Control Designer, rather than what is set at Design-time in the main form's designer.</p> <p>Any help would be greatly appreciated.</p> <p><strong>UPDATE</strong></p> <p>Following on from Lucas' solution, this way means that the _openHeight is only set once, resulting in the desired effect:</p> <pre><code>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 (e.g. 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; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload