Srolling graphics in a panel

Hoser

Member
Joined
Mar 11, 2007
Messages
11
Programming Experience
1-3
Hello All,

This has really has got me stumped. I have some columns which I created inside a panel control using the creategraphics class.
I know the graphics I'm creating is larger than the panel itself yet when I change the panel autoscroll property to true and the autoscrollmin, no scroll bar appears.
I can't seem to get the horizontal scrolling bar to appear no matter what I do. Can someone help me to better understand why this is happening?

Code:

Private Sub Draw_Columns(ByVal NumberOfColumns As Integer)

Dim grfx2 As Graphics = Panel1.CreateGraphics
Dim grfx3 As Graphics = Panel2.CreateGraphics
Dim I As Integer
Dim days As Integer
Dim Weekend As Boolean = False
Dim Xlocation As Integer = (Panel1.Location.X - 12)
Dim XPanel2Location As Integer = Panel2.Location.X
For I = 1 To NumberOfColumns
'Draw Rectangle
grfx2.DrawRectangle(Pens.Black, New Rectangle(Xlocation, Panel1.Location.Y - 76, 21, Panel1.Height))
days += 1
If days = 6 Then
'Fill Rectangle
grfx2.FillRectangle(Brushes.SkyBlue, New Rectangle(Xlocation, Panel1.Location.Y - 76, 20, Panel1.Height))
ElseIf days = 7 Then
grfx2.FillRectangle(Brushes.SkyBlue, New Rectangle(Xlocation, Panel1.Location.Y - 76, 20, Panel1.Height))
days = 0
End If
Xlocation += 21
If I <= 9 Then
grfx3.DrawString(I, New Font("VERDANA", 8, FontStyle.Regular, GraphicsUnit.Point), Brushes.Black, XPanel2Location - 8, Panel2.Location.Y - 35)
Else
grfx3.DrawString(I, New Font("VERDANA", 8, FontStyle.Regular, GraphicsUnit.Point), Brushes.Black, XPanel2Location - 12, Panel2.Location.Y - 35)
End If
XPanel2Location += 21
Next
End Sub


Regards:eek:
 
Don't draw to a control with a Graphics instance created by CreateGraphics. You normally use the Paint event to draw and you are provided the Graphics instance there through the e.Graphics instance.

AutoScroll will only work if there is some controls outside visible area, which graphics isn't. You have to add scrollbars your self, HScrollBar and VScrollBar, when these are scrolled you use the Scroll event to force a paint refresh to the Panel with Refresh method (Panel123.Refresh), this causes the Paint event handler method to execute.

Your paint code could check the Scroll control for its Value and Maximum to determine what visible part to draw.

You understand there could be a lot of unnecessary drawing when scrolling, as long as the content is static. Better would be to implement the cached image, and display this with the Picturebox control inside the Panel. This would also mean the AutoScroll would work. You would only need to paint a new image when the source data or visual properties was changed.
 
Thanks John,

Your explination was helpful. I will take your suggestion and place a picturebox in the panel and draw my graphic to it.

Regards:D
 
I didn't mean draw to Picturebox control, I meant draw to bitmap image, then let Picturebox display this image.
 
Back
Top