469,609 Members | 1,097 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,609 developers. It's quick & easy.

Paint event causes System.NullReferenceException

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
Nov 20 '05 #1
6 1747

Check out if the Form1.ActiveForm has a value.

if Form1.ActiveForm is nothing then
MsgBox("Blub")
end if

"Able" <ab**************@epost.no> schrieb im Newsbeitrag
news:Nt*****************@news2.e.nsc.no...
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

Nov 20 '05 #2
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
"Armin" <Ar***@humsoft.com> skrev i melding
news:bp*************@news.t-online.com...

Check out if the Form1.ActiveForm has a value.

if Form1.ActiveForm is nothing then
MsgBox("Blub")
end if

"Able" <ab**************@epost.no> schrieb im Newsbeitrag
news:Nt*****************@news2.e.nsc.no...
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


Nov 20 '05 #3
Cor
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
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 _

)

Nov 20 '05 #4

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.....
New Rectangle(0, 0, Form1.ActiveForm.Width, Form1.ActiveForm.Height), _
"Able" <ab**************@epost.no> schrieb im Newsbeitrag
news:xK*****************@news2.e.nsc.no...
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
"Armin" <Ar***@humsoft.com> skrev i melding
news:bp*************@news.t-online.com...

Check out if the Form1.ActiveForm has a value.

if Form1.ActiveForm is nothing then
MsgBox("Blub")
end if

"Able" <ab**************@epost.no> schrieb im Newsbeitrag
news:Nt*****************@news2.e.nsc.no...
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



Nov 20 '05 #5
Damn. This seems to be a late evening.

Regards Able

"Cor" <no*@non.com> skrev i melding
news:uk**************@TK2MSFTNGP11.phx.gbl...
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
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 _

)


Nov 20 '05 #6
Solved it by replacing Form1.ActiveForm with Me in Form1_paint event.

Regards Able

"Able" <ab**************@epost.no> skrev i melding
news:6W*****************@news2.e.nsc.no...
Damn. This seems to be a late evening.

Regards Able

"Cor" <no*@non.com> skrev i melding
news:uk**************@TK2MSFTNGP11.phx.gbl...
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
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 _

)



Nov 20 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Tamir Khason | last post: by
reply views Thread by Duncan Mole | last post: by
8 posts views Thread by WStoreyII | last post: by
4 posts views Thread by Aaron Smith | last post: by
7 posts views Thread by Dennis | last post: by
16 posts views Thread by tommaso.gastaldi | last post: by
7 posts views Thread by Rotsey | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.