📄 frmmain.cs
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace ApplicationExceptionTest
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class frmMain : System.Windows.Forms.Form
{
private System.Windows.Forms.Label lblName;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox txtName;
private System.Windows.Forms.TextBox txtEmail;
private System.Windows.Forms.Button btnSubmit;
private System.Windows.Forms.Button btnClose;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public frmMain()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.lblName = new System.Windows.Forms.Label();
this.label1 = new System.Windows.Forms.Label();
this.txtName = new System.Windows.Forms.TextBox();
this.txtEmail = new System.Windows.Forms.TextBox();
this.btnSubmit = new System.Windows.Forms.Button();
this.btnClose = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// lblName
//
this.lblName.Location = new System.Drawing.Point(8, 8);
this.lblName.Name = "lblName";
this.lblName.TabIndex = 0;
this.lblName.Text = "姓名:";
this.lblName.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
//
// label1
//
this.label1.Location = new System.Drawing.Point(8, 32);
this.label1.Name = "label1";
this.label1.TabIndex = 1;
this.label1.Text = "Email:";
this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
//
// txtName
//
this.txtName.Location = new System.Drawing.Point(112, 8);
this.txtName.Name = "txtName";
this.txtName.Size = new System.Drawing.Size(160, 21);
this.txtName.TabIndex = 2;
this.txtName.Text = "";
//
// txtEmail
//
this.txtEmail.Location = new System.Drawing.Point(112, 32);
this.txtEmail.Name = "txtEmail";
this.txtEmail.Size = new System.Drawing.Size(160, 21);
this.txtEmail.TabIndex = 3;
this.txtEmail.Text = "";
//
// btnSubmit
//
this.btnSubmit.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.btnSubmit.Location = new System.Drawing.Point(112, 64);
this.btnSubmit.Name = "btnSubmit";
this.btnSubmit.Size = new System.Drawing.Size(75, 21);
this.btnSubmit.TabIndex = 4;
this.btnSubmit.Text = "录入(&S)";
this.btnSubmit.Click += new System.EventHandler(this.btnSubmit_Click);
//
// btnClose
//
this.btnClose.DialogResult = System.Windows.Forms.DialogResult.Cancel;
this.btnClose.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.btnClose.Location = new System.Drawing.Point(200, 64);
this.btnClose.Name = "btnClose";
this.btnClose.Size = new System.Drawing.Size(75, 21);
this.btnClose.TabIndex = 5;
this.btnClose.Text = "关闭(&C)";
this.btnClose.Click += new System.EventHandler(this.btnClose_Click);
//
// frmMain
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.CancelButton = this.btnClose;
this.ClientSize = new System.Drawing.Size(292, 101);
this.Controls.Add(this.btnClose);
this.Controls.Add(this.btnSubmit);
this.Controls.Add(this.txtEmail);
this.Controls.Add(this.txtName);
this.Controls.Add(this.label1);
this.Controls.Add(this.lblName);
this.MaximizeBox = false;
this.Name = "frmMain";
this.Text = "自定义异常";
this.Load += new System.EventHandler(this.frmMain_Load);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new frmMain());
}
private void frmMain_Load(object sender, System.EventArgs e)
{
}
private void btnSubmit_Click(object sender, System.EventArgs e)
{
if(txtName.Text.Length == 0 && txtEmail.Text.Length == 0)
{
MessageBox.Show("请填写姓名和Email。", "填写不完整", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Information);
return;
}
try
{
SaveInfo(txtName.Text, txtEmail.Text);
}
catch(EmailErrorException ex)
{
MessageBox.Show(ex.Message, "Email格式错误",System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Information);
return;
}
MessageBox.Show("保存成功。", "成功", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Information);
}
private bool SaveInfo(string name, string email)
{
string[] subStrings = email.Split('@');
if(subStrings.Length != 2)
{
throw new EmailErrorException();
}
else
{
int index = subStrings[1].IndexOf(".");
if(index <= 0)
{
throw new EmailErrorException();
}
if(subStrings[1][subStrings[1].Length -1] == '.')
{
throw new EmailErrorException();
}
}
return true;
}
private void btnClose_Click(object sender, System.EventArgs e)
{
this.Close();
Application.Exit();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -