目录
继承窗体就是根据现有的窗体的结构创建一个与其一样的新窗体,这种从现有窗体继承的过程称为可视化继承。为了从一个窗体继承,包含该窗体的文件或命名空间必须已编译成可执行文件或DLL。
创建继承窗体的方法有两种:一种是通过变成方式创建继承窗体,另一种是使用继承选择器创建继承窗体。
一、通过编程方式创建继承窗体
(1)在默认窗体Form1中添加控件
在默认窗体Form1中添加TextBox控件、Button控件、Label控件各一个。
鼠标选中Button控件,在右下角属性窗口修改表格Text内容为:“确认输入”;
鼠标选中Label控件,在右下角属性窗口修改表格Text内容为:“空”;
添加控件后,Form1.Designer.cs会自动生成控件相关的代码。
(2)添加控件的事件
在TextBox控件中添加文本内容改变事件,在Button控件中添加点击事件,Label控件中不需要添加事件。
鼠标选中Button控件,在右下角事件窗口选择表格Click,点击其右侧空白的表格内容,会在Form1.cs和Form1.Designer.cs中自动生成点击事件相关代码;
鼠标选中TextBox控件,在右下角事件窗口选择表格TextChanged,点击其右侧空白的表格内容,会在Form1.cs和Form1.Designer.cs中自动生成文本内容改变事件相关代码;
//Form1中添加文本框、标签、按钮控件
namespace App1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Button1_Click(object sender, EventArgs e)
{
}
private void TextBox1_TextChanged(object sender, EventArgs e)
{
}
}
}
//Form1.Designer.cs
namespace App1
{
partial class Form1
{
/// <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()
{
textBox1 = new TextBox();
label1 = new Label();
button1 = new Button();
SuspendLayout();
//
// textBox1
//
textBox1.Location = new Point(54, 33);
textBox1.Name = "textBox1";
textBox1.Size = new Size(129, 23);
textBox1.TabIndex = 0;
textBox1.TextChanged += TextBox1_TextChanged; //自动生成事件
//
// label1
//
label1.AutoSize = true;
label1.Location = new Point(54, 104);
label1.Name = "label1";
label1.Size = new Size(0, 17);
label1.TabIndex = 1;
//
// button1
//
button1.BackColor = Color.IndianRed;
button1.Location = new Point(245, 27);
button1.Name = "button1";
button1.Size = new Size(90, 35);
button1.TabIndex = 2;
button1.Text = "确认输入";
button1.UseVisualStyleBackColor = false;
button1.Click += Button1_Click; //自动生成事件
//
// Form1
//
AutoScaleDimensions = new SizeF(7F, 17F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(384, 161);
Controls.Add(button1);
Controls.Add(label1);
Controls.Add(textBox1);
Name = "Form1";
StartPosition = FormStartPosition.CenterScreen;
Text = "Form1";
ResumeLayout(false);
PerformLayout();
}
#endregion
//自动生成控件
private TextBox textBox1;
private Label label1;
private Button button1;
}
}
(3)添加事件的内容
在Form1.cs中相关事件的程序段中手动输入相关事件的内容。
//Form1中添加文本框、标签、按钮控件
namespace App1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Button1_Click(object sender, EventArgs e)
{
if (textBox1.Text == "") //如果文本框没有输入任何内容则提示
{
MessageBox.Show("请输入内容");
}
else //如果文本框有输入内容则标签显示
{
label1.Text = "您输入的文本:" + textBox1.Text;
}
}
private void TextBox1_TextChanged(object sender, EventArgs e)
{
if (textBox1.Text == "") //如果文本框没有输入任何内容则标签内容为空
{
label1.Text = "";
}
}
}
}
(4)运行
生成并显示Form1
二、通过编程方式创建继承窗体
(1)修改Form2为继承窗体
在项目中增加窗体Form2,并把它修改为继承窗体。
//Form2.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 App1
{
//public partial class Form2 : Form //继承窗提前,Form2 继承于窗体Form
public partial class Form2 : App1.Form1 //继承窗提前,Form2 继承于窗体App1.Form1
{
public Form2()
{
InitializeComponent();
}
}
}
(2)修改Form2的属性
Form2可以继承Form1的多数属性,比如:控件、启动位置,等。
有些不能继承来的属性需要手动修改,比如:窗体大小,等。
修改本例中Form2大小为与Form1相同。
(3)修改启动窗体
程序默认的启动窗体是Form1,在Program.cs中修改启动窗体为Form2。
//Program.cs
namespace App1
{
internal static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
// To customize application configuration such as set high DPI settings or default font,
// see https://aka.ms/applicationconfiguration.
ApplicationConfiguration.Initialize();
//Application.Run(new Form1());
Application.Run(new Form2()); //继承窗体后默认运行窗体2
}
}
}
(4)运行
生成并显示Form2,与生成并显示Form1操作相同。
三、使用继承选择器创建继承窗体
继承窗体或其他对象最简单的方法是使用“继承选择器”。通过继承选择器可以使用已经在其他解决方案中创建的代码或用户界面。
为了使用“继承选择器”对话框从某个窗体继承,包含该窗体的项目必须已经生成为可执行文件或DLL。
使用“继承选择器”继承已有窗体解决方案必须选择“Windows窗体应用(.NET Framework)”,不得选择“Windows窗体应用”,否则“添加新项”的时候找不到“继承的窗体”模板。(通过编程的方法继承窗体时不必计较如何选择)
选择Windows窗体应用(.NET Framework)生成解决方案时:
选择Windows窗体应用生成解决方案时:
(1)通过继承选择器选择继承的窗体
在上述的解决方案中,通过继承选择器选择已经生成的Form1(或Form2)为继承窗体,创建窗体Form3。
鼠标选中项目,右键,添加→新建项→Windows Forms→继承的窗体(Windows窗体)→Form1
//Form3.cs
//继承选择器选择继承的窗体
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace App2
{
public partial class Form3 : App2.Form1
{
public Form3()
{
InitializeComponent();
}
}
}
对生成的Form3.cs[设计]进行修饰,比如修改启动位置、背景颜色等。
修改默认的生成并显示的窗体为Form3。
(2)运行
生成并显示Form3.