Reputation: 117
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)
{
}
}
}
Upvotes: 1
Views: 242
Reputation: 7478
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.
Upvotes: 2