【发布时间】:2012-05-06 09:14:13
【问题描述】:
我正在尝试在 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 是控件在用户控件设计器中的值,而不是在主窗体设计器中的设计时设置的值。
任何帮助将不胜感激。
更新
从 Lucas 的解决方案开始,这种方式意味着 _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;
}
【问题讨论】:
-
我建议的解决方案对您有用吗?
-
刚刚测试了您的第二个解决方案,稍作修改,它就可以工作了。用我修改后的解决方案更新您的答案是否正确?
-
只需在您的问题标题中进行 Update 并使用您的答案和一些描述进行更新:)。
-
我看到你编辑了我的答案,但如果你愿意,我可以放弃它,让你更新问题并根据我的答案描述你自己的版本:)。
-
好的,拒绝,我会更新这个问题。 :-)
标签: c# visual-studio-2008 .net-3.5 user-controls