2010-07-19 85 views
1

我在这里遇到了一个奇怪的问题。我有一个winforms面板编程方式有标签和一个usercontrol(这是能够崩溃/扩大)添加。用户定义的控件位于标签进入的位置。由于控件的大小和数量,我将面板设置为支持滚动。滚动面板导致控件出现半途下降面板

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

操纵如下发生:

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

迭代通过组,然后通过此用户控件的实例进行,以确保它们被正确分组。 (我希望这explination是有道理的)

我重新编排方法如下包括:

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开始内所以他们应该是在顶部。我搜索了四周,一直未能找到有关此错误的信息。有人知道这件事发生了什么?我非常感谢关于此事的任何指示/帮助/建议。

回答

2

问题解决。

我需要使用使用以下命令:

int iYpos = pnlMain.AutoScrollPosition.Y 

如果别人在相同的probme我希望这有助于运行。

(此时我终于在键盘前合拢 - 这是一个漫长的一天)

+0

这工作就像一个魅力!谢谢丹尼尔。 – Naren 2013-04-04 13:36:53