Form1 hides instead of closing - c#

I am trying to create a simple login page on which if i click login button form1 should hide and form2 should popup and on form2 when i click logout it should bring me back to form1, problem is that when i click logout button on form2 it brings me back to form1 like it should but when i click "X" (Close) on form1 it hides (I can see the application running in task manager) instead of closing.
form1 sign in button code:
private void button1_Click(object sender, EventArgs e)
{
Form2 mainUI = new Form2();
mainUI.Show();
this.Hide();
}
form2 logout button code:
private void button1_Click(object sender, EventArgs e)
{
Form1 loginPage = new Form1();
loginPage.Show();
this.Close();
}

The problem is here, you are creating a new instance of Form1 instead of opening it again:
Form1 loginPage = new Form1();
Here try this one:
private void button1_Click(object sender, EventArgs e)
{
var formToBeOpen = Application.OpenForms.OfType<Form1>().SingleOrDefault();
formToBeOpen.Show();
this.Close();
}
Now regarding your post, that if you close your Form2, your Form1 is still running on the background, because of this code:
this.Hide();
You just hide it, then open your Form2, then you click the X button on your Form2 which closes your Form2 but not necessarily mean will close your Form1 as well, since it's still running on the background. If you want to close your Form1, add this on your Form2 code:
private void Form2_FormClosed(object sender, FormClosedEventArgs e)
{
var formToBeOpen = Application.OpenForms.OfType<Form1>().SingleOrDefault();
formToBeOpen.Close();
}

i'll try to answer. because you declare a new instance in button logout. in form1 button put this code
private void button1_Click(object sender, EventArgs e)
{
Form2 x = new Form2(this);
x.Show();
this.Hide();
}
as form2, since i pass form1, put this code
public partial class Form2 : Form
{
Form form1;
public Form2(Form x)
{
InitializeComponent();
form1 = x;
}
private void button1_Click(object sender, EventArgs e)
{
this.Hide();
form1.Show();
}
}

Related

C# Back button on visual studio

I am creating a quiz for my As level coursework on visual studio 2019(c#). In this I will be creating a help button that will have information that the user may need if they are stuck. The button to access the help form will be avaliable through a menu strip bar loacted in the top corner of every form. In the help form there will be a menu strip bar with a back button. I would like to know how to code a back button to go back to the previous form eg Question 1-10 forms or the login form. I know how to code it if i wanted to back to a specific form but it is the fact it may need to back to any form as i dont know which form the user will have previously been on.
If you want to code a back button to go back to the previous form, you can refer to the following code:
Code in Form1.cs:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnForward_Click(object sender, EventArgs e)
{
this.Hide();
Form2 newform = new Form2();
newform.ShowDialog();
this.Show();
}
}
Code in Form2.cs :
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void btnForward_Click(object sender, EventArgs e)
{
this.Hide();
Form3 newform = new Form3();
newform.ShowDialog();
this.Show();
}
private void btnBack_Click(object sender, EventArgs e)
{
this.DialogResult = DialogResult.OK;
}
}
Code in Form3.cs :
public partial class Form3 : Form
{
public Form3()
{
InitializeComponent();
}
private void btnForward_Click(object sender, EventArgs e)
{
this.Hide();
Form4 newform = new Form4();
newform.ShowDialog();
this.Show();
}
private void btnBack_Click(object sender, EventArgs e)
{
this.DialogResult = DialogResult.OK;
}
}
If you want to open other forms, just change this code ‘Form2 newform = new Form2();’.
Commonly, one would store the questions in a list or array. If you also store the index of the current question, then you could use that to return to the correct question.
The button should navigate between different windows:
STEP 1 - add a new button on your current window.
STEP 2 - double click on that button to access cs file:
private void Button_Click(object sender, RoutedEventArgs e)
{
}
STEP 3 - create a new object window (to navigate to) and open that, then close current window
private void Button_Click(object sender, RoutedEventArgs e)
{
NewWindow page2= new NewWindow();
page2.Show();
this.Close();
}
You should be able to move between different pages back and forth

Why do my C# Form closed when displayed?

i have 2 forms, those are form1.cs and form2.cs
on the form1, it has button1, which will call form2 to show
here's the button1 code
private void button1_Click(Object sender, EventArgs e )
{
form2 form = new form2();
form2.show(); // to call form2
this.dispose(); //to dispose form1
}
and then form2 showed, and it closed suddenly. anyone know how to solve this ?
When you close your main form with this.dispose() you are terminating the program causing form2 to be disposed also because you are diposing the reference to form2. You would be better off passing a reference to your form1 to form2 and using this.Hide() instead.
You can try something like this:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 form = new Form2();
form.setParent(this);
form.Show();
this.Hide();
}
}
And in form2 to go back to form1
public partial class Form2 : Form
{
Form parentForm;
public Form2()
{
InitializeComponent();
}
public void setParent(Form value)
{
parentForm = value;
}
private void button1_Click(object sender, EventArgs e)
{
parentForm.Show();
this.Close();
}
}
private void button1_Click(Object sender, EventArgs e )
{
form2 form = new form2();
form2.show(); // to call form2
this.hide(); //to hide form1
}
if form1 is program starter then application will close . Hence instead
this.dispose();
U just write
this.hide();
Show() does not wait for form2 to close before continuing to the next command (dispose).
This will end up in closing form2 because it's probably running on a background thread.
Use ShowDialog to hold the execution of Dispose until the second form closes.
Also, you can set the second form to run on a foreground thread. This way the second form will not be dependent on the life of the first.
You can either use this.Hide() which should hide your current form or use a thread to open the new form.
Example: C# open a new form, and close a form...

How to show another form after playing an SWF file in C#

I have a form that contains a Shockwave Flash object. I want that after playing the file or in the middle of it by pressing a key another form is shown. What should I do? Should I use an event?
Here's the code:
private void Form1_Load(object sender, EventArgs e)
{
axShockwaveFlash1.Movie = "c:/intro.swf";
axShockwaveFlash1.Forward();
axShockwaveFlash1.Play();
Form2 form2 = new Form2();
form2.Show();
}
And in Form2:
private void Form2_Load(object sender, EventArgs e)
{
Form1 form1 = new Form1();
form1.Close();
}
When I debug both forms are shown but I just want Form2.
You can pass the Form1 instance to the Form2 constructor, and close it there, like:
public Form2(Form1 form1)
{
InitializeComponents();
form1.Hide();//hide the control from the user, or close it if it not the main form..
}
Or if you want it to close the form1 instance when a button click then:
private Form1 _form1 = null;
public Form2(Form1 form1)
{
InitializeComponents();
_form1 = form1;
}
private void button1_Click(object sender, EventArgs e)
{
if (_form1 != null)
{
_form1.Hide();
}
}
Initialize the form2 in form1:
axShockwaveFlash1.Movie = "c:/intro.swf";
axShockwaveFlash1.Forward();
axShockwaveFlash1.Play();
Form2 form2 = new Form2(this);//"this here is the form1 instance itself"
form2.Show();
you can also use timer to check weather the movie is playing or not,if not then open new form which is form2
private void flash_Load(object sender, EventArgs e)
{
axShockwaveFlash1.Movie = #"C:\Users\Adil M\Documents\Visual Studio 2012\Projects\TProject\a.swf";
int a = axShockwaveFlash1.MovieData.Length;
timer1.Start();
}
now code on timer event
private void timer1_Tick(object sender, EventArgs e)
{
if (!axShockwaveFlash1.IsPlaying())
{
Fome2 fobj = new Form2();
this.Hide();
timer1.stop();
fobj.show();
}
else if(axShockwaveFlash1.IsPlaying())
{
}
}
this code is working for you only when your movie is complete or for any key you can also do it in timer
thanx

Open Form2 from Form1, close Form1 from Form2

Now, I have two forms, called form1 and form2, in the form1 there's a button, when I click it, then open the form2
Question: in the form2, I want to create a button when I click it, the form2 close and the form1 close. How to do?this
Form1:
private void button1_Click(object sender, EventArgs e)
{
Form2 frm = new Form2(this);
frm.Show();
}
Form2:
public partial class Form2 : Form
{
Form opener;
public Form2(Form parentForm)
{
InitializeComponent();
opener = parentForm;
}
private void button1_Click(object sender, EventArgs e)
{
opener.Close();
this.Close();
}
}
This works:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Me.Hide()
Form2.Show()
Your question is vague but you could use ShowDialog to display form 2. Then when you close form 2, pass a DialogResult object back to let the user know how the form was closed - if the user clicked the button, then close form 1 as well.
on the form2.buttonclick put
this.close();
form1 should have object of form2.
you need to subscribe Closing event of form2.
and in closing method put
this.close();
if you just want to close form1 from form2 without closing form2 as well in the process, as the title suggests, then you could pass a reference to form 1 along to form 2 when you create it and use that to close form 1
for example you could add a
public class Form2 : Form
{
Form2(Form1 parentForm):base()
{
this.parentForm = parentForm;
}
Form1 parentForm;
.....
}
field and constructor to Form2
if you want to first close form2 and then form1 as the text of the question suggests, I'd go with Justins answer of returning an appropriate result to form1 on upon closing form2
I did this once for my project, to close one application and open another application.
System.Threading.Thread newThread;
Form1 frmNewForm = new Form1;
newThread = new System.Threading.Thread(new System.Threading.ThreadStart(frmNewFormThread));
this.Close();
newThread.SetApartmentState(System.Threading.ApartmentState.STA);
newThread.Start();
And add the following Method. Your newThread.Start will call this method.
public void frmNewFormThread)()
{
Application.Run(frmNewForm);
}
//program to form1 to form2
private void button1_Click(object sender, EventArgs e)
{
//MessageBox.Show("Welcome Admin");
Form2 frm = new Form2();
frm.Show();
this.Hide();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 m = new Form2();
m.Show();
this.Visible = false;
}

I would like to control Form1 from Form2

So I want basically the user to login in first in order to use the other form. However, my dilemma is that the login box is in Form2, and the main form is Form1.
if ((struseremail.Equals(username)) && (strpasswd.Equals(password)))
{
MessageBox.Show("Logged in");
form1.Visible = true;
form1.WindowState = FormWindowState.Maximized;
}
else
{
MessageBox.Show("Wow, how did you screw this one up?");
}
However, Form1 doesn't become visible, (since I launch it as visble = false) after they log in. Can someone help?
EDIT:
Brilliant response, but my problem is still here. I basically want to load Form2 First, (which is easy I run Form1 and set it to hide) But when Form2 is closed, I want Form1 to be closed as well.
private void Form2_FormClosing(Object sender, FormClosingEventArgs e)
{
Form1 form1 = new Form1();
form1.Close();
MessageBox.Show("Closing");
}
this doesn't seem to work...
You will need to pass the reference of one form to another, so that it can be used in the other form. Here I've given an example of how two different forms can communicate with each other. This example modifies the text of a Label in one form from another form.
Download Link for Sample Project
//Your Form1
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 frm = new Form2(this);
frm.Show();
}
public string LabelText
{
get { return Lbl.Text; }
set { Lbl.Text = value; }
}
}
//Your Form2
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private Form1 mainForm = null;
public Form2(Form callingForm)
{
mainForm = callingForm as Form1;
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
this.mainForm.LabelText = txtMessage.Text;
}
//Added later, closing Form1 when Form2 is closed.
private void Form2_FormClosed(object sender, FormClosedEventArgs e)
{
mainForm.Close();
}
}
(source: ruchitsurati.net)
(source: ruchitsurati.net)
When you log in and do Form1.visible = true; have you also tried Form1.Show(); that should show form2
However, Personally, I would prefer setting the application to run form2 directly in the program.cs file.
static void Main()
{
Application.Run(new Form2());
}
then when user successfully logs in, do
form1.Show();
this.Hide(); // this part is up to you
mind you, in form2, when / after you instantiate form1, you might want to also add this :
newform1.FormClosed += delegate(System.Object o, FormClosedEventArgs earg)
{ this.Close(); };
this closes form2 when form1 is closed
better yet do form1.Show() in a new thread, and then this.Close(); for form2. this removes the need of adding to the form2's FormClosed event: you can thus close form2 immediately after starting form1 in a new thread. But working with threads might get a little complicated.
EDIT:
form2 is form1's parent. if form2 is your main application form, closing it closes your program (generally). Thus you either want to just hide and disable form2, and close it only after form1 is closed, or start form1 in a new thread. Your edit pretty much opens form1, then immediately closes it.

Categories