多窗体:新建 / 删除窗体,项目 1 登录示例;标识符支持中文

- 新建窗体:生成与 VS 新建窗体相同的 X.cs 和 X.Designer.cs,沿用工程命名空间;
  删除窗体(启动窗体不能删)
- 标签页按窗体分组,启动窗体排在最前并默认打开
- 运行时点哪个窗口哪个到最前;消息框始终在所有窗口之上
- 示例:项目 1 任务 1 登录与增加用户,两个窗体的 Designer.cs 由设计器引擎生成;
  修正书上未声明的 change 变量、关闭后重复 Show 已释放窗体的问题
- 设计器的正则支持中文控件名、类名、命名空间
- 修正:切换窗体时属性窗口的名称没有刷新
- 测试:多窗体端到端测试;新窗体模板测试

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
xuyueandClaude Opus 5.5 committed 2026-10-22 11:23:37 +08:00
1 parent 02d2f0eabc
commit 8feda289de
19 files changed
+739 -36

No files matched your search

+143
View File
@@ -0,0 +1,143 @@
namespace 登录
{
partial class AddUser
{
/// <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.LblUsername = new System.Windows.Forms.Label();
this.txtNewUser1 = new System.Windows.Forms.TextBox();
this.LblPwd = new System.Windows.Forms.Label();
this.txtPass1 = new System.Windows.Forms.TextBox();
this.LblCheckPwd = new System.Windows.Forms.Label();
this.txtPass2 = new System.Windows.Forms.TextBox();
this.btnOk = new System.Windows.Forms.Button();
this.btnCancel = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// LblUsername
//
this.LblUsername.AutoSize = true;
this.LblUsername.Location = new System.Drawing.Point(25, 27);
this.LblUsername.Name = "LblUsername";
this.LblUsername.Size = new System.Drawing.Size(41, 12);
this.LblUsername.TabIndex = 0;
this.LblUsername.Text = "用户名";
//
// txtNewUser1
//
this.txtNewUser1.Location = new System.Drawing.Point(90, 24);
this.txtNewUser1.Name = "txtNewUser1";
this.txtNewUser1.Size = new System.Drawing.Size(110, 21);
this.txtNewUser1.TabIndex = 1;
//
// LblPwd
//
this.LblPwd.AutoSize = true;
this.LblPwd.Location = new System.Drawing.Point(25, 57);
this.LblPwd.Name = "LblPwd";
this.LblPwd.Size = new System.Drawing.Size(29, 12);
this.LblPwd.TabIndex = 2;
this.LblPwd.Text = "密码";
//
// txtPass1
//
this.txtPass1.Location = new System.Drawing.Point(90, 54);
this.txtPass1.Name = "txtPass1";
this.txtPass1.PasswordChar = '*';
this.txtPass1.Size = new System.Drawing.Size(110, 21);
this.txtPass1.TabIndex = 3;
//
// LblCheckPwd
//
this.LblCheckPwd.AutoSize = true;
this.LblCheckPwd.Location = new System.Drawing.Point(25, 87);
this.LblCheckPwd.Name = "LblCheckPwd";
this.LblCheckPwd.Size = new System.Drawing.Size(53, 12);
this.LblCheckPwd.TabIndex = 4;
this.LblCheckPwd.Text = "确认密码";
//
// txtPass2
//
this.txtPass2.Location = new System.Drawing.Point(90, 84);
this.txtPass2.Name = "txtPass2";
this.txtPass2.PasswordChar = '*';
this.txtPass2.Size = new System.Drawing.Size(110, 21);
this.txtPass2.TabIndex = 5;
//
// btnOk
//
this.btnOk.Location = new System.Drawing.Point(35, 120);
this.btnOk.Name = "btnOk";
this.btnOk.Size = new System.Drawing.Size(75, 23);
this.btnOk.TabIndex = 6;
this.btnOk.Text = "确定";
this.btnOk.UseVisualStyleBackColor = true;
this.btnOk.Click += new System.EventHandler(this.btnOk_Click);
//
// btnCancel
//
this.btnCancel.Location = new System.Drawing.Point(125, 120);
this.btnCancel.Name = "btnCancel";
this.btnCancel.Size = new System.Drawing.Size(75, 23);
this.btnCancel.TabIndex = 7;
this.btnCancel.Text = "取消";
this.btnCancel.UseVisualStyleBackColor = true;
this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click);
//
// AddUser
//
this.AcceptButton = this.btnOk;
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(220, 160);
this.Controls.Add(this.btnCancel);
this.Controls.Add(this.btnOk);
this.Controls.Add(this.txtPass2);
this.Controls.Add(this.LblCheckPwd);
this.Controls.Add(this.txtPass1);
this.Controls.Add(this.LblPwd);
this.Controls.Add(this.txtNewUser1);
this.Controls.Add(this.LblUsername);
this.Name = "AddUser";
this.Text = "增加用户";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Label LblUsername;
private System.Windows.Forms.TextBox txtNewUser1;
private System.Windows.Forms.Label LblPwd;
private System.Windows.Forms.TextBox txtPass1;
private System.Windows.Forms.Label LblCheckPwd;
private System.Windows.Forms.TextBox txtPass2;
private System.Windows.Forms.Button btnOk;
private System.Windows.Forms.Button btnCancel;
}
}
+48
View File
@@ -0,0 +1,48 @@
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 登录
{
public partial class AddUser : Form
{
public AddUser()
{
InitializeComponent();
}
private void btnOk_Click(object sender, EventArgs e)
{
if (PublicData.intNum < 4)//确保还有足够的存储空间
{
if (txtPass1.Text.Equals(txtPass2.Text) && txtPass2.Text != "")//确保两次密码相同且非空
{
//存储新增用户
PublicData.SysUserName[PublicData.intNum] = txtNewUser1.Text;
PublicData.SysPwd[PublicData.intNum] = txtPass1.Text;
PublicData.intNum++;//增加实际存储数量
this.Close();//关闭当前窗体
}
else
{
MessageBox.Show("用户名或密码错误,请重新输入", "新增用户");
}
}
else
{
MessageBox.Show("用户名已满不能增加", "新增用户");
}
}
private void btnCancel_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
+133
View File
@@ -0,0 +1,133 @@
namespace 登录
{
partial class Login
{
/// <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.LblUsername = new System.Windows.Forms.Label();
this.txtUserName = new System.Windows.Forms.TextBox();
this.LblPwd = new System.Windows.Forms.Label();
this.txtPassword = new System.Windows.Forms.TextBox();
this.btnOk = new System.Windows.Forms.Button();
this.btnCancel = new System.Windows.Forms.Button();
this.btnAdd = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// LblUsername
//
this.LblUsername.AutoSize = true;
this.LblUsername.Location = new System.Drawing.Point(30, 30);
this.LblUsername.Name = "LblUsername";
this.LblUsername.Size = new System.Drawing.Size(53, 12);
this.LblUsername.TabIndex = 0;
this.LblUsername.Text = "用户名称";
//
// txtUserName
//
this.txtUserName.Location = new System.Drawing.Point(100, 27);
this.txtUserName.Name = "txtUserName";
this.txtUserName.Size = new System.Drawing.Size(160, 21);
this.txtUserName.TabIndex = 1;
//
// LblPwd
//
this.LblPwd.AutoSize = true;
this.LblPwd.Location = new System.Drawing.Point(30, 66);
this.LblPwd.Name = "LblPwd";
this.LblPwd.Size = new System.Drawing.Size(29, 12);
this.LblPwd.TabIndex = 2;
this.LblPwd.Text = "密码";
//
// txtPassword
//
this.txtPassword.Location = new System.Drawing.Point(100, 63);
this.txtPassword.Name = "txtPassword";
this.txtPassword.PasswordChar = '*';
this.txtPassword.Size = new System.Drawing.Size(160, 21);
this.txtPassword.TabIndex = 3;
//
// btnOk
//
this.btnOk.Location = new System.Drawing.Point(30, 105);
this.btnOk.Name = "btnOk";
this.btnOk.Size = new System.Drawing.Size(75, 23);
this.btnOk.TabIndex = 4;
this.btnOk.Text = "登录";
this.btnOk.UseVisualStyleBackColor = true;
this.btnOk.Click += new System.EventHandler(this.btnOk_Click);
//
// btnCancel
//
this.btnCancel.Location = new System.Drawing.Point(115, 105);
this.btnCancel.Name = "btnCancel";
this.btnCancel.Size = new System.Drawing.Size(75, 23);
this.btnCancel.TabIndex = 5;
this.btnCancel.Text = "取消";
this.btnCancel.UseVisualStyleBackColor = true;
this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click);
//
// btnAdd
//
this.btnAdd.Location = new System.Drawing.Point(200, 105);
this.btnAdd.Name = "btnAdd";
this.btnAdd.Size = new System.Drawing.Size(75, 23);
this.btnAdd.TabIndex = 6;
this.btnAdd.Text = "增加用户";
this.btnAdd.UseVisualStyleBackColor = true;
this.btnAdd.Click += new System.EventHandler(this.btnAdd_Click);
//
// Login
//
this.AcceptButton = this.btnOk;
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(300, 150);
this.Controls.Add(this.btnAdd);
this.Controls.Add(this.btnCancel);
this.Controls.Add(this.btnOk);
this.Controls.Add(this.txtPassword);
this.Controls.Add(this.LblPwd);
this.Controls.Add(this.txtUserName);
this.Controls.Add(this.LblUsername);
this.Name = "Login";
this.Text = "登录";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Label LblUsername;
private System.Windows.Forms.TextBox txtUserName;
private System.Windows.Forms.Label LblPwd;
private System.Windows.Forms.TextBox txtPassword;
private System.Windows.Forms.Button btnOk;
private System.Windows.Forms.Button btnCancel;
private System.Windows.Forms.Button btnAdd;
}
}
+57
View File
@@ -0,0 +1,57 @@
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 登录
{
public partial class Login : Form
{
public Login()
{
InitializeComponent();
}
public bool IsLogin = false;//定义登录标识
private void btnOk_Click(object sender, EventArgs e)
{
for (int i = 0; i < PublicData.SysUserName.Length; i++)
{
string usr = PublicData.SysUserName[i];
string pwd = PublicData.SysPwd[i];
if (txtUserName.Text == usr && txtPassword.Text == pwd && txtUserName.Text != "")//遍历用户名密码是否存在
{
IsLogin = true;//变更登录标识
MessageBox.Show("登录成功,欢迎进入!", "登录");
PublicData.intUser = i;//存储当前用户在数组中的索引
break;//结束循环
}
else
IsLogin = false;
}
if (IsLogin == false)
{
MessageBox.Show("无效的密码或用户名,请重试", "登录");
txtPassword.Focus();
}
}
private void btnCancel_Click(object sender, EventArgs e)
{
IsLogin = false;
this.Close();//关闭当前窗体
}
private void btnAdd_Click(object sender, EventArgs e)
{
AddUser addfrm = new AddUser();//实例化添加用户窗体
addfrm.Show();//显示增加用户窗体
}
}
}
+22
View File
@@ -0,0 +1,22 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace 登录
{
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Login());
}
}
}
+16
View File
@@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 登录
{
public class PublicData
{
public static string[] SysUserName = new string[4];//存储系统用户名
public static string[] SysPwd = new string[4];//存储系统用户密码
public static int intNum { get; set; }//当前数据存储的用户数量
public static int intUser { get; set; }//记录当前用户索引号
}
}
+4
View File
@@ -0,0 +1,4 @@
{
"title": "项目1 任务1 C#引入(登录与增加用户)",
"source": "教材书 p2–7,按书上代码整理。修正:删去未声明的变量 change;增加用户窗体关闭后会被释放,改为每次点击时新建"
}