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
Related
I have a problem concerning c# and Windows Forms.
I am developing an application which needs more than 10 forms.
I started to close forms by clicking buttons and start new forms. It works quite well.
But now I am in form no. 5. I can't link any button with this new form...
In earlier forms, it worked??
Is there a limit on a number of forms?
Thanks for your help!
Greetings from Germany
Code: here it works
namespace WindowsFormsApplication1
{
public partial class Form3: Form
{
public Form3()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
this.Hide();
Form4 f4 = new Form4();
f4.ShowDialog();
}
}
}
Code: here it isn't working
namespace WindowsFormsApplication1
{
public partial class Form4: Form
{
public Form4()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
this.Hide();
Form3 f3 = new Form3();
f3.ShowDialog();
}
private void button2_Click(object sender, EventArgs e)
{
this.Hide();
Form5 f5 = new Form5();
f5.ShowDialog();
}
}
}
This is due to the fact that you're using Hide() instead of Close().
If you use the Hide() function, it just sets the form's property Visibility.Hidden, which means that the Form is still running on the background.
Now, when you call the function from Form4 to "Close" and open Form3, you're just hidding Form4, if you want to call Form4 back, you're gonne create two Form4 (of the same Type), since it's still in the background.
One way I like to do that is to check if the form is open before creating a new one:
private void button1_Click(object sender, EventArgs e)
{
Close();
var app = Application.OpenForms["Form3"];
if (app == null)
{
Form3 f3 = new Form3();
f3.Show();
}
else { app.Show(); app.BringToFront(); }
}
Or you can just call a new Form, but make sure you Close the form.
private void button1_Click(object sender, EventArgs e)
{
Close();
Form3 f3 = new Form3();
f3.Show();
}
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();
}
}
I have a form called form1 with controls which are created during run-time.
When I press a button on the Form another Form loads called combat and form1 is hidden so that only 1 form (combat) is visible.
When I press a button on combat I want my form1 form the be shown. However I can't access it.
Here is what I've tried:
private void combatBtn_Click(object sender, EventArgs e)
{
Form combat = new Combat(this);
this.Hide();
combat.Show();
}
public partial class Combat : Form
{
public Combat(Form form)
{
InitializeComponent();
form.Show();
}
private void button1_Click(object sender, EventArgs e)
{
form.Show();
}
}
You need to store the parent form in a field so that you can access it outside the constructor.
public partial class Combat : Form
{
private form1 form; // Or whatever class you form1 is supposed to be
public Combat(Form form)
{
InitializeComponent();
this.form = form;
}
private void button1_Click(object sender, EventArgs e)
{
form.Show();
}
}
It's generally not advisable to pass an instance of a parent form to a child. In this case (as is often true) the code is actually simpler when you don't:
private void combatBtn_Click(object sender, EventArgs e)
{
Form combat = new Combat();
this.Hide();
combat.ShowDialog();
this.Show();
}
If you need to show the parent form before the child form is closed then you can do so through events:
in Combat add:
public event Action MyEvent; //TODO rename to a meaningful name
Fire the event in the button click handler:
private void button1_Click(object sender, EventArgs e)
{
MyEvent();
}
And then have your main form add a handler to the event:
private void combatBtn_Click(object sender, EventArgs e)
{
Combat combat = new Combat();
this.Hide();
combat.MyEvent += () => this.Show();
combat.Show();
}
I have a form called form1 with controls which are created during run-time.
When I press a button on the Form another Form loads called combat and form1 is hidden so that only 1 form (combat) is visible.
When I press a button on combat I want my form1 form the be shown. However I can't access it.
Here is what I've tried:
private void combatBtn_Click(object sender, EventArgs e)
{
Form combat = new Combat(this);
this.Hide();
combat.Show();
}
public partial class Combat : Form
{
public Combat(Form form)
{
InitializeComponent();
form.Show();
}
private void button1_Click(object sender, EventArgs e)
{
form.Show();
}
}
You need to store the parent form in a field so that you can access it outside the constructor.
public partial class Combat : Form
{
private form1 form; // Or whatever class you form1 is supposed to be
public Combat(Form form)
{
InitializeComponent();
this.form = form;
}
private void button1_Click(object sender, EventArgs e)
{
form.Show();
}
}
It's generally not advisable to pass an instance of a parent form to a child. In this case (as is often true) the code is actually simpler when you don't:
private void combatBtn_Click(object sender, EventArgs e)
{
Form combat = new Combat();
this.Hide();
combat.ShowDialog();
this.Show();
}
If you need to show the parent form before the child form is closed then you can do so through events:
in Combat add:
public event Action MyEvent; //TODO rename to a meaningful name
Fire the event in the button click handler:
private void button1_Click(object sender, EventArgs e)
{
MyEvent();
}
And then have your main form add a handler to the event:
private void combatBtn_Click(object sender, EventArgs e)
{
Combat combat = new Combat();
this.Hide();
combat.MyEvent += () => this.Show();
combat.Show();
}
I want to close all forms but this code doesnt work. Could someone give me the right code?
private Form0 _form0 = null;
public Form1(Form0 form0)
{
InitializeComponent();
form0.Hide();
_form0 = form0;
}
form0 show a demo when user press the entrance button form1 will be shown.
private void ToolStripMenuItem7_Click(object sender, EventArgs e)
{
Form1 form1 = new Form1(form0);
form1.Dispose();
form1.Close();
}
private void ToolStripMenuItem7_Click(object sender, EventArgs e)
{
using(Form1 form1 = new Form1(form0))
{
form1.Show();
}
}
Application.Exit(); will close all forms and entire application as well.