winform控件动态布局显示

项目中需要实现这样一个功能,用winform做一个客户端软件,首页上有4个模块(通过4个panel实现),这4个模块需要通过配置来动态显示其中的一个或多个,很显然我们都知道这有2的4次方,也就是16种组合,去掉一种0的状态,也就是15种排列方式。

假设这4个模块分别为A、B、C、 D

那15种组合就是:A,B,C,D,AB,AC,AD,BC,BD,CD,ABC,ABD,ACD,BCD,ABCD

方法一:

我们把这15种排列一个一个罗列出来,这样简单明了,不会把自己绕进去,就是需要写15个if而已,具体就不写了

方法二:

允许一定程度的if嵌套,什么程度呢,只要不把自己绕进去就行了,我选择了这种方式。

涵盖A、AB、AC、AD这几种组合

if(A){
A.Visible = true;
A.Location = new Point(a1,a2);
  if(B && !C && !D){
    B.Visible = true;
	B.Location = new Point(b1,b2);
  }else if(!B && C && !D){
    C.Visible = true;
	C.Location = new Point(c1,c2);
  }else if(!B && !C && D){
    D.Visible = true;
	D.Location = new Point(d1,d2);
  }
}

涵盖B、BC、BD

if(!A && B){
   B.Visible = true;
   B.Location = new Point(b1,b2);
   if(C && !D){
     C.Visible = true;
	 C.Location = new Point(c1,c2);
   }else if(!C && D){
     D.Visible = true;
	 D.Location = new Point(d1,d2);
   }
}

涵盖C、CD

if(!A && !B && C){
  C.Visible = true;
  C.Location = new Point(c1,c2);
  if(D){
    D.Visible = true;
	D.Location = new Point(d1,d2);
  }
}

D

if(!A && !B && !C && D){
  D.Visible = true;
  D.Location = new Point(d1,d2);
}

ABC

if(A && B && C && !D){
  A.Visible = true;
  A.Location = new Point(a1,a2);
  B.Visible = true;
  B.Location = new Point(b1,b2);
  C.Visible = true;
  C.Location = new Point(c1,c2);
}

ABD

if(A && B && !C && D){
  A.Visible = true;
  A.Location = new Point(a1,a2);
  B.Visible = true;
  B.Location = new Point(b1,b2);
  D.Visible = true;
  D.Location = new Point(c1,c2);
}

ACD

if(A && !B && C && D){
  A.Visible = true;
  A.Location = new Point(a1,a2);
  C.Visible = true;
  C.Location = new Point(b1,b2);
  D.Visible = true;
  D.Location = new Point(c1,c2);
}

BCD

if(!A && B && C && D){
  B.Visible = true;
  B.Location = new Point(a1,a2);
  C.Visible = true;
  C.Location = new Point(b1,b2);
  D.Visible = true;
  D.Location = new Point(c1,c2);
}

ABCD

if(A && B && C && D){
  A.Visible = true;
  A.Location = new Point(a1,a2);
  B.Visible = true;
  B.Location = new Point(a1,a2);
  C.Visible = true;
  C.Location = new Point(b1,b2);
  D.Visible = true;
  D.Location = new Point(c1,c2);
}

这样就相当于用了9个大判断实现了功能,又在比较容易理解的范围内。
不过我相信肯定还有更简洁易懂的方法。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

角落工程师

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值