Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2008/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Usercontrol运行时宽度和高度_C#_Visual Studio 2008_.net 3.5_User Controls - Fatal编程技术网

C# Usercontrol运行时宽度和高度

C# Usercontrol运行时宽度和高度,c#,visual-studio-2008,.net-3.5,user-controls,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 =

我试图在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属性,这意味着它可能在窗体中显示之前。问题是,当控件需要在表单中显示时,大小似乎会被调整。在此之前,它是控件设计器中设置的一个值,您现在可以得到它

解决此问题的最简单方法是,当您确定控件已按表单绘制时,读取
高度
值,即您可以从控件的构造函数中获取
打开
参数,添加一个新方法,用于初始化
Open
\u 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
方法

具有事件机制的解决方案

您还可以在适当的时候使用控件自己的事件来读取高度。这样,您就不必更改
表单
的代码中的任何内容

因此,在本例中,代码可以如下所示(尽管我还没有测试过):


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

我建议的解决方案对您有效吗?刚刚测试了您的第二个解决方案,稍加修改后,它就可以工作了。用我修改过的答案更新你的答案是否合适?只需在你的问题标题中进行na更新,并用你的答案和一些描述进行更新:)。我看到你编辑了我的答案,但如果你愿意,我可以放弃它,让你更新问题并根据我的答案描述你自己的版本:)。好的,拒绝,我将对问题进行更新。:-)
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;
}