tags:

views:

8

answers:

1

I've got a strange problem here. I have a winforms panel which programatically has labels and a usercontrol (which is capable of collapsing/expanding) added. The user defined controls are in groups which is where the labels come in to it. Due to the size and number of controls I have set the panel to support scrolling.

So far so good: The problem occurs when the panel is scrolled down away from the top. Any manipulation of controls within the panel results in dead space (empty) at the top.

Manipulation takes place as follows:

When a control is added, removed or resized the "RearrangeControls()" method is called. The method maintains an integer for which the height of all controls (and empty space to prevent controls appearing right next to each other) which is added to every time a control is adjusted.

Iteration takes place through groups and then through instances of this user control to ensure that they are grouped properly. (I hope this explination makes sense)

My Rearrange method is included below:

  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();
  }

The Y values of all controls within this panel all start at 0 so they should be at the top. I've searched around and haven't been able to find any information about what this error is. does anyone know what's going on with this thing? I'd greatly appreciate any pointers/help/advice on this matter.

+1  A: 

Problem solved.

I needed to use use the following:

int iYpos = pnlMain.AutoScrollPosition.Y

If anyone else runs in to the same probme I hope this helps.

(At which point I finally collape in front of keyboard - it's been a long day)

Daniel Snowden