Form 1 in C# hangs up - c#

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.

Related

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

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

How can I change the background of the a form from a second form?

I am using C# windows forms and I'm having a problem. I have a form which I want to change the background of, however, I want to do so from a second form. The second for has a button which when pressed, the background of the first form changes. Here is my code
First Form:
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
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
frm2.ShowDialog();
}
}
}
Second Form:
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 frm1 = new Form1();
frm1.TransparencyKey = Color.Turquoise;
frm1.BackColor = Color.Turquoise;
}
}
}
The button is supposed to turn the first form transparent. This however, does not work. Am I missing something? Thank you!
You can set the Form 2's owner to Form1. Then access it's properties that way.
Form 1
private void button1_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
frm2.ShowDialog(this);
}
Form 2
private void button1_Click(object sender, EventArgs e)
{
Form1 frm1 = (Form1)this.Owner;
frm1.TransparencyKey = Color.Turquoise;
frm1.BackColor = Color.Turquoise;
}
You can do it using delegate and events or by implementing singleton in parent form.

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;

Calling other form method

I have 2 form, Form 1 and 2. Form 1 will be used to initialize Form 2. After Form 2 being initialize, Form 2 will be calling Form 1 method but it just won't work. I had been searching for hours but still no idea how it works. Please help me out.
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.Windows.Forms;
namespace parentChildTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 x = new Form2();
x.Show();
}
public static void callMe(){
MessageBox.Show("HAHAHA");
}
}
}
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.Windows.Forms;
namespace parentChildTest
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
/*How to call callMe()?????*/
}
}
}
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form1.Callme();
}
}
private void button1_Click(object sender, EventArgs e)
{
Form1.callMe();
}

Categories

Resources