内存损坏,关闭多个表单的问题。 [英] Memory corruption, problem closing multiple forms.

查看:81
本文介绍了内存损坏,关闭多个表单的问题。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用VS2005和.net 2.0。我正在创建一个包含3

表格的应用程序。我希望允许用户使用表单向前和向后移动,并且

保留用户输入的数据。我以为我会使不活动的表单

看不见但这会在用户关闭时产生内存损坏问题

form2或form3而不是formMain。


我的主窗体有一个Next按钮,使主窗体不可见,

启动一个新窗体,我称之为form2。表单2有一个Previous和Next

按钮。上一个按钮会使主窗体再次可见。

下一步按钮将调用form3。这两个按钮都会使form2

隐形。 form3有一个前一个按钮,可以返回到form2 vislble和

让它自己隐形。用户可以使用这3个表单前进和后退。


实际上,我在formMain和form2中有一个布尔变量来表示

form2或form3是否已经创建。由于我的应用程序允许他们返回

并转发这3个表单我只想让form2和form3可见,如果

他们已经创建了。


我已经尝试在form2和form3的结束事件中使用代码来尝试

关闭它的调用形式,即form2或formMain。但这不起作用,你知道,当我的代码将调用表单设置为visible = false时,它会自动调用表单的Closing事件。所以,无论我放在那里的是什么

代码,当我只是将表单设置为

时,它将被执行。


我的问题是,如果用户在form2或

form3关闭应用程序,那么如何确保所有表单都已关闭?编码关闭

主窗体会自动关闭所有chidren表格中的

订单吗?如果没有,我该如何解决这个问题呢?因为现在它在我的应用程序中造成了内存更正。
内存更正。谢谢。

-

谢谢。

Hi, I''m using VS2005 and .net 2.0. I''m creating an application that has 3
forms. I want allow users to move forward and backward with the forms and
retain the data users have entered. I thought I''ll make the inactive forms
invisible but this is creating a memory corruption problem when user close
the form2 or form3 and not the formMain.

My main form has a Next button which makes the main form invisible and
starts a new form which I''ll call form2. The form 2 has a Previous and Next
button. The previous button would make the main form visiable again. The
next button would invoke a form3. both of these buttons would make form2
invisible. The form3 has a previous button to go back to form2 vislble and
make itself invisible. User can go forward and backward with these 3 forms.

Actually, I have a boolean variable in formMain and form2 to indicate if
form2 or form3 has been created. Since my application allows them to go back
and forward with these 3 forms I only want to make form2 and form3 visible if
they''re already created.

I already tried have code in the closing event of form2 and form3 to try to
close it''s calling form, namely form2 or formMain. But this doesn''t work,
you see, when my code sets the calling form to visible=false, it
automatically callsing the form''s Closing event. So, doesn''t matter what
code I put there, it will get executed when I''m just setting the form to be
not visible.

My question is, if the user is closing the application either at form2 or
form3 then how do I make sure all forms are closed? Would coding to close
the main form automatically close all the chidren form in the approperate
order? If not, how can I address this? Becuase right now it''s causing
memory correuption in my application. Thanks.
--
Thanks.

推荐答案

2007年3月27日星期二23 :16:03-0700,Pucca

< Pu *** @ discuss.microsoft.comwrote:
On Tue, 27 Mar 2007 23:16:03 -0700, Pucca
<Pu***@discussions.microsoft.comwrote:

[...]

我的问题是,如果用户在form2或

form3关闭应用程序,那么如何确保所有表单都关闭?编码为

关闭主窗体会自动关闭

批准订单中的所有孩子吗?如果没有,我该如何解决这个问题呢?现在,因为它会导致我的应用程序内存更正。谢谢。
[...]
My question is, if the user is closing the application either at form2 or
form3 then how do I make sure all forms are closed? Would coding to
close the main form automatically close all the chidren form in the
approperate order? If not, how can I address this? Becuase right now
it''s causing memory correuption in my application. Thanks.



一些想法:


从你的帖子中不清楚内存损坏是什么问题你是

有。你提到了一个,但除此之外,什么都没有。


我不知道可见/关闭问题与记忆有什么关系

腐败问题。在任何情况下都不应该发生内存损坏,不管你用表单做什么都没关系,并且没有任何理由来改变你的b $ b可见属性或关闭表单应该本身

损坏内存。


我也不明白为什么你的Closing事件被解雇了所有你正在做

将Visible属性设置为false。隐藏表格与关闭它时不一样

,并且结束事件将被解雇似乎是正确的b $ b $(请注意,反之亦然)不一定是真的...例如,当

a表单显示为模态对话框时,单击

表单上的关闭按钮会导致表单隐藏而不是实际处理)


另请注意,从.NET 2.0开始,您应该使用FormClosing事件,

而不是Closing事件。就此而言,在你的情况下,

在我看来,封闭事件实际上更合适。可能没有实际的差别,但是这对我来说似乎是一个更好的选择

,因为在此时,FormClosing事件是,不能保证表格实际上会被关闭。


基本上,你的帖子中有一点没有加起来,至少在

首次阅读。如果你能稍微澄清一下你的情况,那就是

有用。


Pete

Some thoughts:

It''s not clear from your post what "memory corruption" problem you''re
having. You mention having one, but other than that, there''s nothing.

I don''t see how the visible/closing issue is related to the memory
corruption issue. Memory corruption shouldn''t happen in any case, no
matter what you''re doing with the forms, and there''s not any reason that
changing the visible property or closing a form should in and of itself
corrupt memory.

I also don''t see why your Closing event gets fired when all you''re doing
is setting the Visible property to false. Hiding the form isn''t the same
as closing it, and it doesn''t seem right that the Closing event would get
fired (note that the converse is not necessarily true...for example, when
a form is displayed as a modal dialog, clicking the close button on the
form results in the form being hidden rather than actually disposed).

Also note that as of .NET 2.0, you should use the FormClosing event,
rather than the Closing event. For that matter, in your situation it
seems to me that the Closed event is actually more appropriate. There''s
probably no practical difference, but it just seems like a better choice
to me here, since at the point in time that the FormClosing event is
received, it is not guaranteed that the form will actually be closed.

Basically, there''s a bit in your post that doesn''t add up, at least at
first read. If you could clarify your situation a bit, that would be
helpful.

Pete





我不确定你的设计是否是最简洁的方式来实现你想要的东西 - 这个也许还可以解释为什么你在你描述的问题中运行

...我可以建议其他三种方式

这可能会使编码更多一些直截了当:


(1)轻量级选项可以是一个表单,但有三个单独的

groupBoxes,每个'模拟'一个表单,每个groupBox包含你要收集数据的

控件。在表单上放一个close,next和previous

按钮,当用户点击下一个时/ previous,hide / unhide

groupBoxes。你甚至可以创建一个用enum

代码+ groupBox填充的排序列表,并在用户

点击下一个时间后循环到第一个groupbox''form''第三个groupbox,或者向后循环到最后一个groupbox

''form''当用户点击前一个第一个条目时..这是最容易实现的
,然而,可能是最不容易改变的。


例如,您的下一个/上一个按钮点击事件可能看起来像

喜欢(未编译/测试) :


private void btnNext_Click(object sender,EventArgs e)

{

((GroupBox)(m_groupBoxList [m_currentFormEnum]) ).Visible = false;

m_currentFormEnum ++;

if(m_currentFormEnum == FormEnum.End)

m_currentFormEnum = FormEnum.Form1;

((GroupBox)(m_groupBoxList [m_currentFormEnum]))。Visible = true;

}


private void btnPrevious_Click(object sender, EventArgs e)

{

((GroupBox)(m_groupBoxList [m_currentFormEnum]))。Visible = false;

m_currentFormEnum - ;

if(m_currentFormEnum == FormEnum.Begin)

m_currentFormEnum = FormEnum.Form3;

((GroupBox)(m_groupBoxList [m_currentFormEnum]))。可见= true;

}

(2)另一种选择,可能是将每个''形式''表示为自定义用户

控制,每个都包含用于捕获所需数据的本机.NET控件。用户控件可以具有公共属性,用于公开用户输入的数据。同样,单个表单将托管三个实例化的用户控件,并且当用户点击下一个/上一个时,您将隐藏/取消隐藏您的自定义用户

控件。这是一个更多的工作,但是你的新用户控件可以在其他地方重复使用,并且还可以与托管表单(这是一个很好的分离)一个好东西)。


(3)第三个更重量级的选项可能是使用类似

来自Microsoft的用户界面进程应用程序块你要点什么来做你想要的东西,还有更多!您可以从以下网址找到关于UIPAB的



(1) http://msdn2.microsoft.com/en-us/library/ms979217.aspx

(2) http://www.codeproject.com/dotnet/UIPAB1.asp


不知道为什么,但有些东西告诉我还有很多其他的可能性

;-) ..希望这会对你有所帮助...


亲切问候,

Patrick

-

Patrick Simpe-Asante

MCAD,MSCD.Net

" Pucca"写道:
Hi ,

I''m not sure whether design-wise what you have is the cleanest way to
achieve what you''re looking for - this might also explain why you''re running
into the kind of issues that you describe... I can suggest three other ways
that might make coding this a little more straightforward:

(1) A lightweight option could be to have one form, but with three separate
groupBoxes that ''simulate'' a form each, each groupBox of which contains the
controls that you want to collect data in. Put a close, next and previous
button on the form, and as the user clicks through next/previous, hide/unhide
the groupBoxes. You could even create a sorted list populated with an enum
code + groupBox, and cycle over to the first groupbox ''form'' when the user
clicks next past the third groupbox, or cycles backwards to the last groupbox
''form'' when the user clicks previous past the first entry.. This is the most
simple to implement, however is probably the least amenable to change.

As an example, your next/previous button click events might look something
like (not compiled/tested):

private void btnNext_Click(object sender, EventArgs e)
{
((GroupBox)(m_groupBoxList[m_currentFormEnum])).Visible = false;
m_currentFormEnum++;
if (m_currentFormEnum == FormEnum.End)
m_currentFormEnum = FormEnum.Form1;
((GroupBox)(m_groupBoxList[m_currentFormEnum])).Visible = true;
}

private void btnPrevious_Click(object sender, EventArgs e)
{
((GroupBox)(m_groupBoxList[m_currentFormEnum])).Visible = false;
m_currentFormEnum--;
if (m_currentFormEnum == FormEnum.Begin)
m_currentFormEnum = FormEnum.Form3;
((GroupBox)(m_groupBoxList[m_currentFormEnum])).Visible = true;
}
(2) Another option, might be to represent each ''form'' as a custom user
control, which each contain the native .NET controls used to capture your
required data. The user controls could have public properties that expose the
data that the user entersin them. Again, a single form will host the three
instantiated user controls, and you would have hide/unhide your custom user
controls as the user clicks through next/previous. This is a little bit more
work, however your new user controls will be re-useable elsewhere, and also
would have a nice separation from the hosting Form (which is a good thing).

(3) A third much more heavyweight option could be to use something like the
User Interface Process Application Block from Microsoft which enables you to
do what you''re looking for, along with a whole lot more! You can find out
about the UIPAB from:
(1) http://msdn2.microsoft.com/en-us/library/ms979217.aspx
(2) http://www.codeproject.com/dotnet/UIPAB1.asp

Not sure why, but something tells me that there are many other possibilities
as well ;-).. Hope this helps you though...

Kind regards,
Patrick
--
Patrick Simpe-Asante
MCAD, MSCD.Net
"Pucca" wrote:

我正在使用VS2005和.net 2.0。我正在创建一个包含3

表格的应用程序。我希望允许用户使用表单向前和向后移动,并且

保留用户输入的数据。我以为我会使不活动的表单

看不见但这会在用户关闭时产生内存损坏问题

form2或form3而不是formMain。


我的主窗体有一个Next按钮,使主窗体不可见,

启动一个新窗体,我称之为form2。表单2有一个Previous和Next

按钮。上一个按钮会使主窗体再次可见。

下一步按钮将调用form3。这两个按钮都会使form2

隐形。 form3有一个前一个按钮,可以返回到form2 vislble和

让它自己隐形。用户可以使用这3个表单前进和后退。


实际上,我在formMain和form2中有一个布尔变量来表示

form2或form3是否已经创建。由于我的应用程序允许他们返回

并转发这3个表单我只想让form2和form3可见,如果

他们已经创建了。


我已经尝试在form2和form3的结束事件中使用代码来尝试

关闭它的调用形式,即form2或formMain。但这不起作用,你知道,当我的代码将调用表单设置为visible = false时,它会自动调用表单的Closing事件。所以,无论我放在那里的是什么

代码,当我只是将表单设置为

时,它将被执行。


我的问题是,如果用户在form2或

form3关闭应用程序,那么如何确保所有表单都已关闭?编码关闭

主窗体会自动关闭所有chidren表格中的

订单吗?如果没有,我该如何解决这个问题呢?因为现在它在我的应用程序中造成了内存更正。
内存更正。谢谢。

-

谢谢。
Hi, I''m using VS2005 and .net 2.0. I''m creating an application that has 3
forms. I want allow users to move forward and backward with the forms and
retain the data users have entered. I thought I''ll make the inactive forms
invisible but this is creating a memory corruption problem when user close
the form2 or form3 and not the formMain.

My main form has a Next button which makes the main form invisible and
starts a new form which I''ll call form2. The form 2 has a Previous and Next
button. The previous button would make the main form visiable again. The
next button would invoke a form3. both of these buttons would make form2
invisible. The form3 has a previous button to go back to form2 vislble and
make itself invisible. User can go forward and backward with these 3 forms.

Actually, I have a boolean variable in formMain and form2 to indicate if
form2 or form3 has been created. Since my application allows them to go back
and forward with these 3 forms I only want to make form2 and form3 visible if
they''re already created.

I already tried have code in the closing event of form2 and form3 to try to
close it''s calling form, namely form2 or formMain. But this doesn''t work,
you see, when my code sets the calling form to visible=false, it
automatically callsing the form''s Closing event. So, doesn''t matter what
code I put there, it will get executed when I''m just setting the form to be
not visible.

My question is, if the user is closing the application either at form2 or
form3 then how do I make sure all forms are closed? Would coding to close
the main form automatically close all the chidren form in the approperate
order? If not, how can I address this? Becuase right now it''s causing
memory correuption in my application. Thanks.
--
Thanks.


嗨Pucca,


忘了提及 - 在下面的第一个选项中,组框将是

叠加在一起......以防万一从我的帖子中看不出来......


问候,

Patrick


-

Patrick Simpe-Asante

MCAD,MSCD.Net

" ; Patrick Simpe-Asante"写道:
Hi Pucca,

Forgot to mention - in the first option below, the group boxes would be
overlaid over each other... Just in case that wasn''t obvious from my post...

regards,
Patrick

--
Patrick Simpe-Asante
MCAD, MSCD.Net
"Patrick Simpe-Asante" wrote:

嗨Pucca,


我不确定你的设计是否是最干净的方式要实现你想要的东西 - 这也可以解释为什么你在你描述的问题中运行

...我可以建议其他三种方式

可能会使编码变得更加简单:


(1)轻量级选项可以是一个表单,但有三个单独的

groupBoxes每个'模拟'一个表单,每个groupBox包含你要收集数据的

控件。放一个close,next和previous <表单上的
按钮,当用户点击下一个/上一个时,隐藏/取消隐藏

groupBoxes。你甚至可以创建一个用enum

代码+ groupBox填充的排序列表,并在用户

点击下一个时间后循环到第一个groupbox''form''第三个groupbox,或者向后循环到最后一个groupbox

''form''当用户点击前一个第一个条目时..这是最容易实现的
,然而,可能是最不容易改变的。


例如,您的下一个/上一个按钮点击事件可能看起来像

喜欢(未编译/测试) :


private void btnNext_Click(object sender,EventArgs e)

{

((GroupBox)(m_groupBoxList [m_currentFormEnum]) ).Visible = false;

m_currentFormEnum ++;

if(m_currentFormEnum == FormEnum.End)

m_currentFormEnum = FormEnum.Form1;

((GroupBox)(m_groupBoxList [m_currentFormEnum]))。Visible = true;

}


private void btnPrevious_Click(object sender,EventArgs e)

{

(( GroupBox)(m_groupBoxList [m_currentFormEnum]))。Visible = false;

m_currentFormEnum--;

if(m_currentFormEnum == FormEnum.Begin)

m_currentFormEnum = FormEnum.Form3;

((GroupBox)(m_groupBoxList [m_currentFormEnum]))。Visible = true;

}


(2)另一种选择,可能是将每个''形式''表示为自定义用户

控件,每个控件都包含用于捕获你的原生.NET控件

所需数据。用户控件可以具有公共属性,用于公开用户输入的数据。同样,单个表单将托管三个实例化的用户控件,并且当用户点击下一个/上一个时,您将隐藏/取消隐藏您的自定义用户

控件。这是一个更多的工作,但是你的新用户控件可以在其他地方重复使用,并且还可以与托管表单(这是一个很好的分离)一个好东西)。


(3)第三个更重量级的选项可能是使用类似

来自Microsoft的用户界面进程应用程序块你要点什么来做你想要的东西,还有更多!您可以从以下网址找到关于UIPAB的



(1) http://msdn2.microsoft.com/en-us/library/ms979217.aspx

(2) http://www.codeproject.com/dotnet/UIPAB1.asp


不知道为什么,但有些东西告诉我还有很多其他的可能性

;-) ..希望这会对你有所帮助...


亲切问候,

Patrick

-

Patrick Simpe-Asante

MCAD,MSCD.Net


" Pucca"写道:
Hi Pucca,

I''m not sure whether design-wise what you have is the cleanest way to
achieve what you''re looking for - this might also explain why you''re running
into the kind of issues that you describe... I can suggest three other ways
that might make coding this a little more straightforward:

(1) A lightweight option could be to have one form, but with three separate
groupBoxes that ''simulate'' a form each, each groupBox of which contains the
controls that you want to collect data in. Put a close, next and previous
button on the form, and as the user clicks through next/previous, hide/unhide
the groupBoxes. You could even create a sorted list populated with an enum
code + groupBox, and cycle over to the first groupbox ''form'' when the user
clicks next past the third groupbox, or cycles backwards to the last groupbox
''form'' when the user clicks previous past the first entry.. This is the most
simple to implement, however is probably the least amenable to change.

As an example, your next/previous button click events might look something
like (not compiled/tested):

private void btnNext_Click(object sender, EventArgs e)
{
((GroupBox)(m_groupBoxList[m_currentFormEnum])).Visible = false;
m_currentFormEnum++;
if (m_currentFormEnum == FormEnum.End)
m_currentFormEnum = FormEnum.Form1;
((GroupBox)(m_groupBoxList[m_currentFormEnum])).Visible = true;
}

private void btnPrevious_Click(object sender, EventArgs e)
{
((GroupBox)(m_groupBoxList[m_currentFormEnum])).Visible = false;
m_currentFormEnum--;
if (m_currentFormEnum == FormEnum.Begin)
m_currentFormEnum = FormEnum.Form3;
((GroupBox)(m_groupBoxList[m_currentFormEnum])).Visible = true;
}
(2) Another option, might be to represent each ''form'' as a custom user
control, which each contain the native .NET controls used to capture your
required data. The user controls could have public properties that expose the
data that the user entersin them. Again, a single form will host the three
instantiated user controls, and you would have hide/unhide your custom user
controls as the user clicks through next/previous. This is a little bit more
work, however your new user controls will be re-useable elsewhere, and also
would have a nice separation from the hosting Form (which is a good thing).

(3) A third much more heavyweight option could be to use something like the
User Interface Process Application Block from Microsoft which enables you to
do what you''re looking for, along with a whole lot more! You can find out
about the UIPAB from:
(1) http://msdn2.microsoft.com/en-us/library/ms979217.aspx
(2) http://www.codeproject.com/dotnet/UIPAB1.asp

Not sure why, but something tells me that there are many other possibilities
as well ;-).. Hope this helps you though...

Kind regards,
Patrick
--
Patrick Simpe-Asante
MCAD, MSCD.Net
"Pucca" wrote:

我正在使用VS2005和.net 2.0。我正在创建一个包含3

表格的应用程序。我希望允许用户使用表单向前和向后移动,并且

保留用户输入的数据。我以为我会使不活动的表单

看不见但这会在用户关闭时产生内存损坏问题

form2或form3而不是formMain。


我的主窗体有一个Next按钮,使主窗体不可见,

启动一个新窗体,我称之为form2。表单2有一个Previous和Next

按钮。上一个按钮会使主窗体再次可见。

下一步按钮将调用form3。这两个按钮都会使form2

隐形。 form3有一个前一个按钮,可以返回到form2 vislble和

让它自己隐形。用户可以使用这3个表单前进和后退。


实际上,我在formMain和form2中有一个布尔变量来表示

form2或form3是否已经创建。由于我的应用程序允许他们返回

并转发这3个表单我只想让form2和form3可见,如果

他们已经创建了。


我已经尝试在form2和form3的结束事件中使用代码来尝试

关闭它的调用形式,即form2或formMain。但这不起作用,你知道,当我的代码将调用表单设置为visible = false时,它会自动调用表单的Closing事件。所以,无论我放在那里的是什么

代码,当我只是将表单设置为

时,它将被执行。


我的问题是,如果用户在form2或

form3关闭应用程序,那么如何确保所有表单都已关闭?编码关闭

主窗体会自动关闭所有chidren表格中的

订单吗?如果没有,我该如何解决这个问题呢?因为现在它在我的应用程序中造成了内存更正。
内存更正。谢谢。

-

谢谢。
Hi, I''m using VS2005 and .net 2.0. I''m creating an application that has 3
forms. I want allow users to move forward and backward with the forms and
retain the data users have entered. I thought I''ll make the inactive forms
invisible but this is creating a memory corruption problem when user close
the form2 or form3 and not the formMain.

My main form has a Next button which makes the main form invisible and
starts a new form which I''ll call form2. The form 2 has a Previous and Next
button. The previous button would make the main form visiable again. The
next button would invoke a form3. both of these buttons would make form2
invisible. The form3 has a previous button to go back to form2 vislble and
make itself invisible. User can go forward and backward with these 3 forms.

Actually, I have a boolean variable in formMain and form2 to indicate if
form2 or form3 has been created. Since my application allows them to go back
and forward with these 3 forms I only want to make form2 and form3 visible if
they''re already created.

I already tried have code in the closing event of form2 and form3 to try to
close it''s calling form, namely form2 or formMain. But this doesn''t work,
you see, when my code sets the calling form to visible=false, it
automatically callsing the form''s Closing event. So, doesn''t matter what
code I put there, it will get executed when I''m just setting the form to be
not visible.

My question is, if the user is closing the application either at form2 or
form3 then how do I make sure all forms are closed? Would coding to close
the main form automatically close all the chidren form in the approperate
order? If not, how can I address this? Becuase right now it''s causing
memory correuption in my application. Thanks.
--
Thanks.


这篇关于内存损坏,关闭多个表单的问题。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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