开启掘金成长之旅!这是我参与「掘金日新计划 · 2 月更文挑战」的第 2 天,点击查看活动详情,如果文章对你有帮助,请点点关注赞一下哦~
在 visual studio 中(这里使用2019)
创建好之后将可以看到,创建了一个名为TipForm的窗体,
找到工具箱
通过拖拽工具箱里面的控件,实现自动化编程
using System.Windows.Forms;
namespace LubanAddin
{
public partial class TipForm : Form
{
public TipForm()
{
InitializeComponent();
}
}
}
Partial 关键词定义的类可以在多个地方被定义,最后编译的时候会被当作一个类来处理。这种带有关键字 Partial 的代码实现了界面和后台分离,但是类名相同
我们观察自动化创建的TipForm.cs的结构
我们打开TipForm.Designer.cs文件,可以看到,TipForm 的另一部分类是在这个文件中设计的。
namespace LubanAddin
{
partial class TipForm
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.SuspendLayout();
//
// TipForm
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(800, 450);
this.Name = "TipForm";
this.Text = "TipForm";
this.ResumeLayout(false);
}
#endregion
}
}
这个部分类中定义了我们使用的控件、事件委托、Dispose方法、InitializeComponent初始化方法等。因为这里面的代码都是自动生成的,因此设计成了一个部分类。
Partial是局部类型的意思。允许我们将一个类、结构或接口分成几个部分,分别实现在几个不同的.cs文件中。C#编译器在编译的时候仍会将各个部分的局部类型合并成一个完整的类。
我们可以通过拖拽,或者自己编程根据自己的需求实现窗体。我们拖拽的部分一般都会写在InitializeComponent函数体中,同时也会在partial class TipForm类中创建对应的私有控件对象,eg. private System.Windows.Forms.Button button;