Paint event causes System.NullReferenceException

  • Thread starter Thread starter Able
  • Start date Start date
A

Able

Dear friends

I have some drawing commands in the Form1_paint
event. It works pretty well. The drawing gets
redrawn when the window is resized. It gets redrawn after I minizie the
window and bring it back. But when I bring another application to the
foreground and drag it across my graphics window or let my window come to
foreground again the program crashes.

This errormsg occurs at "Public Class Form1":

An unhandled exception of
type 'System.NullReferenceException' occurred in
system.windows.forms.dll

Additional information: Object reference not set to an
instance of an object

Here is the Form1_paint routine:
Dim gb As New Drawing2D.LinearGradientBrush( _

New Rectangle(0, 0, Form1.ActiveForm.Width, Form1.ActiveForm.Height), _

Color.White, Color.Yellow, 10 _

)

gb.SetSigmaBellShape(0.9)

e.Graphics.FillRectangle( _

gb, _

0, _

0, _

Form1.ActiveForm.Width, _

Form1.ActiveForm.Height _

)



Somebody knows what is wrong?



Regards Able
 
Check out if the Form1.ActiveForm has a value.

if Form1.ActiveForm is nothing then
MsgBox("Blub")
end if
 
OK, I typed this in first of Form1_paint event

If Form1.ActiveForm Is Nothing Then

MsgBox("Blub")

End If

The message was fired before each crash But I dont understand how I can take
advantage of this.

Rgards Able
 
Hi Able,

Can you try it also in a simple program with only this, I cannot get your
exception.

A nice white and yellow page that I can close, minimize, hide behind another
etc.
no exception.

Framework 1.1

Cor
 
Now you know that Form1.ActiveForm is nothing.

That's the Reason why it crashes....

You can not get the Width of a Object that is not existent. Right ?

Only execute the whole Code if activeForm is not nothing
or don't use ActiveForm.....
_
 
Back
Top