Usercontrol运行时宽度和高度

时间:2012-04-25 16:28:31

标签: c# visual-studio-2008 .net-3.5 user-controls

我正在尝试在VS2008中创建一个简单的用户控件(不是WPF),它实际上是一个SplitContainer,在Panel1中有一个按钮,当按下时,切换Panel2Collapsed属性和将控件的大小调整为Panel1的大小。

以下是控件的基本内容:

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

问题是运行时中的this.Height是控件在用户控件设计器中的值,而不是主窗体设计者在设计时设置的值。

非常感谢任何帮助。

更新

来自卢卡斯'解决方案,这种方式意味着_openHeight只设置一次,从而产生所需的效果:

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

1 个答案:

答案 0 :(得分:5)

你正在它的构造函数中读取控件的Height属性,这意味着它可能在表单中显示之前。问题是,当需要在窗体中显示控件时,大小似乎会被调整。在此之前,它是控件设计器中设置的值,您现在正在使用它。

解决问题的最简单方法是在确定控件已经在表单中绘制时读取Height值,即可以从控件的构造函数中获取open参数,添加一个初始化Open_closedHeight的新方法,并在表单的Load事件中调用它。

这样的事情:

public MyUserControl()
{
    InitializeComponent(); 
}

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

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

然后从表单的AdjustControlSize事件中调用Load方法。

具有事件处理机制的解决方案

您还可以使用控件自己的事件在适当的时候阅读Height。这样您就不必更改Form代码中的任何内容。

所以,在这种情况下,代码看起来像这样(虽然我没有测试过):

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

这样,控件每次改变尺寸时都可以自行调整。