C# Winform编程(1)基础篇
- Visual Studio 2022开发环境新建WinForm应用项目
- WinForm代码结构
- 新键窗体文件
- 从Form1启动Form2
- 修改控件属性
- 退出程序和关闭窗口
Visual Studio 2022开发环境新建WinForm应用项目
WinForm代码结构
-
Program.cs
程序入口
Application.Run(new Form1()); // 设置哪个窗体文件首先被执行using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using System.Windows.Forms; namespace WindowsFormsApp1 { internal static class Program { /// <summary> /// 应用程序的主入口点。 /// </summary> [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new 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 WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent(); // Form1.Designer.cs里面定义初始化组件
}
private void button1_Click(object sender, EventArgs e)
{
}
}
}
- Form1.Designer.cs
namespace WindowsFormsApp1
{
partial class Form1
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要修改
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.saveFileDialog1 = new System.Windows.Forms.SaveFileDialog();
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(79, 101);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 15F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(992, 526);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.SaveFileDialog saveFileDialog1;
private System.Windows.Forms.Button button1;
}
}
新键窗体文件
右键项目->添加->窗体(Windows窗体)
从Form1启动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 WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2();
form2.Show(); // 窗口方式显示,允许在后台运行,Form1和Form2的鼠标焦点可随意切换
}
private void button2_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2();
form2.ShowDialog(); // 对话框方式显示,不允许在后台运行,必须先关闭Form2焦点才能切换到Form1
}
}
}
修改控件属性
退出程序和关闭窗口
- 退出程序
Application.Exit(); - 关闭窗口
form.Close();