I'm CaLendarW Blog

Working with Dock

by on Feb.04, 2009, under coding snippet

近半年開始著重地寫 Windows Application (以前以 web 同 DLL 為主), 寫左咁耐都無用過 Control.Dock 呢個屬性, 今日第一次用, 就出左好多次序上既問題, 不過在網上找到以下既解決方案, 當解決了次序既問題後, Dock 其實係幾好用!!~~

private void RemoveBtn(Button btn1)
{
	if(this.Controls.Contains(btn1))
		this.Controls.Remove(btn1);
}

private void SetDockProperity(Button btn1, DockStyle dockStyle)
{
	btn1.Dock = dockStyle;
}

private void DockTop_Click(object sender, System.EventArgs e)
{
	RemoveBtn(this.button1);
	RemoveBtn(this.button2);
	RemoveBtn(this.button3);
	SetDockProperity(this.button1, System.Windows.Forms.DockStyle.Top);
	SetDockProperity(this.button2, System.Windows.Forms.DockStyle.Top);
	SetDockProperity(this.button3, System.Windows.Forms.DockStyle.Top);
	this.Controls.Add(this.button3); // 注意順序, 後 Add 的最上面
	this.Controls.Add(this.button2);
	this.Controls.Add(this.button1);
}

private void DockBottom_Click(object sender, System.EventArgs e)
{
	RemoveBtn(this.button1);
	RemoveBtn(this.button2);
	RemoveBtn(this.button3);
	SetDockProperity(this.button1, System.Windows.Forms.DockStyle.Bottom);
	SetDockProperity(this.button2, System.Windows.Forms.DockStyle.Bottom);
	SetDockProperity(this.button3, System.Windows.Forms.DockStyle.Bottom);
	this.Controls.Add(this.button3);  // 注意順序, 後 Add 的最下面
	this.Controls.Add(this.button2);
	this.Controls.Add(this.button1);
}

轉載自: 藍色小鋪

:

Leave a Reply

You must be logged in to post a comment.

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!