gpt4 book ai didi

c# - 用户控件运行时宽度和高度

转载 作者:太空狗 更新时间:2023-10-29 21:40:49 27 4
gpt4 key购买 nike

我正在尝试在 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;
}

最佳答案

您正在它的构造函数中读取控件的 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
...

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

事件机制解决方案

您还可以在适当的时候使用控件自己的事件来读取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;
}

这样控件可以在每次更改大小时自行调整。

关于c# - 用户控件运行时宽度和高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10319852/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com