RE: Memory corruption, problem closing multiple forms.

Tech-Archive recommends: Fix windows errors by optimizing your registry



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:

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:

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.
.


Quantcast