VS2008で有効なのボタンを持つ簡単なユーザーコントロール(WPFではなく)を作成しようとしています。プロパティを切り替えてコントロールをサイズ変更するPanel1
Panel1
。問題は、ランタイムでthis.Height
ではなく、設計時に設定されているものよりも、コントロールがユーザーコントロールデザイナーである値であることであるユーザーコントロールの実行時の幅と高さ
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;
}
:ここ
は、コントロールの裸の骨ですメインフォームのデザイナー。
ご協力いただければ幸いです。ルーカスのソリューションに続き
UPDATE
、この方法は_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;
}
私が提案したソリューションはあなたのために働いていますか? –
ちょうどあなたの2番目の解決策をテストし、少し修正するだけで動作します。修正されたソリューションで回答を更新するのが適切な方法ですか? –
あなたの質問の見出しにna ** Update **を作成し、あなたの答えといくつかの説明で更新してください:)。 –