C# 在运行时更改Windows窗体面板位置

C# 在运行时更改Windows窗体面板位置,c#,windows,winforms,C#,Windows,Winforms,我设计了一个表单,用作打印模板。它是用来填写表格的,所以我必须在绘制页边距之前考虑页边距 我当前的问题是试图让控件按指定的数量切换 private static void MarginShift(控件ctrl,边距m){ 标签lbl=ctrl作为标签; 如果(lbl!=null){ lbl.Location=新点(lbl.Location.X+m.左,lbl.Location.Y+m.顶); }否则{ 面板pnl=ctrl作为面板; 如果(pnl!=null){ int x=pnl.Locati

我设计了一个表单,用作打印模板。它是用来填写表格的,所以我必须在绘制页边距之前考虑页边距

我当前的问题是试图让控件按指定的数量切换

private static void MarginShift(控件ctrl,边距m){
标签lbl=ctrl作为标签;
如果(lbl!=null){
lbl.Location=新点(lbl.Location.X+m.左,lbl.Location.Y+m.顶);
}否则{
面板pnl=ctrl作为面板;
如果(pnl!=null){
int x=pnl.Location.x;
int y=pnl.Location.y;
pnl.位置=新点(x+m.左侧,y+m.顶部);
if((pnl.Location.X==X)和&(pnl.Location.Y==Y)&&
((0
我的小控制台输出会因为我从模板表单传递的每一个内容而受到影响

Microsoft的控件文档说明了以下价值:

获取或设置控件左上角相对于其容器左上角的坐标

那么,为什么使用移动a不能移动a


我需要做什么来纠正这个问题?

没关系。面板固定在窗体上

private static void MarginShift(Control ctrl, Margins m) {
  Label lbl = ctrl as Label;
  if (lbl != null) {
    lbl.Location = new Point(lbl.Location.X + m.Left, lbl.Location.Y + m.Top);
  } else {
    Panel pnl = ctrl as Panel;
    if (pnl != null) {
      int x = pnl.Location.X;
      int y = pnl.Location.Y;
      pnl.Location = new Point(x + m.Left, y + m.Top);
      if ((pnl.Location.X == x) && (pnl.Location.Y == y) &&
          ((0 < m.Left) || (0 < m.Top))) {
        Console.WriteLine("WTF?");
      }
      foreach (Control c2 in pnl.Controls) {
        MarginShift(c2, m);
      }
    }
  }
}