Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Usercontrol runtime width and height

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;
}
like image 426
dav_i Avatar asked Apr 25 '12 16:04

dav_i


1 Answers

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.

like image 200
Lukasz M Avatar answered Sep 19 '22 12:09

Lukasz M