使用C#开发数据库系统——事件处理

使用C#开发数据库应用数据系统

技术内容

1.2 事件处理

1.2.1 编写事件处理程序

1.2.2 技能训练

——————————————————————————————

第一章——初始Windows程序

1.2 事件处理

平时我们在计算机上的操作基本都是通过鼠标或者敲击键盘,系统就会有相应的反应。这些鼠标单击,鼠标释放,键盘键按下,键盘键释放都是 Windows 操作系统中的事件。Windows 操作系统本身就是通过事件来处理用户请求的。例如,单击 “开始” 按钮就会显示 “开始” 菜单,双击某文件夹图标,就会打开该文件夹窗口等。Windows 的这种随时响应用户的触发事件,并作出相应处理的机制就叫做事件驱动机制。

使用 Visual Studio 创建的 Windows 程序也是由事件驱动的。那么,怎么才能让程序知道发生了什么事件呢? .NET Framework 为窗体和控件定义了很多常用的事件,我们只要针对用户触发的事件,编写相应的事件处理程序即可。

1.2.1 编写事件处理程序

窗体和控件都有很多定义好的事件,比较常用的有窗体的 Load 事件,按钮的 Click 事件等。有关控件的具体事件,可以在 Visual Studio 中选择控件,在 “属性” 窗口中查看事件列表。

在 Visual Studio 中编写事件处理程序非常方便,步骤如下:

(1)单击要创建事件处理程序的窗体或控件。

(2)在 “属性” 窗口单击 “事件” 按钮。

(3)双击要处理的事件定位到事件处理方法。

(4)编写处理代码。

问题:如何实现 MySchool 窗体中 ”取消“ 按钮的功能,即单击 ”取消“ 按钮时,关闭登录窗体?

分析:当单击按钮时,会触发按钮的 Click 事件;当该事件发生时

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
MySchool namespace MySchool { partial class FrmLogin { /// <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() { System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(FrmLogin)); this.btnLogin = new System.Windows.Forms.Button(); this.btnCancel = new System.Windows.Forms.Button(); this.txtUserName = new System.Windows.Forms.TextBox(); this.txtPwd = new System.Windows.Forms.TextBox(); this.cboLoginType = new System.Windows.Forms.ComboBox(); this.lblLoginId = new System.Windows.Forms.Label(); this.lblPwd = new System.Windows.Forms.Label(); this.lblLoginType = new System.Windows.Forms.Label(); this.SuspendLayout(); // // btnLogin // this.btnLogin.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink; this.btnLogin.BackColor = System.Drawing.Color.Transparent; this.btnLogin.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("btnLogin.BackgroundImage"))); this.btnLogin.BackgroundImageLayout = System.Windows.Forms.ImageLayout.None; this.btnLogin.Cursor = System.Windows.Forms.Cursors.Hand; this.btnLogin.FlatAppearance.BorderColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(192)))), ((int)(((byte)(128))))); this.btnLogin.FlatAppearance.BorderSize = 0; this.btnLogin.FlatStyle = System.Windows.Forms.FlatStyle.Flat; this.btnLogin.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134))); this.btnLogin.Location = new System.Drawing.Point(181, 299); this.btnLogin.Name = "btnLogin"; this.btnLogin.Size = new System.Drawing.Size(84, 21); this.btnLogin.TabIndex = 3; this.btnLogin.Text = "登 录"; this.btnLogin.UseVisualStyleBackColor = false; // // btnCancel // this.btnCancel.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("btnCancel.BackgroundImage"))); this.btnCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel; this.btnCancel.FlatAppearance.BorderColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(192)))), ((int)(((byte)(128))))); this.btnCancel.FlatAppearance.BorderSize = 0; this.btnCancel.FlatStyle = System.Windows.Forms.FlatStyle.Flat; this.btnCancel.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134))); this.btnCancel.Location = new System.Drawing.Point(283, 299); this.btnCancel.Name = "btnCancel"; this.btnCancel.Size = new System.Drawing.Size(84, 21); this.btnCancel.TabIndex = 4; this.btnCancel.Text = "取 消"; this.btnCancel.UseVisualStyleBackColor = true; // // txtUserName // this.txtUserName.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; this.txtUserName.Location = new System.Drawing.Point(254, 190); this.txtUserName.Name = "txtUserName"; this.txtUserName.Size = new System.Drawing.Size(167, 23); this.txtUserName.TabIndex = 0; // // txtPwd // this.txtPwd.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; this.txtPwd.Location = new System.Drawing.Point(254, 227); this.txtPwd.Name = "txtPwd"; this.txtPwd.Size = new System.Drawing.Size(167, 23); this.txtPwd.TabIndex = 1; this.txtPwd.UseSystemPasswordChar = true; // // cboLoginType // this.cboLoginType.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; this.cboLoginType.Items.AddRange(new object[] { "系统管理员", "生"}); this.cboLoginType.Location = new System.Drawing.Point(254, 264); this.cboLoginType.Name = "cboLoginType"; this.cboLoginType.Size = new System.Drawing.Size(167, 22); this.cboLoginType.TabIndex = 2; // // lblLoginId // this.lblLoginId.AutoSize = true; this.lblLoginId.BackColor = System.Drawing.Color.Transparent; this.lblLoginId.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134))); this.lblLoginId.Location = new System.Drawing.Point(181, 194); this.lblLoginId.Name = "lblLoginId"; this.lblLoginId.Size = new System.Drawing.Size(67, 14); this.lblLoginId.TabIndex = 5; this.lblLoginId.Text = "用户名:"; // // lblPwd // this.lblPwd.AutoSize = true; this.lblPwd.BackColor = System.Drawing.Color.Transparent; this.lblPwd.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134))); this.lblPwd.Location = new System.Drawing.Point(181, 231); this.lblPwd.Name = "lblPwd"; this.lblPwd.Size = new System.Drawing.Size(68, 14); this.lblPwd.TabIndex = 6; this.lblPwd.Text = "密 码:"; // // lblLoginType // this.lblLoginType.AutoSize = true; this.lblLoginType.BackColor = System.Drawing.Color.Transparent; this.lblLoginType.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134))); this.lblLoginType.Location = new System.Drawing.Point(181, 268); this.lblLoginType.Name = "lblLoginType"; this.lblLoginType.Size = new System.Drawing.Size(82, 14); this.lblLoginType.TabIndex = 7; this.lblLoginType.Text = "登录类型:"; // // FrmLogin // this.AcceptButton = this.btnLogin; this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 14F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("$this.BackgroundImage"))); this.CancelButton = this.btnCancel; this.ClientSize = new System.Drawing.Size(506, 372); this.Controls.Add(this.cboLoginType); this.Controls.Add(this.lblLoginType); this.Controls.Add(this.lblPwd); this.Controls.Add(this.lblLoginId); this.Controls.Add(this.txtPwd); this.Controls.Add(this.txtUserName); this.Controls.Add(this.btnCancel); this.Controls.Add(this.btnLogin); this.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134))); this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle; this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon"))); this.MaximizeBox = false; this.Name = "FrmLogin"; this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; this.Text = "登录"; this.ResumeLayout(false); this.PerformLayout(); } #endregion private System.Windows.Forms.Button btnLogin; private System.Windows.Forms.Button btnCancel; private System.Windows.Forms.TextBox txtUserName; private System.Windows.Forms.TextBox txtPwd; private System.Windows.Forms.ComboBox cboLoginType; private System.Windows.Forms.Label lblLoginId; private System.Windows.Forms.Label lblPwd; private System.Windows.Forms.Label lblLoginType; } }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值