好的,這是完整的問題。我已經在C#中創建了一個簡單的應用程序,它顯示當前日期&時間&它有一個「關於」按鈕,通過這個按鈕我已經使它打開顯示我的名字的Form2。一切正常,但每當我打開Form2 &關閉它(即通過單擊Form2窗口框架上的「X」按鈕)&再次如果我點擊「關於」按鈕Form1凍結&掛斷。
下面是代碼Form 1代碼:C#中的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 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();
}
}
}
這裏是Form2的代碼:
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)
{
}
}
}
這是非常基本的,SecondForm對象在關閉它時處理掉了。再次顯示它會導致程序崩潰,並導致ObjectDisposedException異常。它不會「凍結」,調試器顯示問題。在Winforms編程中使用介紹性的書籍或教程,以避免犯這樣的基本錯誤。 –
您的Form2代碼中有很多Form3。儘早瞭解名稱在編程中很重要。 –
@HansPassant你能指點我正確的方向嗎?你能建議我一個教會所有這些基本事情的網站嗎? –