C#Windows应用程序开发之事件处理器

开发 后端
C#Windows应用程序开发之事件处理器的实现需要注意什么呢?C#Windows应用程序开发之事件处理器是如何实现的呢?那么本文就向你介绍这些内容。

C#Windows应用程序开发之事件处理器的前言:通常windows应用程序都有相似的特征:控件、菜单、工具条、状态栏等等。每次我们开始作一个新的windows应用程序时都是以相同的事情开始:建立项目,添加控件和事件处理器。如果我们有一个模板,那么我们就可以节约大量的时间了。

在介绍如何建立模板的过程中,将涉及大量的微软.net framework类库的基本知识。如果你没有使用集成开发环境那么本文介绍的模板对你将非常有用,如果你使用了visual studio.net这样的集成开发环境你也可以从中了解控件的工作方式,这对你也是很有用的。

C#Windows应用程序开发之事件处理器

在windows程序设计中添加事件处理器是最重要的任务。事件处理器保证了程序与用户交互,同时完成其他重要的功能。在c#中你可以给控件和菜单事件添加事件处理器以俘获你想处理的事件,下面的代码给Button控件的click事件设计了一个事件处理器:

  1. button.Click += new System.EventHandler(this.button_Click);  

C#Windows应用程序开发之事件处理器之button_Click事件处理器必须被处理:

  1. private void button_Click(Object sender, System.EventArgs e) {   
  2. MessageBox.Show("Thank you.""The Event Information");   
  3. }  

C#Windows应用程序开发之事件处理器之MenuItem 对象在实例化的同时可以给赋以一个事件处理器:

  1. fileNewMenuItem = new MenuItem("&New",   
  2. new System.EventHandler(this.fileNewMenuItem_Click), Shortcut.CtrlN);   
  3.  
  4. fileOpenMenuItem = new MenuItem("&Open",   
  5. new System.EventHandler(this.fileOpenMenuItem_Click), Shortcut.CtrlO);   
  6.  
  7. fileSaveMenuItem = new MenuItem("&Save",   
  8. new System.EventHandler(this.fileSaveMenuItem_Click), Shortcut.CtrlS);   
  9.  
  10. fileSaveAsMenuItem = new MenuItem("Save &As",   
  11. new System.EventHandler(this.fileSaveAsMenuItem_Click));   
  12.  
  13. fileMenuWithSubmenu = new MenuItem("&With Submenu");   
  14.  
  15. submenuMenuItem = new MenuItem("Su&bmenu",   
  16. new System.EventHandler(this.submenuMenuItem_Click));   
  17.  
  18. fileExitMenuItem = new MenuItem("E&xit",   
  19. new System.EventHandler(this.fileExitMenuItem_Click));   

C#Windows应用程序开发之事件处理器遇到的问题:你不能给工具按钮指派一个事件处理器,但可以给工具条指派一个事件处理器:

  1. toolBar.ButtonClick += new   
  2.  
  3. ToolBarButtonClickEventHandler(this.toolBar_ButtonClick);   
  4.  
  5. protected void toolBar_ButtonClick(Object sender, ToolBarButtonClickEventArgs   
  6.  
  7. e) {   
  8.  
  9. // Evaluate the Button property to determine which button was clicked.   
  10. switch (toolBar.Buttons.IndexOf(e.Button)) {   
  11. case 1:   
  12. MessageBox.Show("Second button.""The Event Information");   
  13. break;   
  14. case 2:   
  15. MessageBox.Show("third button""The Event Information");   
  16. break;   
  17. case 3:   
  18. MessageBox.Show("fourth button.""The Event Information");   
  19. break;   
  20. }   

C#Windows应用程序开发之事件处理器的理解:例子中也给窗体的close事件设计了一个事件处理器,通过重载OnClosing方法你可以接收用户点击窗体的X按钮,这样你可以取消关闭事件:

  1. protected override void OnClosing(CancelEventArgs e) {   
  2. MessageBox.Show("Exit now.""The Event Information");   

C#Windows应用程序开发之事件处理器的基本情况就向你介绍到这里,希望对你学习和理解C#Windows应用程序开发之事件处理器有所帮助。

【编辑推荐】

  1. C#实现Windows后台服务实例浅析
  2. C#Windows应用程序开发之创建窗体
  3. C#Windows应用程序开发之窗体控件
  4. C#Windows应用程序开发之添加菜单
  5. C#Windows应用程序开发之添加状态条
责任编辑:仲衡 来源: builder.com.cn
相关推荐

2009-08-14 17:27:30

C#Windows应用

2009-08-14 17:36:20

C#Windows应用

2009-08-14 17:55:52

C#Windows应用

2009-08-14 17:43:20

C#Windows应用

2009-08-14 18:04:59

C#Windows应用

2009-08-14 17:51:32

C#Windows应用

2009-08-14 14:25:09

Windows服务程序

2009-08-14 15:54:50

Windows服务程序C#Windows服务

2009-08-14 14:17:16

C#Windows服务

2009-08-14 16:24:00

Windows服务程序

2009-08-14 14:45:03

C#Windows服务

2009-08-14 15:19:38

Windows服务程序Windows服务

2009-08-14 15:06:08

Windows服务程序

2009-08-14 15:47:18

C#Windows服务

2011-08-02 17:58:09

iPhone开发 事件

2009-08-24 14:19:27

C# Windows应

2009-08-14 16:48:39

C#Windows服务

2015-11-06 13:59:01

JavaScript事件处理

2009-08-14 13:41:13

C#Windows服务

2009-08-14 10:50:09

Windows服务介绍
点赞
收藏

51CTO技术栈公众号