Syste.NullReferenceException [英] Syste.NullReferenceException

查看:66
本文介绍了Syste.NullReferenceException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿所有,我正在使用以下代码尝试在另一个帖子的帮助下在MdiClient区域上绘制图像

.


但是,行如果m_MDIClient.Width> 0和m_MDIClient.Height> 0

然后抛出一个异常,说m_MDIClient是一个空引用。


我在表单的顶部有以下变量:


私有m_MDIClient作为MdiClient

私有m_OriginalBackgroundImage作为图像

私有m_IgnoreSetBackgroundImage作为布尔值


这是我的表单构造函数中的代码:


Dim ctl作为控件


每个控件中的ctl

如果是TypeOf ctl是MdiClient然后

m_MDIClient = ctl

m_MDIClient.BackColor = Me.BackColor

退出

结束如果

下一页


在我的mdi载入中:


Me.SetStyle(ControlStyles.UserPaint,True)

Me.SetStyle(ControlStyles.ResizeRedraw,True)

Me.SetStyle(ControlStyles.DoubleBuffer,True)

Me.SetStyle(ControlStyles.AllPaintingInWmPaint ,是的)


比我有以下功能(updatebackground抛出

例外)


Private Sub mdiMain_BackgroundImageChanged(ByVal sender As

System.Object,ByVal e As System.EventArgs)处理

MyBase.BackgroundImageChanged

如果不是m_IgnoreSetBackgroundImage那么

m_OriginalBackgroundImage = Me.BackgroundImage

UpdateBackGround()

结束如果

结束子


私有子mdiMain_Resize(ByVal发送者为System.Object,ByVal e As

System.EventArgs )处理MyBase.Resize

如果不是(m_MDIClient什么都没有)那么UpdateBackGround()

结束子


Private Sub UpdateBackGround( )

如果不是(m_OriginalBackgroundImage什么都不是)那么

如果m_MDIClient.Width> 0和m_MDIClient.Height> 0然后

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

结束如果

结束如果

结束子

如果有人有任何想法会很棒,谢谢!


-Ivan


***通过Developersdex发送 http://www.developersdex.com ***

不要刚参加USENET ......获得奖励!

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

*** Sent via Developersdex http://www.developersdex.com ***
Don''t just participate in USENET...get rewarded for it!

推荐答案

Ivan,


移动你的代码来自表格'构造函数到form_Load,它会正常工作。

在构造函数调用期间,MDIClient可能无法使用。


希望这会有所帮助!

Bharat Patel

Microsoft,Visual Basic .NET


此帖子按原样提供。没有保证,也没有授予任何权利。

请仅回复新闻组。谢谢。

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.


没有运气,同样的错误。似乎由于某种原因,MdiClient仍然设置为Nothing。当我通过调试时,它将MdiClient分配给

引用,但是当updatebackground被调用时,它没有那个值。


-Ivan


***通过开发人员指南 http发送://www.developersdex.com ***

不要只参加USENET ......获得奖励!
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

*** Sent via Developersdex http://www.developersdex.com ***
Don''t just participate in USENET...get rewarded for it!

Ivan,


修改UpdateBackGround方法中的第一行,如下所示:


如果不是(m_OriginalBackgroundImage什么都不是),也不是( m_MDIClient是

没什么)那么


这样可行。在

调用构造函数期间加载表单之前调用此方法。因此在表单加载后它会正常工作。

希望这会有所帮助!

Bharat Patel

Microsoft,Visual Basic .NET

此帖子按原样提供。没有保证,也没有授予任何权利。

请仅回复新闻组。谢谢。

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.


这篇关于Syste.NullReferenceException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆