Bob Butler
5/2/2012 12:41:00 AM
I'm working on something where I need to be able to draw multiple colored
blocks with text on them on a picturebox. After toying with FillRect and
the .Line method and other methods I decide to try formatting a second
picturebox with a single block's layout and then use PaintPicture to copy it
to the final position. It seemed like that would simplify the formatting
and eliminate having to keep creating and destroying brushes since I could
use the BackColor and ForeColor properties to let VB do it for me.
I was surprised when it would only allow me to change the BackColor of the
picturebox once. Further changes were accepted without error and the
property value changed but the color on the screen didn't. I've tried every
combination I can think of using Autoredraw, .picture=.image, Refresh, and
nothing works. I assume that I'm missing something blatantly obvious but if
I am I can't see it. I did find that using 'Set Picture2=Nothing' causes it
to recreate the control and work as I had expected but without that all I
get is the first color block repeated. Any suggestions or a dope slap would
be appreciated.
This is VB6 on XP; all you need is a form with 2 picture boxes.
Private Sub Form_Load()
picture1.Move 0, 0, Me.ScaleWidth / 2, Me.ScaleHeight
picture1.AutoRedraw = True
picture1.BackColor = vbWhite
picture1.BorderStyle = vbBSNone
picture2.Move picture1.Width, 0, picture1.Width, picture1.Height / 5
picture2.BorderStyle = vbBSNone
picture2.AutoRedraw = False
MakeBlock 4, vbBlue ' whichever one is first sets the color for all
MakeBlock 3, vbCyan
MakeBlock 2, vbGreen
MakeBlock 1, vbYellow
MakeBlock 0, vbRed
End Sub
Private Sub MakeBlock(ByVal Offset As Long, ByVal TheColor As Long)
' Set picture2 = Nothing ' <-- this line makes it work as expected
picture2.BackColor = TheColor
picture2.Picture = picture2.Image
picture2.Cls
picture2.Refresh
picture2.Picture = picture2.Image
picture1.PaintPicture picture2.Picture, 0, Offset * picture2.Height, , , , ,
, , vbSrcCopy
End Sub