Hide/Show form while retaining Info  
Author Message
Blkbird





PostPosted: Visual C# General, Hide/Show form while retaining Info Top

Is there a way to load info to a form from a file (ie text to labels,etc.), from that form call another and hide the first only to later unhide the first from an object on the second from.
PROGRAM FLOW:
1)Start up
2)Press a button info comes up
3)Press another button to a)hide form1 and b)show form2
4)Do stuff on form2
5)Press button on form2 to a)make form1 visible again and b)close form2
6)Form1 still has info loaded in step 2

I've got everything working but when I make form1.visible = false to make it disappear then make it re-appear through an object in the code for form2 all the info I loaded is now gone.

Can this be done

Thanks
Thomas


Visual C#7  
 
 
SvenC





PostPosted: Visual C# General, Hide/Show form while retaining Info Top

Hi Blkbird,

what is your code to make form1 visible again You should pass a reference of the form instance to your second form so that the second form uses that reference to make it visible again.

--
SvenC


 
 
Blkbird





PostPosted: Visual C# General, Hide/Show form while retaining Info Top

The code for making it visible again is as follows:
MenuForm ==form1
SalesForm ==form2


//Global
MenuForm menu = new MenuForm();

//someButton_click()in SalesForm
this.Close();
menu.Visible = true;


I understand about passing a reference but how do you do that.

Thanks

Thomas


 
 
SvenC





PostPosted: Visual C# General, Hide/Show form while retaining Info Top

Where did you define variable menu to make it global It must be a member of a class. If you want it to be a singleton you would need to make it a static class member.

--
SvenC


 
 
PJ. van de Sande





PostPosted: Visual C# General, Hide/Show form while retaining Info Top

You can use the Form.FormClosed event to get informed when a form is closed, if you receive this event just show your MenuForm again. So, in your MenuForm your code will look like this:


private void button1_Click(object sender, EventArgs e)
{
// Create form and wire event.
Form form2 = new Form();
form2.FormClosed += new FormClosedEventHandler(form2_FormClosed);
// Hide this menu form and show form2.
this.Hide();
form2.Show();
}
void form2_FormClosed(object sender, FormClosedEventArgs e)
{
// Form2 was closed, so show this one again.
this.Show();
}



 
 
Blkbird





PostPosted: Visual C# General, Hide/Show form while retaining Info Top

menu is defined right after the class declaration of the form2

public class LogIn : System.Windows.Forms.Form
{
MenuForm menu = new MenuForm();


I am using two different forms in a project, and going between two different *.cs files
Does that help
Thanks

Thomas

 
 
RizwanSharp





PostPosted: Visual C# General, Hide/Show form while retaining Info Top

The code for making it visible again is as follows:
MenuForm ==form1
SalesForm ==form2


//Global
MenuForm menu = new MenuForm();

//someButton_click()in SalesForm
this.Close();
menu.Visible = true;


I understand about passing a reference but how do you do that.

Thanks

Thomas

In the Code Above, Basicall you are not showing the form1 again but you are creating a new instance and showing the newly created instance except showing the old one again. So, Now you have 2 instances of form1 after doing this! 1 is hidden and the other is visible!

There are 2 Techniques to do what you want to do:

1) Modify the Contructor of SalesForm to accept an Argument type of Form (to pass the reference of MenuForm) like this:

public class SalesForm

{

private Form menuForm;

public SalesForm(Form menuForm)

{

this.menuForm = menuForm; // Set reference here

}

Override Closing Event of Form2 i.e SalesForm

and put this Code in that:

menu.Visible = true;

And now Whenever you Close the SalesForm it'll automatically show the original Form!

////////////////////

Second Solution:

Show the SalesForm from MenuForm using:

salesForm.Show(this); // Tell the parent of salesForm as MenuForm

salesForm.Show(); // Dont use this

Now again Override the Closing Event of Form and out this code in that:

this.ParentForm.Visible = true;

I hope both solutiosn will work like a Charm!

Best Regards,



 
 
Blkbird





PostPosted: Visual C# General, Hide/Show form while retaining Info Top

Aside from being totally confused and bewildered I am pretty much in the same boat I was in :-\.

Solution one seems much less confusing so I decided to do that here is how my code now reads. Without a bunch of the VS generated stuff:
MenuForm.cs
namespace StorePOS
{
public class MenuForm : System.Windows.Forms.Form
{

//VS GENERATED THISTHATS AND WHATNOTS
public MenuForm()
{

InitializeComponent();
}
//this button opens SalesForm.cs
private void uiPOSButton_Click(object sender, System.EventArgs e)
{
this.Visible = false;
SalesForm sales = new SalesForm(this);
sales.ShowDialog();
}
SalesForm.cs
namespace StorePOS
{
public class SalesForm : System.Windows.Forms.Form
{
//OBJECT DECLARATIONS
MenuForm menu = new MenuForm();
private System.ComponentModel.Container components = null;

public SalesForm(Form MenuForm)
{
InitializeComponent();
//I TOOK OUT THE THIS REF because VS gave an error on it.
MenuForm = MenuForm; // Set reference here
}
//this button closes SalesForm and is supposed to unhide MenuForm
//right now it still only loads an object while letting the orriginal just hide and wait :-(
private void uiMenuFormButton_Click(object sender, System.EventArgs e)
{
this.Close();
menu.Visible = true;
}


Thanks
Thomas

 
 
RizwanSharp





PostPosted: Visual C# General, Hide/Show form while retaining Info Top

Aside from being totally confused and bewildered I am pretty much in the same boat I was in :-\.

Solution one seems much less confusing so I decided to do that here is how my code now reads. Without a bunch of the VS generated stuff:
MenuForm.cs
namespace StorePOS
{
public class MenuForm : System.Windows.Forms.Form
{

//VS GENERATED THISTHATS AND WHATNOTS
public MenuForm()
{

InitializeComponent();
}
//this button opens SalesForm.cs
private void uiPOSButton_Click(object sender, System.EventArgs e)
{
this.Visible = false;
SalesForm sales = new SalesForm(this);
sales.ShowDialog();
}
SalesForm.cs
namespace StorePOS
{
public class SalesForm : System.Windows.Forms.Form
{
//OBJECT DECLARATIONS
MenuForm menu = new MenuForm();

MenuForm menu;
private System.ComponentModel.Container components = null;

public SalesForm(Form MenuForm)
{
InitializeComponent();
//I TOOK OUT THE THIS REF because VS gave an error on it.
MenuForm = MenuForm; // Set reference here

this.menu = MenuForm;
}
//this button closes SalesForm and is supposed to unhide MenuForm
//right now it still only loads an object while letting the orriginal just hide and wait :-(
private void uiMenuFormButton_Click(object sender, System.EventArgs e)
{
this.Close();
menu.Visible = true;
}

Try to remove the Code in Red and use the code in Green.

I hope this will work just fine!

Best Regards,



 
 
Blkbird





PostPosted: Visual C# General, Hide/Show form while retaining Info Top

I did that and I got the error
'Can not implicitly convert type 'System.Window.Form.forms' to ' StorePOS.MenuForm'

Thanks Thomas

 
 
RizwanSharp





PostPosted: Visual C# General, Hide/Show form while retaining Info Top

I did that and I got the error
'Can not implicitly convert type 'System.Window.Form.forms' to ' StorePOS.MenuForm'

Thanks Thomas

Ahh ok! Try this now:

namespace StorePOS
{
public class MenuForm : System.Windows.Forms.Form
{

//VS GENERATED THISTHATS AND WHATNOTS
public MenuForm()
{

InitializeComponent();
}
//this button opens SalesForm.cs
private void uiPOSButton_Click(object sender, System.EventArgs e)
{
this.Visible = false;
SalesForm sales = new SalesForm(this);
sales.ShowDialog();
}
SalesForm.cs
namespace StorePOS
{
public class SalesForm : System.Windows.Forms.Form
{
//OBJECT DECLARATIONS
MenuForm menu = new MenuForm();

MenuForm menu;
private System.ComponentModel.Container components = null;

public SalesForm(MenuForm menuForm) // Type and name Changed here from Form to MenuForm
{
InitializeComponent();
//I TOOK OUT THE THIS REF because VS gave an error on it.
MenuForm = MenuForm; // Set reference here

this.menu = menuForm; // Name Changed here
}
//this button closes SalesForm and is supposed to unhide MenuForm
//right now it still only loads an object while letting the orriginal just hide and wait :-(
private void uiMenuFormButton_Click(object sender, System.EventArgs e)
{
this.Close();
menu.Visible = true;
}

I hope this will work now

Lol Best Regards,



 
 
Blkbird





PostPosted: Visual C# General, Hide/Show form while retaining Info Top

That was it thank-you.

Can you tell me what you all this did one last time, I'm still a bit confused

Thanks
Thomas

 
 
RizwanSharp





PostPosted: Visual C# General, Hide/Show form while retaining Info Top

 
That was it thank-you.

Can you tell me what you all this did one last time, I'm still a bit confused

Thanks
Thomas

Hello Thomas,

See, I passed a reference of the main Form to the Secondary form in the constructor, (Because a Class is always reference type) so Passing the (this) in new Form2(this) means that you pass the reference of main Form in form2's constructor, Than you create a field to hold that reference in Form2, And when needed you Do anything with that reference which will be refelected to the original Instance!

I hope its clear now!

Best Regards,