How to show a hidden form from a different form? - c#

I have a Form1, Form2 and Form3, Form1 being the main form.
The program starts at Form1, when I click a "next" button Form2 is called and Form1 hides.
Inside Form1:
Form2 form2 = new Form2(this);
this.Hide();
form2.Show();
And inside Form2 there's an "exit" and a "next" button. When I click on exit button, Form2 is closed and goes back to Form1, unhiding it.
Inside Form2 and inside partial class Form2:
private Form parent;
public Form2(Form caller)
{
parent=caller;
}
On exit button:
this.Close();
parent.show();
And at the "next" button of Form2 I call Form3 and close Form2:
Form3 form3 = new Form3();
this.Close();
form3.Show();
Now that I'm in Form3 how can I unhide/show Form1 from Form3 ?
I know that I have to list here the solutions I've tried, but I'm just starting with OOP and I'm stuck trying to do this. I've tried several things but none of them make sense and don't know how to do this.
Just to clarify, I don't want to create a new instance of Form1 I just want to Show the one that's already created.
Any help is really appreciated.

Form1 should look like:
Form2 form2 = new Form2(this);
this.Hide();
form2.Show();
Form2 should look like:
Form3 form3 = new Form3(parent);
this.Close();
form3.Show();
Your form2 knows about your form1 via the parent member, and it can hence give your form1 to Form3 (which should also have a constructor that takes a Form1)
Every form should have a constructor that takes a Form1 and stores it in a parent variable. Every form other than Form1 should pass the parent variable to any new form it creates
You can make this easier using inheritance for the ctor, the next and the close functionality
I do not recommend calling your class Form- call it Form1 so it doesn't clash with System.Windows.Forms.Form
Actually you should call them all something better- MainForm, CreatePersonForm and CreateAddressForm for example

Related

How to Bring One Form on Another form

Here Form2 is Drawing Viewer And Form3 is toolbar when form2 is maximized form3 is get to back of form2. I want form3(Toolbar) on form3. if i minimize or maximize still form3 should be in front of form2. How can i bring form3 in front of form2
In WinForms there is control named statusStrip. Maybe will be better to use it instead of new form.
Anyway, if you want to do it in your way, you can put in your Form2 to the bottom Panel, in which you can load your Form3 to.
Try using in a method where you have shown form 2
Form Form2 = new Form();
Form2.MdiParent = this;
Form2.Show();
Refer below link for more details :
https://www.codeproject.com/Articles/3553/Introduction-to-MDI-Forms-with-C

How to call Form2 in Form1

I'm doing a shop with 7 forms, the form 1 is the basic of the store with picture's Box and a textbox saying how much should I pay. When I click in the picture's box, for example in a T-shirt, it opens a new Form with some information and a button saying "add to cart".
Basically I want to click on the button "add to cart" and then the Form2 closes and back's to form 1 and in the textbox (that shows how much should I pay) shows the value.
Forms are objects like any other, and you can pass references to them and call methods on them like any other.
For example, let's say you expose a method on Form1 which accepts the value and updates the UI:
public void UpdatePayAmount(double amount)
{
// use the supplied value to update your text box
}
Then you would want to call that method from Form2 when clicking on the "Add to Cart" button. Something like:
form1Instance.UpdatePayAmount(someAmountValue);
So your Form2 code needs a reference to an instance of Form1 in a variable somewhere. Since Form2 now depends on Form1, a sensible place to put that requirement would be in its constructor. Perhaps populating a private field:
private Form1 form1Instance;
public Form2(Form1 form1)
{
form1Instance = form1;
}
Now Form2 requires a reference to an instance of Form1 when you create it, so it can call a method on that instance when the button is clicked. So when you create Form2 in your Form1 code you would supply that instance:
form2 = new Form2(this);
form2.Show();

Opening a new form without owner?

I made a C# Windows Forms application that requires the user to login using a form called Form1 as shown below:
// This code is called from Form1
Form2 f = new Form2();
f.Show();
// Then Form1 does some finalization logic and closes itself
After the user logs in, the login form should close and the main application window Form2 should be opened. But the problem I'm running into is that if I call Form2 from Form1, Form1 becomes Form2's owner, thus closing Form1 closes both forms and ends the application.
How may I call Form2 such that it is independent of Form1?
The problem is not the owner, but that the Windows message loop is tied to Form1. When Form1 closes, so does the application. Look in your Main method:
Application.Run(new Form1());
The easiest solution is to show your login form (assuming it's a login form) as a modal dialog, and then begin the windows message loop on Form2:
static void Main()
{
var form1 = new Form1();
form1.ShowDialog();
if (form1.LoginSuccessful)
{
Application.Run(new Form2());
}
}
EDIT:
Just did some googling and looks like another alternative is custom ApplicationContexts. Never worked with these myself though:
http://msdn.microsoft.com/en-us/library/system.windows.forms.applicationcontext.aspx

Showing the second form exactly on the place of first form

From main form (Form1) I am calling to show another form(Form2).
but I want it to show exactly the same place and size that form1 is, so that we wont be able to see form1 anymore until we either close form2 or move it somewhere else.
so I wrote these lines:
Form2 f2 = new Form2();
f2.Left = this.Left;
f2.Top = this.Top;
f2.Size = this.Size;
f2.Show();
But it still has problems. form2 us not completely on form1. any other thing I should add to the code?
Yes, you are doing this the wrong way around. The actual size of the form is only the same as the design size if the machine you run this on has the exact same user preferences, system font size and video DPI setting. If it is off a lot then the DPI setting is different. If it is off a little then the user preferences are different. Like a larger title bar font or bigger buttons. Fix:
Form2 f2 = new Form2();
f2.Show();
f2.Left = this.Left;
f2.Top = this.Top;
f2.Size = this.Size;
If that's too noticeable then you should let the Form2's Load event do this. Pass a reference to your main form or use the Owner property and Show(owner). In other words:
Form2 f2 = new Form2();
f2.Show(this);
in Form2:
protected override void OnLoad(EventArgs e) {
base.OnLoad(e);
this.Location = Owner.Location;
this.Size = Owner.Size;
}
If you don't want the user to interact with Form1 until Form2 is closed, then change your last line to
f2.ShowDialog();
Then it doesn't matter if the user can still see Form1. Windows won't let Form1 get the focus again until Form2 is closed.
What will happen when show your Form2 with the same size and same locations with Form1? Form1 will be invisible right? So, why you dont use form1.Hide(); ?
try this...
Form2 f2 = new Form2();
f2.Show();
f2.SetBounds(this.Location.X, this.Location.Y,this.Width, this.Height);
//this.Hide(); // if you want to hide 1stform after showing 2nd form
In your form properties for form2, set it to "center on parent"
If the forms are the same size, then this will place form2 over form1 and form 1 is not accessible. Still open the form using modal (form2.ShowDialog()) so focus stays with form2 even if the user moves form two manually.
You will still be able to move form2 like I just mentioned off of form one, but that was not specified as part of this question.

Start new form on closing one. C#

When my program runs it closes form1 after a few seconds. Depending on what happened during form1's lifespan I may want to open form2. Normally I would just make a new instance of form2 and use the show() method. But form2 is then a child of form1 and then also closes. Does any body have an idea on how to get this to work? thanks.
For multi-form applications I tend to have one form that is the "main" form, which opens up the sub forms.
The main form is the one that gets started with Application.Run(...)
In your case you might want to have a blank form that can be the controller, and have Application.Run call that.
That form can then start instantiate your Form1 and run it.
e.g.
public ControlForm : public Form
{
Form1 form1;
Form2 form2;
public ControlForm()
{
form1 = new Form1();
form2 = new Form2();
}
public void Start() // or something similar
{
form1.ShowDialog(); // will block showing the form, or you can do other tricks
// to show the form here
if(form1.someFlag) form2.ShowDialog();
}
}
This is just "psudo-C#" code, but hopefully the concept makes sense
Then your main function can just run "ControlForm"
Its just a concept you might want to try
You can open a new form in your application's bootstrapper (main method). You will want to call Application.Run(yourFormHere). You would have two of these in a row in the order you want to show the forms. You could store the results of the first form in some static location and check that before showing the second form.
I ended up doing this:
Auth f = new Form1();
Application.Run(f);
if (f.authed)
{
Application.Run(new Form2());
}
I don't think that your problem is that the Form2 instance is a child form of the Form1 instance, but rather that the Form1 instance is your applications main form. That will make your application quit whenever Form1 closed. One way to prevent this is to alter the main method to not set Form1 as the main form (see here for details on that).

Categories