What event for graphics methods

W

Woodie Morris

I'm just getting started in vb.net, and I'm using the drawline method to
draw to a form's picturebox in the form's load event, but the picturebox is
blank when the form loads. The code works fine when it's placed into the
click event of a command button on the form and you click the command
button, but I want it drawn before the form is shown. What event should I
use? (I've tried everyone I can think of: Activate, activated, shown, etc).

Thanks -Woodie M.
 
A

Armin Zingler

Woodie said:
I'm just getting started in vb.net, and I'm using the drawline method to
draw to a form's picturebox in the form's load event,

If it's not visible, it can not be seen.
but the picturebox is
blank when the form loads. The code works fine when it's placed into the
click event of a command button on the form and you click the command
button, but I want it drawn before the form is shown.

Output is done on the screen. If the window is not visible on the screen
(yet), there is no output. Well, you can draw without getting an
exception but it goes to nowhere. The OS takes your command and thinks:
the output window is not visible, so discard this command. Something
like that.
What event should I
use? (I've tried everyone I can think of: Activate, activated, shown, etc).

The Picturebox' Paint event.

Though, the events you've mentioned should work also, but you would
loose the graphics after restoring the window from minimized state or
after your window is unconvered (after being covered before). In the
events mentioned, do you call the Picturebox' CreateGraphics method to
get a Graphics object to draw on? Anyway, use the Paint event.


Basics:
http://msdn.microsoft.com/en-us/library/dd162759(VS.85).aspx

(in particular sub topic "when to draw in a window"; the Paint event is
the managed handler for the WM_PAINT message mentioned.) Unfortunatelly,
at first glance, I didn't find an introductory sentence that explicitly
states that the output to windows is not persitent and must be done
every time the OS asks you to do so.


Armin
 
W

Woodie Morris

Thanks Armin, that helped a lot. I've got it working fine except for when
the form containing the picturebox is first shown. The picturebox is blank
when it's first shown, but then shows the calendar grid (it's going to be a
calendar program) only after resizing the form. The form is the project's
startup form. Here's my code, it's in a class that's instantiated in the
form's declaration section.

Public Sub Draw() Handles CalendarPictureBox.Paint,
CalendarPictureBox.Resize
Dim t As Int16, HeightIndex As Long, WidthIndex As Long
Dim MyPen As New Pen(DividerColor)
MyPen.Width = 4
Dim G As Graphics = CalendarPictureBox.CreateGraphics
If WeekView Then

Else
G.Clear(DayColor1)
WidthIndex = CalendarPictureBox.Width / 7
HeightIndex = CalendarPictureBox.Height / 5
For t = 1 To 4
G.DrawLine(MyPen, 0, t * HeightIndex,
CalendarPictureBox.Width, t * HeightIndex)
Next t
For t = 1 To 6
G.DrawLine(MyPen, t * WidthIndex, 0, t * WidthIndex,
CalendarPictureBox.Height)
Next
MyPen.Color = BorderColor
G.DrawLine(MyPen, 0, MyPen.Width / 2, CalendarPictureBox.Width,
MyPen.Width / 2)
G.DrawLine(MyPen, CalendarPictureBox.Width - (MyPen.Width / 2),
0, CalendarPictureBox.Width - (MyPen.Width / 2), CalendarPictureBox.Height)
G.DrawLine(MyPen, CalendarPictureBox.Width,
CalendarPictureBox.Height - (MyPen.Width / 2), 0,
CalendarPictureBox.Height - (MyPen.Width / 2))
G.DrawLine(MyPen, MyPen.Width / 2, CalendarPictureBox.Height,
MyPen.Width / 2, 0)
End If
End Sub

CalendarPictureBox and the Colors are assigned in the form's load event
before calling the Draw method.

How do I get the calendar grid up after showing the form without using a
button or resize, etc?

Thanks again, Woodie
 
A

Armin Zingler

Woodie said:
Thanks Armin, that helped a lot. I've got it working fine except for when
the form containing the picturebox is first shown. The picturebox is blank
when it's first shown, but then shows the calendar grid (it's going to be a
calendar program) only after resizing the form. The form is the project's
startup form. Here's my code, it's in a class that's instantiated in the
form's declaration section.

Public Sub Draw() Handles CalendarPictureBox.Paint,
CalendarPictureBox.Resize

I must admit, I did not know that this even can be compiled because the
signature for the Paint event handler is actually:

Public Sub Draw(ByVal sender As Object, ByVal e As
System.Windows.Forms.PaintEventArgs) Handles
CalendarPictureBox.Paint


Then use

Dim G As Graphics = e.Graphics

to paint on. Do not call CalendarPictureBox.CreateGraphics. Otherwise
you paint on the picturebox first, afterwards it's covered by what
you've painted on e.graphics - i.e. by nothing (or by what's painted in
OnPaintBackground, the BackgroundColor). The reason why this does
not happen if you enlarge the control, is, that e.graphics contains a
clipping region covering only the dirty part (the "update region") of
the control. So if you enlarge it, the part already visible before is
not overwritten. In other words, the first time it is shown, the whole
control must be painted, and consequently the whole control is filled
with the background color.

Also, do _not_ call e.graphics.Dispose which would have been necessary
as ther counterpart to CreateGraphics.

The Resize event must be handled seperately because signatures do not
match. Call CalendarPictureBox.Invalidate there. That puts the whole
control's client area to the update region and it will be painted ASAP,
i.e. the Paint event will be raised.



BTW, Option Strict On is your friend. ;)

WidthIndex = PictureBox1.Width / 7

Did you think about the fractions of this division? Cut? Rounding? Also
note that there is the \ operator (if required).


Armin
 
C

Chris

I'm just getting started in vb.net, and I'm using the drawline method to
draw to a form's picturebox in the form's load event, but the picturebox is
blank when the form loads. The code works fine when it's placed into the
click event of a command button on the form and you click the command
button, but I want it drawn before the form is shown. What event should I
use? (I've tried everyone I can think of: Activate, activated, shown, etc).

Thanks -Woodie M.

As Armin alluded to in his posts, you should not draw directly to a
picture box control. It was not meant for that purpose. Check out
the following link and the rest of that site as well:

http://www.bobpowell.net/pictureboxhowto.htm

http://www.bobpowell.net

Chris
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top