Usercontrol runtime width and height

14,040

You are reading the control's Height property in it's constructor, which means it's probably BEFORE it shows in the form. The thing is, the size seems to be adjusted when the control needs to be show in the form. Before this, it's a value set in the control's designer, which you are now getting.

The easiest way to solve the issue is to read the Height value when you're sure that the control was already drawn in the form i.e. you can get open parameter out of the control's constructor, add a new method that initializes Open and _closedHeight and call it in Load event of the form.

Something like this:

public MyUserControl()
{
    InitializeComponent(); 
}

public AdjustControlSize(bool open)
{
    _openHeight = this.Height;
    _closedHeight = splitContainer1.SplitterDistance;
    Open = open; 
}

//the rest of the control's code is unchanged
...

Then call AdjustControlSize method from form's Load event.

Solution with eventing mechanism

You can also use control's own events to read the Height when appropriate. This way you don't have to change anything in the Form's code.

So, in this case, the code can look like this (I haven't tested this though):

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;
}

This way the control can self adjust itself each time its size is changed.

Share:
14,040
dav_i
Author by

dav_i

Multi-language developer with extensive experience in C# and Kotlin, with a test-driven mindset, and a passion for finding simple solutions to complex problems. My goal is to solve novel problems to help the world be a better place.

Updated on July 02, 2022

Comments

  • dav_i
    dav_i almost 2 years

    I am trying to create a simple User Control (not WPF) in VS2008 which effectively is a SplitContainer with a button in Panel1, which when pressed, toggles the Panel2Collapsed property and resizes the control to the size of Panel1.

    Here are the bare bones of the 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 which the control is in the User Control Designer, rather than what is set at Design-time in the main form's designer.

    Any help would be greatly appreciated.

    UPDATE

    Following on from Lucas' solution, this way means that the _openHeight is only set once, resulting in 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 (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;
    }
    
    • Lukasz M
      Lukasz M about 12 years
      Has the solution I suggested worked for you?
    • dav_i
      dav_i about 12 years
      Just tested your second solution and, with a bit of modification, it works. Is it proper practice to update your answer with my modified solution?
    • Lukasz M
      Lukasz M about 12 years
      Just make na Update in your question heading and update it with the answer you have and some description :).
    • Lukasz M
      Lukasz M about 12 years
      I saw You edited my answer, but if you want I can discard it and just let You update the question and describe your own version based on my answer :).
    • dav_i
      dav_i about 12 years
      Okay, reject away and I shall do an update on the question. :-)