The scroll position is not maintained in an auto-scrollable Panel control in a Windows Forms application (829417)
The information in this article applies to:
- Microsoft Visual Studio 2005 Professional Edition
- Microsoft Visual Studio 2005 Standard Edition
- Microsoft Visual Studio .NET (2003), Enterprise Architect Edition
- Microsoft Visual Studio .NET (2003), Enterprise Developer Edition
- Microsoft Visual Studio .NET (2003), Academic Edition
- Microsoft Visual Studio .NET (2002), Professional Edition
- Microsoft Visual Studio .NET (2002), Enterprise Architect Edition
- Microsoft Visual Studio .NET (2002), Enterprise Developer Edition
- Microsoft Visual Studio .NET (2002), Academic Edition
SYMPTOMSIn a Microsoft Windows Forms application, if you clear and
then redraw an auto-scrollable Panel control that contains child controls, the scroll position is not
maintained.CAUSESometimes, you must clear the contents of the Panel control and then redraw the contents of the Panel control. For example, you must do this if the auto-scrollable Panel control contains a collection of controls that have a specific order. Typically, these controls are user controls.
However, the application does not store the value of the
AutoScrollPosition property of the Panel control. Therefore, the scroll position is not
maintained when the contents of the Panel control are redrawn.WORKAROUNDTo work around this behavior, use a System.Drawing.Point structure to store value of the AutoScrollPosition property of the Panel control. After the Panel control is redrawn, you can retrieve the value of the AutoScrollPosition property by using a new instance of the System.Drawing.Point structure. The get method of the Panel.AutoScrollPosition.X property and the get method of the Panel.AutoScrollPosition.Y property return negative values. However, positive values are
required. You can use the Math.Abs function to obtain a positive value from the Panel.AutoScrollPosition.X property and the Panel.AutoScrollPosition.Y property, as in the following line of code: Visual
Basic .NET or Visual Basic 2005 codePanel1.AutoScrollPosition = New Point(Math.Abs(Panel1.AutoScrollPosition.X), Math.Abs(CurrentPoint.Y))
Visual
C# .NET Visual C# 2005 codepanel1.AutoScrollPosition = new Point(Math.Abs(panel1.AutoScrollPosition.X), Math.Abs(CurrentPoint.Y)); Sample applicationTo use this workaround in a sample application, follow these
steps:
- Click Start, point to
Programs, point to Microsoft Visual Studio
.NET, and then click Microsoft Visual Studio
.NET or Microsoft Visual Studio
2005.
- On the File menu, point to
New, and then click Project. The New
Project dialog box appears.
- Under Project Types, click Visual
Basic Projects or click Visual
C# Projects.
Note In Visual Studio 2005, click Visual
C#. - Under Templates, click Windows
Application.
- In the Name box, type
SampleWinApp, and then click OK. By
default, a form that is named Form1 is created.
- Add a Button control and a Panel control to the Form1 form.
Note Do not insert the Button control inside the Panel control. - Right-click the Panel control, and then
click Properties.
- Set the AutoScroll property to True.
- In the Form1.vb file, add the following code before the End Class statement:
Visual
Basic .NET or Visual Basic 2005 codePrivate count As Integer
Private arrayctl As New ArrayList
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim newtxt As New TextBox
newtxt.Text = count
count += 1
arrayctl.Add(newtxt)
DrawControls()
End Sub
Private Sub DrawControls()
Dim txt As TextBox
Dim CurrentPoint As System.Drawing.Point
CurrentPoint = Panel1.AutoScrollPosition()
Dim i As Integer = 0
Panel1.Controls.Clear()
Panel1.SuspendLayout()
For Each txt In arrayctl
Panel1.Controls.Add(txt)
txt.Width = Panel1.ClientRectangle.Width
txt.Top = i
i += txt.Height
Next
Panel1.ResumeLayout()
Panel1.AutoScrollPosition = New Point(Math.Abs(Panel1.AutoScrollPosition.X), Math.Abs(CurrentPoint.Y))
End Sub Visual
C# .NET or Visual C# 2005 codeprivate int count;
private ArrayList arrayctl = new ArrayList();
private void button1_Click(object sender, System.EventArgs e)
{
TextBox newtxt = new TextBox();
newtxt.Text = count.ToString();
count++; arrayctl.Add(newtxt);
DrawControls();
}
private void DrawControls()
{
System.Drawing.Point CurrentPoint; CurrentPoint = panel1.AutoScrollPosition;
int i = 0;
panel1.Controls.Clear();
panel1.SuspendLayout();
foreach (TextBox txt in arrayctl)
{
panel1.Controls.Add(txt);
txt.Width = panel1.ClientRectangle.Width;
txt.Top = i; i += txt.Height;
}
panel1.ResumeLayout();
panel1.AutoScrollPosition = new Point(Math.Abs(panel1.AutoScrollPosition.X), Math.Abs(CurrentPoint.Y));
} - On the Debug menu, click
Start.
- In the Form1 form, click Button1
repeatedly to make the scroll bar appear in the Panel control.
Notice that the scroll position is maintained
in the Panel control.
STATUS This
behavior is by design.REFERENCESFor more information, visit the following Microsoft
Developer Network (MSDN) Web site:
Modification Type: | Major | Last Reviewed: | 2/28/2006 |
---|
Keywords: | kbvs2005swept kbvs2005applies kbprb kbScrollBar kbWindowsForms KB829417 kbAudDeveloper |
---|
|