从C#WinForms App中的用户控件调用父表单时出现Null Reference Exception [英] Null Reference Exception when calling parent form from user control in C# WinForms App

查看:107
本文介绍了从C#WinForms App中的用户控件调用父表单时出现Null Reference Exception的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在C#WinForms应用程序中遇到一个特殊问题,其中在父窗体中使用了服务器用户控件。

以下是用户控件问题的代码:



       ; 
private
void LoadMCQuestionScreen( clsQuestion
MCQuestion)



        ;
{


           
ucMultiChoiceQuestion MCQuestionScreen =

< span style ="font-size:9.5pt; font-family:Consolas; color:#2B91AF; background:white"> ucMultiChoiceQuestion (MCQuestion,Players,glbintQuestionIndex,  glbblTransYesOrNo, lstTransList.ElementAt( glbintQuestionIndex));


 



           
MCQuestionScreen.Parent =
this ;



           
MCQuestionScreen.Show();


       
}


这是发生崩溃的用户控件的代码块:



       
private
void QuestionIsAnswered( bool
TimeExpired)



       
{


           
if (TimeExpired ==
true < span style ="font-size:9.5pt; font-family:Consolas; color:black; background:white">)


               
glbblAnsweredCorrectly =
false ;



 



               
frmGame form1 =( frmGame this 。父母;



               
form1 .LoadAnswerResponseScreen(glbblAnsweredCorrectly,glbstrAnsweringPlayer);



               
spTickTock.Stop( );



               
MCPlayer.Dispose( );



               
tmrMCTimer.Dispose( );



               
if (pbQuestionImage.Image!=
null



                   
pbQuestionImage.Image.Dispose();



               
this 。Dispose();


       
}


我在
粗体 - "对象引用未设置为对象实例"的行上面出现Null引用异常。在该行上,form1为null。发生崩溃时,此方法和用户控制MCQuestionScreen已经处理掉。


为了防止被处理的代码和用户控制再次尝试运行,我认为这是原因,我在" LoadMCQuestionScreen "
方法:



           
foreach UserControl
uctrl
in
this 。Controls)


               
uctrl.Dispose( );


我希望小片段会杀死该用户控件的任何第二个实例。不幸的是,该代码不起作用,2天后我仍然无法确定问题的实际
来源此外,如果此问题出现在错误的论坛上,请在回复中提供一个链接,以便我知道在哪里找到它。


 


 

解决方案

我不确定我明白你在那里做了什么,但我有一个猜测:
$


在表格/用户控制后仍然调用QuestionIsAnswered(False)已从Visible Forms中删除。反过来意味着this.Parent确实会返回null - 因为它已经没有父级了。



你需要正确停止关闭/隐藏控件之前的计时器。



计时器的确切行为在很大程度上取决于您使用的5个计时器以及它的引用位置。



调用Dispose只会在你正确覆盖它以停止计时器时有所帮助。你有责任将Dispose调用转发给包含的类。

I have a peculiar problem in a C# WinForms app in which sever user controls are used within a parent Form.

Here’s the code for the problem user control:

        private void LoadMCQuestionScreen(clsQuestion MCQuestion)

        {

            ucMultiChoiceQuestion MCQuestionScreen = new ucMultiChoiceQuestion(MCQuestion, Players, glbintQuestionIndex, glbblTransYesOrNo,lstTransList.ElementAt(glbintQuestionIndex));

 

            MCQuestionScreen.Parent = this;

            MCQuestionScreen.Show();

        }

Here’s the block of code for that user control where the crash occurs:

        private void QuestionIsAnswered(bool TimeExpired)

        {

            if (TimeExpired == true)

                glbblAnsweredCorrectly = false;

 

                frmGame form1 = (frmGame)this.Parent;

                form1.LoadAnswerResponseScreen(glbblAnsweredCorrectly, glbstrAnsweringPlayer);

                spTickTock.Stop();

                MCPlayer.Dispose();

                tmrMCTimer.Dispose();

                if (pbQuestionImage.Image != null)

                    pbQuestionImage.Image.Dispose();

                this.Dispose();

        }

I get a Null Reference Exception on the line above in bold – "Object reference not set to an instance of an object." On that line, form1 is null. This method and the user control MCQuestionScreen will have already been disposed when the crash occurs.

In an attempt to prevent the disposed code and user control from trying to run a second time, which I assume to be the cause, I’ve tried this code at top of the "LoadMCQuestionScreen" method:

            foreach (UserControl uctrl in this.Controls)

                uctrl.Dispose();

I was hoping that little snippet would kill any second instance of that user control. Unfortunately, that code doesn’t work and after 2 days I still can’t pin down the actual source of the problem. Also, if this question is on the wrong forum, please provide a link in your response so I know where to find it.

 

 

解决方案

I am not sure I understand what you do there, but I have one guess:

QuestionIsAnswered(False) is still being called after the Form/User Control has been removed from the Visible Forms. Wich in turn would mean that this.Parent would indeed return null - because it has no parent anymore.

You need to properly stop the timer before you close/hide the control.

Exact behavior of the timer depends heavily on wich of the 5+ timers you used, and where it's reference is held.

Calling Dispose would only help if you had overriteen it properly to stop the timer. You are responsible to relay the Dispose calls to contained classes.


这篇关于从C#WinForms App中的用户控件调用父表单时出现Null Reference Exception的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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