I am creating a user control that displays two boxes. Box 1 is displayed at the top of the control and Box 2 is positioned just below it.
I have set AutoScroll=True and purposefully set the AutoScrollMinimum size to twice the height of the user control in order to force a vertical scrollbar.
I want to be able to scroll up and down the user control and have two things happen
1. Box1 moves in and out of the screen depending on how far I scroll
2. Box2 remains stationary in the same position regardless of where the user scrolls.
I have overriden the OnPaint method of the usercontrol with the code below
The problem I am having is that the Box 2 does not remaing stationary as I expected and also if you scroll quickly neither box draws correctly and leaves lines and colors all over the control?? I have set various styles to control this to no affect. Can anybody help me understand where I am going wrong?
Thanks in advance.
I have set AutoScroll=True and purposefully set the AutoScrollMinimum size to twice the height of the user control in order to force a vertical scrollbar.
I want to be able to scroll up and down the user control and have two things happen
1. Box1 moves in and out of the screen depending on how far I scroll
2. Box2 remains stationary in the same position regardless of where the user scrolls.
I have overriden the OnPaint method of the usercontrol with the code below
VB.NET:
Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
'Set the minimum autoscroll size and force a scroll
Me.AutoScrollMinSize = New Size(Me.Width, Me.Height * 2)
Dim oBrush1 As New SolidBrush(Color.White)
Dim oBrush2 As New SolidBrush(Color.Red)
Dim oPen As New Pen(Color.Black)
Dim oBoxSize As New Size(100, 50)
Dim oPosition As New Point(40, 40)
'Draw a box which is scrolled when the scrollbar is moved
Dim oRect As New Rectangle(oPosition, oBoxSize)
e.Graphics.FillRectangle(oBrush1, oRect)
e.Graphics.DrawRectangle(oPen, oRect)
Debug.WriteLine("Drawn rectangle at " & oRect.X & "," & oRect.Y)
'Draw a fixed box that doesn't move (use autoscroll to calculate the new position)
oPosition = New Point(40 - Me.AutoScrollPosition.X, 100 - Me.AutoScrollPosition.Y)
oRect = New Rectangle(oPosition, oBoxSize)
e.Graphics.FillRectangle(oBrush2, oRect)
e.Graphics.DrawRectangle(oPen, oRect)
Debug.WriteLine("Drawn rectangle at " & oRect.X & "," & oRect.Y)
'Cleanup the objects
oBrush1.Dispose()
oBrush2.Dispose()
oPen.Dispose()
'Pass the call down
MyBase.OnPaint(e)
End Sub
Thanks in advance.