先上张效果图,全部展开图和全部折叠图,时间仓促,功能实现比较简单,代码也比较冗余,待后续完善再更新
控件:一个Panel,四个Button,四个TableLayoutPanel,九个Label
代码如下:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Dictionary<string, bool> dic = new Dictionary<string, bool>();
private void Form1_Load(object sender, EventArgs e)
{
dic.Add("StockManage", false);
dic.Add("StockOutManage", false);
dic.Add("InventoryManagement", false);
dic.Add("AllocationManagement", false);
StockManage.Visible = false;
StockOutManage.Visible = false;
InventoryManagement.Visible = false;
AllocationManagement.Visible = false;
btn_StockOutManage.Top = btn_StockManage.Bottom;
btn_InventoryManagement.Top = btn_StockOutManage.Bottom;
btn_AllocationManagement.Top = btn_InventoryManagement.Bottom;
}
private void StockManage_VisibleChanged(object sender, EventArgs e)
{
TableLayoutPanel tlp = sender as TableLayoutPanel;
string name = tlp.Name;
switch (name)
{
case "StockManage":
if (tlp.Visible)
btn_StockOutManage.Top = tlp.Bottom;
else
btn_StockOutManage.Top = btn_StockManage.Bottom;
if (StockOutManage.Visible)
{
StockOutManage.Top = btn_StockOutManage.Bottom;
btn_InventoryManagement.Top = StockOutManage.Bottom;
if (InventoryManagement.Visible)
{
InventoryManagement.Top = btn_InventoryManagement.Bottom;
btn_AllocationManagement.Top = InventoryManagement.Bottom;
if (AllocationManagement.Visible)
{
AllocationManagement.Top = btn_AllocationManagement.Bottom;
}
}
else
{
btn_AllocationManagement.Top = btn_InventoryManagement.Bottom;
if (AllocationManagement.Visible)
{
AllocationManagement.Top = btn_AllocationManagement.Bottom;
}
}
}
else
{
btn_InventoryManagement.Top = btn_StockOutManage.Bottom;
if (InventoryManagement.Visible)
{
InventoryManagement.Top = btn_InventoryManagement.Bottom;
btn_AllocationManagement.Top = InventoryManagement.Bottom;
if (AllocationManagement.Visible)
{
AllocationManagement.Top = btn_AllocationManagement.Bottom;
}
}
else
{
btn_AllocationManagement.Top = btn_InventoryManagement.Bottom;
if (AllocationManagement.Visible)
{
AllocationManagement.Top = btn_AllocationManagement.Bottom;
}
}
}
break;
case "StockOutManage":
if (tlp.Visible)
{
if (StockManage.Visible)
btn_StockOutManage.Top = StockManage.Bottom;
tlp.Top = btn_StockOutManage.Bottom;
btn_InventoryManagement.Top = tlp.Bottom;
}
else
{
btn_InventoryManagement.Top = btn_StockOutManage.Bottom;
}
if (InventoryManagement.Visible)
{
InventoryManagement.Top = btn_InventoryManagement.Bottom;
btn_AllocationManagement.Top = InventoryManagement.Bottom;
if (AllocationManagement.Visible)
{
AllocationManagement.Top = btn_AllocationManagement.Bottom;
}
}
else
{
btn_AllocationManagement.Top = btn_InventoryManagement.Bottom;
if (AllocationManagement.Visible)
{
AllocationManagement.Top = btn_AllocationManagement.Bottom;
}
}
break;
case "InventoryManagement":
if (tlp.Visible)
{
if (StockOutManage.Visible)
btn_InventoryManagement.Top = StockOutManage.Bottom;
tlp.Top = btn_InventoryManagement.Bottom;
btn_AllocationManagement.Top = tlp.Bottom;
}
else
{
btn_AllocationManagement.Top = btn_InventoryManagement.Bottom;
}
if (InventoryManagement.Visible)
{
InventoryManagement.Top = btn_InventoryManagement.Bottom;
btn_AllocationManagement.Top = InventoryManagement.Bottom;
if (AllocationManagement.Visible)
{
AllocationManagement.Top = btn_AllocationManagement.Bottom;
}
}
else
{
btn_AllocationManagement.Top = btn_InventoryManagement.Bottom;
if (AllocationManagement.Visible)
{
AllocationManagement.Top = btn_AllocationManagement.Bottom;
}
}
break;
case "AllocationManagement":
if (InventoryManagement.Visible)
btn_AllocationManagement.Top = InventoryManagement.Bottom;
else
btn_AllocationManagement.Top = btn_InventoryManagement.Bottom;
if (tlp.Visible)
tlp.Top = btn_AllocationManagement.Bottom;
break;
}
}
private void btn_Manage_Click(object sender, EventArgs e)
{
Button btn = sender as Button;
string name = btn.Name;
int index = name.IndexOf('_');
name = name.Substring(index + 1);
dic[name] = dic[name] ? false : true;
StockManage.Visible = dic["StockManage"];
StockOutManage.Visible = dic["StockOutManage"];
InventoryManagement.Visible = dic["InventoryManagement"];
AllocationManagement.Visible = dic["AllocationManagement"];
}
}
修改后代码:
public Form1()
{
InitializeComponent();
}
Dictionary<string, bool> dic = new Dictionary<string, bool>()
{
{ "StockManage", false } ,
{ "StockOutManage", false } ,
{ "InventoryManagement", false } ,
{ "AllocationManagement", false }
};
private string[] btnNames = new string[] { "btn_StockManage", "btn_StockOutManage", "btn_InventoryManagement", "btn_AllocationManagement" };
private void Form1_Load(object sender, EventArgs e)
{
StockManage.Visible = false;
StockOutManage.Visible = false;
InventoryManagement.Visible = false;
AllocationManagement.Visible = false;
}
private void StockManage_VisibleChanged(object sender, EventArgs e)
{
TableLayoutPanel tlp = sender as TableLayoutPanel;
string name = tlp.Name;
AutpAdaptControl(name);
}
private void btn_Manage_Click(object sender, EventArgs e)
{
Button btn = sender as Button;
string name = btn.Name;
int index = name.IndexOf('_');
name = name.Substring(index + 1);
dic[name] = dic[name] ? false : true;
TableLayoutPanel tlp = (TableLayoutPanel)this.Controls.Find(name, true)[0];
tlp.Visible = dic[name];
}
private void AutpAdaptControl(string nameStr)
{
string name = "";
int n = -1;
foreach (var item in dic.Keys)
{
n++;
if (item == nameStr)
break;
}
for (int i = n; i < dic.Count; i++)
{
if (i == 0)//当为第一个TabLayoutPanel时
{
name = dic.ElementAt(i).Key;//获得第一个tlp的名称
TableLayoutPanel tlp = (TableLayoutPanel)this.Controls.Find(name, true)[0];//获得第一个tlp的对象
Button btn = (Button)this.Controls.Find(
btnNames[i], true)[0];//获得第一个Button对象
if (tlp.Visible)//如果第一个tlp的对象显示,则第一个tlp在第一个Button下方
{
tlp.Top = btn.Bottom;
}
else//如果第一个tlp的对象显示不显示,则第二个Button在第一个Button下方
{
Button btnNext = (Button)this.Controls.Find(btnNames[i + 1], true)[0];//获得第二个Button对象
btnNext.Top = btn.Bottom;
}
}
else if (i > 0)//当不是第一个tlp对象时
{
name = dic.ElementAt(i - 1).Key;//获得前一个tlp对象名称
TableLayoutPanel tlp = (TableLayoutPanel)this.Controls.Find(name, true)[0];//获得前一个tlp对象
if (tlp.Visible)//前一个tlp对象显示时
{
Button btnNow = (Button)this.Controls.Find(btnNames[i], true)[0];//获得当前索引的Button
btnNow.Top = tlp.Bottom;//当前索引的Button位于前一个tlp对象的下方
name = dic.ElementAt(i).Key;//获得前一个tlp对象名称
tlp = (TableLayoutPanel)this.Controls.Find(name, true)[0];//获得当前一个tlp对象
tlp.Top = btnNow.Bottom;
}
else//前一个tlp对象不显示时
{
Button btnLast = (Button)this.Controls.Find(btnNames[i - 1], true)[0];//前一个Button对象
Button btnNow = (Button)this.Controls.Find(btnNames[i], true)[0];//当前索引的Button对象
btnNow.Top = btnLast.Bottom;//当前索引的Button对象位于前一个Button对象的下方
name = dic.ElementAt(i).Key;//获得当前一个tlp对象名称
tlp = (TableLayoutPanel)this.Controls.Find(name, true)[0];//获得当前前一个tlp对象
if (tlp.Visible)
{
tlp.Top = btnNow.Bottom;
}
}
}
}
}
}