how to avoid creating same form objects

Hello Experts,
I am busy with visual c# project on win mobile 2003 se.

I have a main form which sync with the server through web service and get the recent list of orders.

for this action I call another class syncDB and that class do the synchronization and writes the records locally.

The process is going fine.

When I want to go to the detail form to see the details I do the main form hide and show the detail form like bellow.

// MainForm.cs

EKS_comsy.detailFrom df=new detailForm(orderNr);
df.Show();
this.Hide();

then if i want to come back to the main form again  i say in the datail form
//detailForm.cs

EKS_comsy.MainForm mf=new MainForm();
mf.Show();
this.Close();

Here i am creating a new object of the main form it initalize all the elements before it come there.  How can i avoid this problem and call the form that i have already. The way i use the code now makes the preformence of the program very low.

Can anyone help me to solve this issue.

thanks in advance.
Ludo_DirckxAsked:
Who is Participating?
 
AlexFMCommented:
df.Closed += new EventHandler(df_Closed);

void df_Closed(object sender, EventArgs e)
{
     this.Show();
}
0
 
AlexFMCommented:
EKS_comsy.detailFrom df=new detailForm(orderNr);
df.FormClosed += new FormClosedEventHandler(df_FormClosed);
this.Hide();
df.Show();


void df_FormClosed(object sender, FormClosedEventArgs e)
{
     this.Show();
}

For C# version before 8.0 use Closed event instead of FormClosed.
0
 
Ludo_DirckxAuthor Commented:
I have to use Closed then
should i use it like
df.Closed += new FormClosedEventHandler(df_FormClosed);

is it right
0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

 
Ludo_DirckxAuthor Commented:
The code you wrot is working fine.
Now in the detailForm should i remove the new main form command.

EKS_comsy.MainForm mf=new MainForm();
mf.Show();


i removed the command above in the detailForm.
When i click the  back to main form button the program just quit itself.

What went wrong.
0
 
AlexFMCommented:
You need to remove all code related to main form from details form. If details form is closed by button, use only
this.Close();

Main form waits for Closed event and shows itself again.
0
 
Ludo_DirckxAuthor Commented:
I think i made it.
I had put it in the right place.
It was my mistake.
thanks a lot.
0
 
Ludo_DirckxAuthor Commented:
hi AlexFM,
I got one more question.
How can i refresh the mainform when i show it again
0
 
AlexFMCommented:
I don't know what is "Refresh" in your program, this is application-specific. In any case, you can add this code to df_Closed function, after this.Show line.
0
 
Ludo_DirckxAuthor Commented:
No it is ok.
I have a timere wihich refreh the form and every 2 minutes i refresh the mainForm to whow the new records.
It is ok.
It was not related to the df_Closed.


thanks.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.