Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Paint event not firing

19 views
Skip to first unread message

Chris Wallis

unread,
Jul 3, 2002, 3:33:40 AM7/3/02
to
I am struggling to understand when the paint event should
fire, since I have created a very simply app to simply
draw a line on a picturebox whenever the paint event
fires. Unfortunately the line only appears when I move
another window over the picturebox, not when the form is
restored from being minimized or focus is switched to
another window and back again. How can I ensure that the
line is always painted?

The app is very simply. Create a form with a picturebox
called picMain and add the following code.

Private Sub Form1_Load(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles MyBase.Load
DrawLine()
End Sub

Private Sub DrawLine()

Dim g As Graphics
Dim oPen As New Pen(Color.Black)

Try
g = picMain.CreateGraphics
g.DrawLine(oPen, 0, 0, 200, 200)
Finally
oPen.Dispose()
oPen = Nothing
g.Dispose()
g = Nothing
End Try
End Sub

Private Sub picMain_Paint(ByVal sender As Object,
ByVal e As System.Windows.Forms.PaintEventArgs) Handles
picMain.Paint
DrawLine()
End Sub

Bob Powell

unread,
Jul 3, 2002, 6:31:15 AM7/3/02
to
You're not using the Graphics object passed to your paint event.

Do not create a new graphics in your line drawing routine, pass the Graphics
object from the PaintEventArgs to the DrawLine routine and use it instead.

Bob.
--
Have you thought about buying my book?
http://www.amazon.com/exec/obidos/ASIN/067232153X/bobpowelnet

Visit NetEdge Software to find out about the Web Service Enabler.
Enable all your legacy VB6 applications to seamlessly connect to .NET
web-services.
http://www.netedgesoftware.com

"Chris Wallis" <c.wallis@nospam> wrote in message
news:14ed601c22263$f3d7ad60$39ef2ecf@TKMSFTNGXA08...

Chris Wallis

unread,
Jul 3, 2002, 6:54:26 AM7/3/02
to
I tried this but it made no difference.

I have however discovered that if I draw on either the
form or a panel instead of a picturebox, then it works
fine. What is so different about a picturebox?

>.
>

Chris Wallis

unread,
Jul 3, 2002, 7:01:46 AM7/3/02
to
Bob,

I beg your pardon, my mistake. It does work passing the
graphics with a picturebox. I am still curious about why
it works the other way with forms and panels though.

>.
>

Bob Powell

unread,
Jul 3, 2002, 9:29:23 AM7/3/02
to
It doesn't work differently. You must have another error.
Why don't you post your code and then we can see what's wrong.

"Chris Wallis" <chris.wallis@nospam> wrote in message
news:12d0701c22281$063271d0$2ae2...@hosting.microsoft.com...

Andrew

unread,
Jul 3, 2002, 1:46:08 PM7/3/02
to
Perhaps its because the PictureBox has its DoubleBuffer style set to true.

"Chris Wallis" <chris.wallis@nospam> wrote in message
news:12d0701c22281$063271d0$2ae2...@hosting.microsoft.com...

Chris Wallis

unread,
Jul 4, 2002, 4:25:59 AM7/4/02
to
Bob,

Just replace the picturebox in the original code with a
panel. Passing the control to the DrawLine funtion rather
than the graphics object works for the panel but not the
picturebox...

In both cases the controls (picturebox and panel) have all
their default properties.

Could this be something to do with how pictureboxes and
panels handle the creation and destruction of Device
Contexts?

>.
>

Bob Powell

unread,
Jul 4, 2002, 6:15:08 AM7/4/02
to
Chris,
Here is a sample program, written in VB that draws a big red X on forms,
pictureboxes and panels.
You'll see that the technique is the same for all of these items. You must
consistently pass and use the PaintEventArgs *or* the Graphics object
provided.

Enjoy.

Bob.

Visit NetEdge Software to find out about the Web Service Enabler.
Enable all your legacy VB6 applications to seamlessly connect to .NET
web-services.
http://www.netedgesoftware.com

Public Class Form1

Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

Public Sub New()

MyBase.New()

'This call is required by the Windows Form Designer.

InitializeComponent()

'Add any initialization after the InitializeComponent() call

End Sub

'Form overrides dispose to clean up the component list.

Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)

If disposing Then

If Not (components Is Nothing) Then

components.Dispose()

End If

End If

MyBase.Dispose(disposing)

End Sub

'Required by the Windows Form Designer

Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form Designer

'It can be modified using the Windows Form Designer.

'Do not modify it using the code editor.

Friend WithEvents PictureBox1 As System.Windows.Forms.PictureBox

Friend WithEvents Panel1 As System.Windows.Forms.Panel

<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

Me.PictureBox1 = New System.Windows.Forms.PictureBox()

Me.Panel1 = New System.Windows.Forms.Panel()

Me.SuspendLayout()

'

'PictureBox1

'

Me.PictureBox1.Location = New System.Drawing.Point(192, 16)

Me.PictureBox1.Name = "PictureBox1"

Me.PictureBox1.Size = New System.Drawing.Size(88, 96)

Me.PictureBox1.TabIndex = 0

Me.PictureBox1.TabStop = False

'

'Panel1

'

Me.Panel1.Location = New System.Drawing.Point(16, 160)

Me.Panel1.Name = "Panel1"

Me.Panel1.Size = New System.Drawing.Size(96, 104)

Me.Panel1.TabIndex = 1

'

'Form1

'

Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)

Me.ClientSize = New System.Drawing.Size(292, 273)

Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.Panel1,
Me.PictureBox1})

Me.Name = "Form1"

Me.Text = "Form1"

Me.ResumeLayout(False)

End Sub

#End Region

Private Sub Form1_Paint(ByVal sender As Object, ByVal e As
System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint

drawabigx(CType(sender, Control), e)

End Sub

Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As
System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint

drawabigx(CType(sender, Control), e)

End Sub

Private Sub Panel1_Paint(ByVal sender As Object, ByVal e As
System.Windows.Forms.PaintEventArgs) Handles Panel1.Paint

drawabigx(CType(sender, Control), e)

End Sub

Private Sub drawabigx(ByVal c As Control, ByVal e As PaintEventArgs)

Dim p As Pen = New Pen(Color.Red, 10)

e.Graphics.DrawLine(p, 5, 5, c.Width - 10, c.Height - 10)

e.Graphics.DrawLine(p, 5, c.Height - 10, c.Width - 10, 5)

p.Dispose()

End Sub

End Class


"Chris Wallis" <chris.wallis@nospam> wrote in message

news:1324001c22334$6cc8dd90$36ef2ecf@tkmsftngxa12...

Deepak Kapoor

unread,
Jul 5, 2002, 6:14:15 AM7/5/02
to
Okay here is my solution.

Don't use paint method at all

call your Draw() function at form load or any other event

and in Draw()

do something like pictureBox.Refresh()

I took this route for one of my applications.
Deepak Kapoor


*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Bob Powell

unread,
Jul 5, 2002, 6:55:53 AM7/5/02
to
You can refresh all the controls from the SizeChanged event too...

Private Sub Form1_SizeChanged(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.SizeChanged

Dim c As Control

For Each c In Me.Controls

c.Refresh()

Next

End Sub

This works with the X program I sent you Chris...

Bob.

"Deepak Kapoor" <letsd...@hotmail.com> wrote in message
news:uw8p4yAJCHA.1876@tkmsftngp12...

0 new messages