OK several dumb (outdated programmer) questions:
Isn't a dumb question.
by derive - you mean - Dim Box1 as New PictureBox - right?
No, I mean inherit from Picturebox, or better from Control. See example
below.
I have no clue how to add a property to a Control and nothing in
help that I can find (likely not knowing where to look) - can you
explain?
the following example works:
Inports System.Drawing
Imports System.Windows.Forms
Public Class Form1
Private sub Form1_Load(ByVal sender as Object, ByVal e as
System.EventArgs) Handles MyBase.Load
DrawEllipse()
End Sub
In Form_Load, drawing doesn't make sense because the Form isn't visible yet.
Question: If I have no dragging or overlapping - just drawing in
various separate parts of the Form - do I need to worry about the
paint event? or OnPaint?
Yes, you have to paint in OnPaint or in the Paint event.
Since it works - why would some of my graphics vanish seemingly at
random? because I did not dispose the pen and g/Graphics properly?
or because Windows does a repaint at random?
Thanks for anything you may add.
Add the following class to a new project, compile it, open Form1 in designer
and add an instance of the control from the toolbox. It's a simple painting
example. Run it, and each time you click the left mouse button, a new point
is added to the list of Points. Afterwards, Invalidate is called in order to
refresh the control.
Public Class MyControl
Inherits Control
Private f_Points As New List(Of Point)
Sub New()
SetStyle( _
ControlStyles.AllPaintingInWmPaint _
Or ControlStyles.UserPaint _
Or ControlStyles.ResizeRedraw _
Or ControlStyles.OptimizedDoubleBuffer, _
True _
)
End Sub
Protected Overrides Sub OnPaint( _
ByVal e As System.Windows.Forms.PaintEventArgs)
e.Graphics.Clear(Color.Black)
For Index = 1 To f_Points.Count - 1
e.Graphics.DrawLine( _
Pens.White, f_Points(Index - 1), f_Points(Index) _
)
Next
End Sub
Protected Overrides Sub OnMouseDown( _
ByVal e As System.Windows.Forms.MouseEventArgs)
MyBase.OnMouseDown(e)
If e.Button = Windows.Forms.MouseButtons.Left _
AndAlso Control.ModifierKeys = Keys.None Then
f_Points.Add(New Point(e.X, e.Y))
Invalidate()
End If
End Sub
Protected Overrides Sub OnPaintBackground( _
ByVal pevent As System.Windows.Forms.PaintEventArgs)
'intentionally empty. All painting is done in OnPaint
End Sub
End Class
Armin