滚动面板导致控件显示在面板的中间位置

时间:2010-07-19 15:46:32

标签: c# winforms

我在这里遇到了一个奇怪的问题。我有一个winforms面板,以编程方式添加标签和用户控件(能够折叠/扩展)。用户定义的控件位于标签所在的组中。由于控件的大小和数量,我已将面板设置为支持滚动。

到目前为止一直很好:当面板从顶部向下滚动时会出现问题。对面板内控件的任何操作都会导致顶部出现死区(空)。

操纵如下:

当添加,删除或调整控件的控件时,将调用“RearrangeControls()”方法。该方法维护一个整数,其中所有控件的高度(以及用于防止控件彼此相邻出现的空白空间)将在每次调整控件时添加。

迭代通过组进行,然后通过此用户控件的实例进行,以确保它们被正确分组。 (我希望这种探索有意义)

我的重新排列方法包含在下面:

  private void RearrangeControls()
  {
     Console.WriteLine(String.Format("Top of panel: {0}", pnlMain.Top));
     this.SuspendLayout();
     //sort ranges in to ascending order, this is the primary grouping
     _lRanges.Sort(CompareStringAscending);

     //now sort items by alphabetical order, we will filter out groups later so this will not be a problem
     _lItems.Sort(CompareSelectionDetailViewAscending);

     int iYPos = 0;

     //first sort through by 
     foreach (string selectedRange in _lRanges)
     {
        int iRangeControlCount = 0;

        KryptonLabel label = pnlMain.Controls[selectedRange] as KryptonLabel;
        label.Location = new Point(_iTitleIndent, iYPos);

        iYPos += label.Height + _iControlSpacing;

        //now sort views
        foreach (SelectionDetailView selectionDetailView in _lItems)
        {
           if (selectionDetailView.Range == selectedRange)
           {
              selectionDetailView.Location = new Point(_iDetailIndent, iYPos);
              //Console.WriteLine(String.Format("{0} {1} {2}", selectionDetailView.Name, selectionDetailView.Location.X.ToString(), selectionDetailView.Location.Y.ToString()));
              iYPos += selectionDetailView.Height + _iControlSpacing;

              iRangeControlCount++;
           }
        }

        //if this is zero, then it meant the last label we aded has no controls associated wiht it.  If this is the case we shoudl remove it from the panel
        if (iRangeControlCount == 0)
        {
           iYPos -= label.Height + _iControlSpacing;
           pnlMain.Controls.Remove(label);
        }

        Console.WriteLine(String.Format("Y: {0}", iYPos));
     }

     pnlMain.ScrollControlIntoView(_oSelectedItem);
     this.ResumeLayout();
  }

此面板中所有控件的Y值均从0开始,因此它们应位于顶部。我一直在搜索,但无法找到有关此错误的信息。有谁知道这件事发生了什么?我非常感谢有关此事的任何指示/帮助/建议。

1 个答案:

答案 0 :(得分:2)

问题解决了。

我需要使用以下内容:

int iYpos = pnlMain.AutoScrollPosition.Y

如果有其他人遇到同样的问题,我希望这有帮助。

(此时我终于在键盘前拼了 - 这是漫长的一天)