How to hide multiple forms with one click in Visual Studio? - c#

I have a form named: form1 from which I can access two other forms: form2 and form3, with one click:
private void buttonViewEmployee_Click(object sender, EventArgs e)
{
string refNumber = 123;
Employee employee = new Employee(refNumber);
FormViewEmployee viewEmployee = new FormViewEmployee(employee);
FormViewNewUpdates newUpdates = new FormViewNewUpdates(employee);
viewEmployee.Show();
newUpdates.Show();
this.Hide();
}
Now, from form3 I would like to hide form2 and form3 and go back to form1:
private void buttonBack_Click(object sender, EventArgs e)
{
FormEditRequests editRequest = new FormEditRequests();
FormViewEmployee viewEmp = new FormViewEmployee(null);
viewEmp.Hide();
this.Hide();
editRequest.Show();
}
It seems to work but form2 actually just hides itself behind form1 but is still visible.
I tried to debug to see what happens exactly but when I debug, it works as perfect as I want but not when I am not debugging.
I tried to change the order of the code execution and also tried to use:
private void Form1_Load(object sender, EventArgs e)
{
FormViewEmployee viewEmp = new FormViewEmployee(null);
base.OnVisibleChanged(e);
viewEmp.Visible = false;
}
But didn't work either.
Can someone explains what happens during the debug mode that doesn't happen when not debugging?

Try following code:
public static void closeAllOpenedForms()
{
FormCollection FM = Application.OpenForms;
if (FM.Count > 1)
{
for (int i = (FM.Count); i > 1; i--)
{
Form sForm = Application.OpenForms[i - 1];
sForm.Close();
}
}
}

You can check/clone/test this code on https://github.com/JomaStackOverflowAnswers/HideForms
If you need to reuse the opened form you can hide/show as neeeded. But if you create new forms everytime is recommended to close the forms(not hide).
FormMain
namespace HideForms
{
public partial class FormMain : Form
{
public FormMain()
{
InitializeComponent();
}
private void buttonOpen_Click(object sender, EventArgs e)
{
Employee employee = new Employee("123");
FormViewEmployee formViewEmployee = new FormViewEmployee(employee);
FormViewNewUpdates formViewNewUpdates = new FormViewNewUpdates(employee, this, new List<Form>{ formViewEmployee });
formViewEmployee.Show();
formViewNewUpdates.Show();
Hide();
}
}
}
FormViewEmployee
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace HideForms
{
public partial class FormViewEmployee : Form
{
private readonly Employee employee;
public FormViewEmployee(Employee employee)
{
InitializeComponent();
this.employee = employee;
}
}
}
FormViewNewUpdates
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace HideForms
{
public partial class FormViewNewUpdates : Form
{
private readonly Employee employee;
private readonly Form parent;
private readonly List<Form> formsToClose;
public FormViewNewUpdates(Employee employee, Form parent, List<Form> formsToClose)
{
InitializeComponent();
this.employee = employee;
this.parent = parent;
this.formsToClose = formsToClose;
}
private void buttonClose_Click(object sender, EventArgs e)
{
foreach(var form in formsToClose)
{
form.Close();//form.Hide(); //Hide in case you need to reuse the form.
}
Close();
parent.Show();// Show the parent form.
}
private void FormViewNewUpdates_FormClosing(object sender, FormClosingEventArgs e)
{
foreach (var form in formsToClose)
{
form.Close();
}
parent.Show();
}
}
}
Output
FormMain When click in open button, it opens FormViewEmployee, FormViewNewUpdates instances.
FormViewEmployee, FormViewNewUpdates instances
When click in Close All button it close both instances and show FormMain instance.
2
References
Control.Hide
Form.Close

Related

Button that close from 1 and opens form 2 [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
so what i want is when you press the button you open form 2 where my game is. the button wich is at form 1 is going to be my main menu for the game, you click it and it opens the game and closes the menu (if it is possible to do this with out having 2 forms and just using 1 do share how i can do this)
Form 1
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication3
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
}
}
}
Form 2
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Spill
{
public partial class MainWindow : Form
{
Random _random;
public MainWindow()
{
InitializeComponent();
_random = new Random();
}
private void MainWindow_Load(object sender, EventArgs e)
{
Size s = new System.Drawing.Size(800, 600);
this.ClientSize = s;
this.FormBorderStyle = FormBorderStyle.FixedSingle;
}
private void MainWindow_KeyDown(object sender, KeyEventArgs e)
{
{
if (e.KeyCode == Keys.Left)
{
Player.Left -= 20;
}
if (e.KeyCode == Keys.Right)
{
Player.Left += 20;
}
}
}
private void timer1_Tick(object sender, EventArgs e)
{
int z = _random.Next(0, 10);
int x = _random.Next(0, 20);
int y = _random.Next(0, 30);
LargeEnemy.Left += z;
MediumEnemy.Left += x;
SmallEnemy.Left += y;
}
private void newGameToolStripMenuItem_Click(object sender, EventArgs e)
{
Application.Restart();
}
private void quitGameToolStripMenuItem_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void LargeEnemy_Click(object sender, EventArgs e)
{
}
}
}
It's very simple:
using System;
using System.Collections.Generic;
using System.ComponentModel;
System.Data;
System.Drawing;
using System.Linq;
System.Text;
System.Threading.Tasks;
System.Windows.Forms;
namespace WindowsFormsApplication3
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//hide the current winform
this.Hide();
//create game winform
WindowsFormsApplication1 frmGame = new WindowsFormsApplication1();
//show the game winform
frmGame.ShowDialog();
//when the game winform closes show again the menu
this.Show();
}
}
}
and in the second form in this functions set them like this
private void newGameToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Close();
}
private void quitGameToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Close();
}
This is a very simple example using a form manager to handle a single instance. This way both forms stay live and you are just updating the visibility.
You will want to close both forms correctly to close the application.
namespace WindowsFormsApplication1
{
static class FormManager
{
public static Form1 Game = new Form1();
public static Form2 Menu = new Form2();
}
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.Run(FormManager.Game);
}
public partial class Form1 : Form
{
private void btn_Menu_Click(object sender, EventArgs e)
{
FormManager.Game.Hide();
FormManager.Menu.Show();
}
}
public partial class Form2 : Form
{
private void btn_Close_Click(object sender, EventArgs e)
{
FormManager.Menu.Hide();
FormManager.Game.Show();
}
}
}

visual studio open link seperate form

Basically trying to learn a few things I havn't tried yet. I want to make a small program that has a Textbox and Button.
Whats typed into the textbox field will be added to the end of a url applied from the button.
http://website.com/stuff?things= +textboxValue
After you click the launch button, I want the url created with the entered text, to open in a second form.
I have everything working except for the text from form1 to carry over to form2. Just wondering how I can go about this.
Form 1
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication8
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2();
form2.LinkTarget = textBox1.Text;
form2.Show();
}
private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
Form3 newFrm = new Form3();
newFrm.Show();
}
private void Form1_Load_1(object sender, EventArgs e)
{
}
}
}
Form2
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication8
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
public string LinkTarget {
get;
set;
}
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
string myUrl = "http://website.com/stuff?things=" + LinkTarget;
}
}
}
Example of what I am going for.
Form one has text box and button.
when you click the button it will open http://www.stackoverflow.com/questions/ in the second form. the second form is basically a browser.
but, in form one if I type 41090977 before I hit the button, it will open http://www.stackoverflow.com/questions/41090977
You can create a property in form2, which you set before you open it:
//... other form2 code
public string LinkTarget {
get;
set;
}
You can set the value like this (in button1_Click):
Form2 form2 = new Form2();
form2.LinkTarget = myTextBox.Text;
form2.Show();
Within form2 you can use the value of the property to create your link. In your current code you have just defined the property. You have to use its content to create your link. This is done by attaching the property value to your string containing the URL with the + operator:
string myUrl = "http://website.com/stuff?things=" + LinkTarget;

Get/Set Variable

the goal of the program is to be able to use the get and set methods of variables.
I have this code in a project C#:
Form1:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
private int c = 0;
public int a { get; set; }
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
a = 5;
Form2 f2 = new Form2();
f2.b = a;
f2.Show();
}
}
}
and in a Form2:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form1 f1 = new Form1();
MessageBox.Show(Convert.ToString(b));
}
}
}
This code not work because the value of b should be 5, but during execution value 0;
any solution?
You can't expect values of one instance to magically propagate to every other instance.
So doing this:
Form1 f1 = new Form1();
int b = f1.a;
Is always going to be 0. You created a new instance, and nothing has happened to it! If you want to get the existing form's value (where the button was presumably clicked) you need to pass it to Form2 somehow.
You can:
Pass it on the constructor of Form2
Set up a service that holds the data instead
Probably about a million other approaches
private void button1_Click(object sender, EventArgs e)
{
a = 5;
Form2 f2 = new Form2();
f2.b=a;
f2.Show();
}
public partial class Form2 : Form
{
public int b;
public Form2()
{
InitializeComponent();
}
}
You want to pass the data before showing the second form instead of after the fact.

Form 1 in C# hangs up

Ok, here is the complete problem. I've created an simple application in C# which displays the current date & time & it has an "About" button through this button I've made it to open Form2 which displays my name. Everything works fine but whenever I open Form2 & close it(i.e by clicking on the "X" button on Form2 window frame) & again if I click on "About" button Form1 freezes & hangs up.
Here is the code Form1 code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Date
{
public partial class Form1 : Form
{
Form3 SecondForm = new Form3();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void timer1_Tick(object sender, EventArgs e)
{
this.label2.Text = DateTime.Now.ToString(" hh:mm:ss tt");
label4.Text = DateTime.Now.ToString(" dd-MM-yyyy ");
}
private void about_btn_Click(object sender, EventArgs e)
{
SecondForm.Show();
}
}
}
Here is the Form2 code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Date
{
public partial class Form3 : Form
{
public Form3()
{
InitializeComponent();
}
private void Form3_Load(object sender, EventArgs e)
{
}
}
}
You need to create your SecondForm each time when you want to show it like this
namespace Date
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void timer1_Tick(object sender, EventArgs e)
{
this.label2.Text = DateTime.Now.ToString(" hh:mm:ss tt");
label4.Text = DateTime.Now.ToString(" dd-MM-yyyy ");
}
private void about_btn_Click(object sender, EventArgs e)
{
Form3 secondForm = new Form3();
secondForm.Show();
}
}
}
Problem in the original code is that when form closes, it tries to destroy controls, data on it, and when you try to call Show again, it can't display it as it already destroyed.

Change the state of a checkbox on Form2 and keep the state

All I am trying to do is to change the state of checkbox on form2, and keep the state after pression OK.
I have form1 which is my main form and it only has one Strip menu. The code for form1 is as follow:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace test
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void dialogToolStripMenuItem_Click(object sender, EventArgs e)
{
Form2 dialog = new Form2();
dialog.ShowDialog();
}
}
}
The Form2 only has one checkbox and one OK button.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace test
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void OK_Click(object sender, EventArgs e)
{
if (cbxForm2.Checked == true)
{
cbxForm2.Checked = true;
}
}
}
}
How can I change my code so when I go back on the menu the state of the combo box is as I left it?
You are creating a new Form2 every time:
private void dialogToolStripMenuItem_Click(object sender, EventArgs e)
{
// the 'new' keyword means you are creating an entirely new instance
Form2 dialog = new Form2();
dialog.ShowDialog();
}
This new instance has no idea what any previous instances looked like, so you need to store the state of the CheckBox and assign the value when you open Form2.
public partial class Form1 : Form
{
// backing field to store the state
bool checkBoxChecked;
public Form1()
{
InitializeComponent();
}
private void dialogToolStripMenuItem_Click(object sender, EventArgs e)
{
Form2 dialog = new Form2();
// assign the state
dialog.CheckBoxChecked = this.checkBoxChecked;
dialog.ShowDialog();
// save the state
this.checkBoxChecked = dialog.CheckBoxChecked;
}
}
You also need to add a property on Form2 so you can retrieve the state:
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
public bool CheckBoxChecked
{
get { return cbxForm2.Checked; }
set { cbxForm2.Checked = value; }
}
}

Categories

Resources