【问题标题】:Scrolling panel causes controls to appear halfway down panel滚动面板导致控件出现在面板的中间
【发布时间】:2010-07-19 15:46:31
【问题描述】:

我在这里遇到了一个奇怪的问题。我有一个 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 开始,因此它们应该位于顶部。我四处搜索,但无法找到有关此错误的任何信息。有谁知道这件事是怎么回事?对于此事,我将不胜感激任何指针/帮助/建议。

【问题讨论】:

    标签: c# winforms


    【解决方案1】:

    问题解决了。

    我需要使用以下内容:

    int iYpos = pnlMain.AutoScrollPosition.Y
    

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

    (此时我终于在键盘前崩溃了——这是漫长的一天)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-01-06
      • 2013-02-27
      • 1970-01-01
      • 1970-01-01
      • 2018-12-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多