C#之WinForm基礎 點選button關閉當前窗體,用this.close()
阿新 • • 發佈:2019-02-20
慈心積善融學習,技術願為有情學。善心速造多好事,前人栽樹後乘涼。我今於此寫經驗,願見文者得啟發。
program.cs
效果
form1的佈局,
解決方案資源管理器,
form1.cs
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 Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { } private void button1_Click(object sender, EventArgs e) { this.Close(); } } }
program.cs
using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using System.Windows.Forms; namespace WindowsFormsApplication3 { static class Program { /// <summary> /// 應用程式的主入口點。 /// </summary> [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1()); } } }
有緣人的提問與心少樸的個人見解
提問:樓主請問一下能不用this,先例項化視窗,Form1 form=new Form1();form.Close();為什麼這樣就不能了
個人見解:
程式碼
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 WindowsFormsApp1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { Form1 form = new Form1(); form.Show(); // form.Close(); } } }
效果
看到這個效果你明白了嗎?form.close()關閉的是新創建出來的窗體
新窗體哪裡來的? -> Form1 form = new Form1(); 建立了一個新的窗體物件
感恩曾經幫助過 心少樸 的人。
C#優秀,值得學習。Winform,WPF 都可以關注一下,眼界要開闊。
Visual Studio IDE很好用,推薦!
注:此文是自學筆記所生,質量中等,故要三思而後行。新手到此,不可照搬,應先研究其理象數,待能變通之時,自然跳出深坑。