Syste.NullReferenceException

  • Thread starter Thread starter Ivan Weiss
  • Start date Start date
I

Ivan Weiss

Hey all, I am using the following code to attempt to draw an image on
the MdiClient Area thanks to some help from another post.

However, the line If m_MDIClient.Width > 0 And m_MDIClient.Height > 0
Then is throwing an exception saying m_MDIClient is a null reference.

I have the following variables at the top of the form:

Private m_MDIClient As MdiClient
Private m_OriginalBackgroundImage As Image
Private m_IgnoreSetBackgroundImage As Boolean

Here is the code in my form constructor:

Dim ctl As Control

For Each ctl In Controls
If TypeOf ctl Is MdiClient Then
m_MDIClient = ctl
m_MDIClient.BackColor = Me.BackColor
Exit For
End If
Next

In the mdi load I have:

Me.SetStyle(ControlStyles.UserPaint, True)
Me.SetStyle(ControlStyles.ResizeRedraw, True)
Me.SetStyle(ControlStyles.DoubleBuffer, True)
Me.SetStyle(ControlStyles.AllPaintingInWmPaint, True)

Than I have the following functions (updatebackground throws the
exception)

Private Sub mdiMain_BackgroundImageChanged(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
MyBase.BackgroundImageChanged
If Not m_IgnoreSetBackgroundImage Then
m_OriginalBackgroundImage = Me.BackgroundImage
UpdateBackGround()
End If
End Sub

Private Sub mdiMain_Resize(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Resize
If Not (m_MDIClient Is Nothing) Then UpdateBackGround()
End Sub

Private Sub UpdateBackGround()
If Not (m_OriginalBackgroundImage Is Nothing) Then
If m_MDIClient.Width > 0 And m_MDIClient.Height > 0 Then
Dim b As Bitmap = New Bitmap(m_MDIClient.Width,
m_MDIClient.Height)
Dim g As Graphics = Me.CreateGraphics.FromImage(b)
g.Clear(Me.BackColor)
g.DrawImage(m_OriginalBackgroundImage, New
Point((m_MDIClient.Width - m_OriginalBackgroundImage.Width) / 2,
(m_MDIClient.Height - m_OriginalBackgroundImage.Height) / 2))
m_IgnoreSetBackgroundImage = True
Me.BackgroundImage = b
m_IgnoreSetBackgroundImage = False
End If
End If
End Sub


If anyone has any ideas that would be great, thanks!

-Ivan
 
Ivan,

Move your code from the Form's constructor to form_Load and it will work fine.
During the constructor call, MDIClient may not be available.

Hope this helps!
Bharat Patel
Microsoft, Visual Basic .NET

This posting is provided "AS IS" with no warranties, and confers no rights.
Please reply to newsgroups only. Thanks.
 
No luck, same error. It seems that for some reason MdiClient is still
set to Nothing. When I debug through it assigns MdiClient to a
reference but than it doesn't have that value when updatebackground is
called.

-Ivan
 
Ivan,

Modify the first line in UpdateBackGround method as follows:

If Not (m_OriginalBackgroundImage Is Nothing) AndAlso Not (m_MDIClient Is
Nothing) Then

This will work. This method is being called before the form is loaded during
the call to constructor. So it will work fine after the form is loaded.


Hope this helps!
Bharat Patel
Microsoft, Visual Basic .NET

This posting is provided "AS IS" with no warranties, and confers no rights.
Please reply to newsgroups only. Thanks.
 
That fixed the problem but now I have another question.

Using the same code, is there a way to stretch my background image so
that it will always fill the entire screen. If the mdi form is reduced
so it is smaller than the image size than it works but when the MDI
Client area is larger than the image than it does not work. Can I have
this function work so the image is stretched to always fill the entire
MDIClient Area?

-Ivan
 
Hello, Ivan:

You have declared m_MDIClient, but you have not assigned anything yet (at least in the code you have showed us).
Try the following:
'\\\
Private m_MDIClient As New MdiClient
'///

I suppose the code is in the parent form constructor...

Regards.


"Ivan Weiss" <[email protected]> escribió en el mensaje | Hey all, I am using the following code to attempt to draw an image on
| the MdiClient Area thanks to some help from another post.
|
| However, the line If m_MDIClient.Width > 0 And m_MDIClient.Height > 0
| Then is throwing an exception saying m_MDIClient is a null reference.
|
| I have the following variables at the top of the form:
|
| Private m_MDIClient As MdiClient
| Private m_OriginalBackgroundImage As Image
| Private m_IgnoreSetBackgroundImage As Boolean
|
| Here is the code in my form constructor:
|
| Dim ctl As Control
|
| For Each ctl In Controls
| If TypeOf ctl Is MdiClient Then
| m_MDIClient = ctl
| m_MDIClient.BackColor = Me.BackColor
| Exit For
| End If
| Next
|
| In the mdi load I have:
|
| Me.SetStyle(ControlStyles.UserPaint, True)
| Me.SetStyle(ControlStyles.ResizeRedraw, True)
| Me.SetStyle(ControlStyles.DoubleBuffer, True)
| Me.SetStyle(ControlStyles.AllPaintingInWmPaint, True)
|
| Than I have the following functions (updatebackground throws the
| exception)
|
| Private Sub mdiMain_BackgroundImageChanged(ByVal sender As
| System.Object, ByVal e As System.EventArgs) Handles
| MyBase.BackgroundImageChanged
| If Not m_IgnoreSetBackgroundImage Then
| m_OriginalBackgroundImage = Me.BackgroundImage
| UpdateBackGround()
| End If
| End Sub
|
| Private Sub mdiMain_Resize(ByVal sender As System.Object, ByVal e As
| System.EventArgs) Handles MyBase.Resize
| If Not (m_MDIClient Is Nothing) Then UpdateBackGround()
| End Sub
|
| Private Sub UpdateBackGround()
| If Not (m_OriginalBackgroundImage Is Nothing) Then
| If m_MDIClient.Width > 0 And m_MDIClient.Height > 0 Then
| Dim b As Bitmap = New Bitmap(m_MDIClient.Width,
| m_MDIClient.Height)
| Dim g As Graphics = Me.CreateGraphics.FromImage(b)
| g.Clear(Me.BackColor)
| g.DrawImage(m_OriginalBackgroundImage, New
| Point((m_MDIClient.Width - m_OriginalBackgroundImage.Width) / 2,
| (m_MDIClient.Height - m_OriginalBackgroundImage.Height) / 2))
| m_IgnoreSetBackgroundImage = True
| Me.BackgroundImage = b
| m_IgnoreSetBackgroundImage = False
| End If
| End If
| End Sub
|
|
| If anyone has any ideas that would be great, thanks!
|
| -Ivan
|
|
| Don't just participate in USENET...get rewarded for it!
 
Yes, I set m_MdiClient to the existing MDIClient Control on the MDI
Container form. I posted that code earlier.

Is there a way of stretching the background image once I have the
control?

-Ivan
 
Hello, Ivan:

I'm not sure to understand.
You can use the form.clientsize or form.clientrectangle properties in the resize event to redraw the image with the appropiate dimensions.

Regards.


"Ivan Weiss" <[email protected]> escribió en el mensaje | Yes, I set m_MdiClient to the existing MDIClient Control on the MDI
| Container form. I posted that code earlier.
|
| Is there a way of stretching the background image once I have the
| control?
|
| -Ivan
 
Yes that is what my code is currently doing if you look at my previous
posts. However, if the MDI Client Size is larger than the image I simply
get a solid color with the image centered. Can I stretch the actual
image so that it actually fills the entire screen all of the time?

-Ivan
 
Ivan,

Change your DrawImage method call as follows:

g.DrawImage(m_OriginalBackgroundImage, m_MDIClient.ClientRectangle)

This will fill the whole client area.

Hope this helps!
Bharat Patel
Microsoft, Visual Basic .NET

This posting is provided "AS IS" with no warranties, and confers no rights.
Please reply to newsgroups only. Thanks.
 
THANK YOU SO MUCH, I cant even tell you how long I have been trying to
get that to work and it finally fills the entire image. You are my
hero, you have no idea lol!

-Ivan
 
Back
Top