青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

C++ Programmer's Cookbook

{C++ 基礎} {C++ 高級} {C#界面,C++核心算法} {設計模式} {C#基礎}

Event Handling in C#(轉)

 

In a previous article, we have seen a sneak preview about event handling in C#. In this article, we will examine the concept in detail with the help of relevant examples.

Understanding the Basic Technique

Actions and events serve an important part in every GUI-based application. These are equally important in the same way because we are arranging components using either Visual Studio .NET or by applying codes. It's these actions that instruct the program what to do when something happens.

For example, when a user performs a mouse click or a keyboard operation, some kind of event is taking place. If the user does not perform that operation, nothing will happen. Think of a situation where you are not performing a mouse click or a keyboard operation after booting the computer. Before going forward, let's discuss how different Application Programming Interfaces (APIs) handle events.

Microsoft Foundation Classes (MFC)

These classes are based upon the Microsoft Win32 API. Normally, development work is done through Visual C++; programming using this API is a tedious task. A developer would have to learn complex set of theories and syntaxes.

Java API

Java provides a nice set of packages and classes such as the Abstract Windowing Toolkit (AWT) and Swing packages to perform GUI-based programming. These classes and packages also provide functionalities for handling events. In Java, you have to learn the concept of Interfaces for applying actions. The main difficulty is that you should learn and remember all methods in the corresponding Interfaces, failing which you would get compile-time errors. As compared to Java, event handling in C# is much more simplified. It's possible to handle various mouse- and key-related events quickly and in a more efficient manner.

The basic principles behind event handling in C# is elaborated below. These principles are applicable to all languages under the .NET framework.

  1. Invoke the related event, such as Click, Key Press, and so forth by supplying a custom method using += operator as shown here:
    b1.Click += new EventHandler(Your Method Name)
    
  2. While applying the above method, it should conform to a delegate of the class System.EventHandler, as shown in the following code fragment:
    public delegate void EventHandler(object sender, Event e) {}
    

In the above code, the first argument indicates the object sending the event and the second argument contains information for the current event. You can use this argument object, here e, to handle functionalities associated with the related event.

Triggering a Button

In this session, we will examine how to activate a WinForm button.

As already outlined, you need not worry about the placement of controls if you are using Visual C# .NET. As you place buttons and text boxes, the built-in editor will automatically create codes in the background. Double-clicking a control takes you to the Form Editor's area, where you can straightaway type your codes. For our examples, we use Notepad as our editor and .NET SDK for compiling and executing the applications.

Use your favorite editor to enter the code shown in Listing 1 and save it as Butevent.cs. Finally, compile and execute the program.

Listing 1

// Compilation   :  csc Butevent.cs
// Execution     :  Butevent

using System;
using System.Windows.Forms;
using System.Drawing;

public class Butevent:Form  {
   TextBox t1 = new TextBox();
   Button b1 = new Button();

   public Butevent() {
     this.Text = "C# Program ";
     t1.Location = new Point(20,30);
     b1.Text = "Click here to activate";
     b1.Location = new Point(20,55);
     b1.Size = new Size(150,20);

     // Invoking Method or EventHandler
     b1.Click+=new EventHandler(OnClick);

     this.Controls.Add(t1);
     this.Controls.Add(b1);

     // Invoking Method or EventHandler
     this.Resize += new EventHandler(OnResize);
   }

   //Applying EventHandler
   public void OnResize(object sender,EventArgs ee) {
      MessageBox.Show("oops! Form Resized");
   }

   //Applying EventHandler
   public void OnClick(object sender,EventArgs e) {
      t1.Text = "Hello C#";
   }

   public static void Main() {
      Application.Run(new Butevent());
   }

}

There are two kinds of processes going on in the above piece of code. One is that, upon clicking the button (b1), "Hello C#" would be printed inside the Textbox. Another is when you resize the form; a message box pops up with the message as shown in the above code. Locate the message yourself by verifying the code.

Working with Mouse and Key Events

We will examine a few mouse- and key-related events in this last session of this article.

If you activate something via the mouse, the mouse event takes place whereas if you are using the keyboard, a key event occurs. There are various types of these events, such as pressing the mouse, releasing the mouse, entering the mouse, pressing the keyboard, and so forth. First, we will cover mouse events.

Handling Mouse Events

The Control class specifies various events using the mouse. One such event is MouseUp. You have to apply this event in your program as shown in Listing 2:

Listing 2

// Compilation   :  csc Mousedemo.cs
// Execution     :  Mousedemo

using System;
using System.Windows.Forms;
using System.Drawing;

public class Mousedemo:Form  {

   public Mousedemo()  {
     this.MouseUp += new MouseEventHandler(OnMouseup);
   }

   public void OnMouseup(object sender,MouseEventArgs e)   {
     this.Text = "Current Position (" +e.X + " , " + e.Y +")";
   }

   public static void Main()  {
     Application.Run(new Mousedemo());
   }

}

As usual, compile and execute the above code. The current mouse coordinates will be displayed on the Form's title bar. e.X and e.Y imply the X and Y coordinates, respectively. Other popular mouse events are Click, DoubleClick, MouseEnter, and MouseLeave. These events also can be handled in the similar way as outlined in the above code. Next, we will examine key-related events, which are triggered via the keyboard.

Using Keyboard Events

Every modern programming language contains all necessary functionalities for handling keyboard-related events. C# also provides us with three events. They are KeyPress, KeyUp, and KeyDown, which you can use to handle keyboard events.

Listing 3 shows the usage of the KeyUp event. As usual, enter the code given below using your editor.

Listing 3

// Compilation   :  csc Keydemo.cs
// Execution     :  Keydemo

using System;
using System.Windows.Forms;
using System.Drawing;

public class Keydemo:Form {

   public Keydemo() {
     this.KeyUp += new KeyEventHandler(OnKeyPress);
   }

   public void OnKeyPress(object sender, KeyEventArgs e)   {
     MessageBox.Show(e.KeyCode.ToString(), "Your input");
   }

   public static void Main()  {
     Application.Run(new Keydemo());
   }

}

Upon execution, press any key and you will be able to view a message box with the corresponding key code in it. Cool, isn't it?

-------------------------------------------

About the Author

Anand Narayanaswamy is a Microsoft MVP (Microsoft Most Valuable Professional) who works as a freelance Web/Software developer and technical writer. He runs and maintains learnxpress.com, and provides free technical support to users. His areas of interest include Web development, Software development using Visual Basic, and in the design and preparation of courseware, technical articles, and tutorials.

posted on 2005-11-15 16:37 夢在天涯 閱讀(981) 評論(0)  編輯 收藏 引用 所屬分類: C#/.NET

公告

EMail:itech001#126.com

導航

統計

  • 隨筆 - 461
  • 文章 - 4
  • 評論 - 746
  • 引用 - 0

常用鏈接

隨筆分類

隨筆檔案

收藏夾

Blogs

c#(csharp)

C++(cpp)

Enlish

Forums(bbs)

My self

Often go

Useful Webs

Xml/Uml/html

搜索

  •  

積分與排名

  • 積分 - 1811733
  • 排名 - 5

最新評論

閱讀排行榜

青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
      <noscript id="pjuwb"></noscript>
            <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
              <dd id="pjuwb"></dd>
              <abbr id="pjuwb"></abbr>
              午夜在线电影亚洲一区| aⅴ色国产欧美| 亚洲亚洲精品在线观看| 久久综合九色99| 久久久久久久久一区二区| 亚洲国产美女| 久久综合色婷婷| 麻豆成人综合网| 欧美资源在线| 久久国产一区二区三区| 久久精品99久久香蕉国产色戒| 亚洲国产影院| 亚洲二区在线观看| 亚洲一二三区在线| 亚洲欧美日韩在线观看a三区 | 亚洲国产精品va在线看黑人 | 国产精品电影在线观看| 久久福利资源站| 国产精品专区h在线观看| 亚洲午夜精品在线| 欧美一区二区三区精品电影| 噜噜噜躁狠狠躁狠狠精品视频| 久久国产精品一区二区三区四区| 你懂的网址国产 欧美| 亚洲国产精品悠悠久久琪琪| 亚洲精选久久| 亚洲欧美精品在线| 久久这里有精品15一区二区三区| 卡一卡二国产精品| 亚洲国产精品ⅴa在线观看| 亚洲精品日本| 亚洲韩国日本中文字幕| 在线播放豆国产99亚洲| 亚洲国产婷婷| 中日韩高清电影网| 久久精品亚洲乱码伦伦中文| 欧美成人中文字幕在线| 国产精品电影在线观看| 亚洲精品激情| 久久影音先锋| 亚洲欧美日韩精品一区二区| 欧美14一18处毛片| 欧美亚洲一区| 国产精品日韩欧美综合| 亚洲国产综合在线| 久久久久久69| 久久精品国产99国产精品澳门| 国产精品videossex久久发布| 亚洲国产91| 欧美国产精品劲爆| 欧美福利一区二区三区| 亚洲国产一区在线| 最新精品在线| 欧美日韩综合不卡| 亚洲免费影视| 欧美在线视频不卡| 亚洲欧洲日韩在线| 亚洲淫片在线视频| 亚洲激情欧美| 亚洲欧美日韩人成在线播放| 影音先锋中文字幕一区| 狂野欧美一区| 欧美精品18| 一区二区高清在线观看| 亚洲精品久久7777| 欧美亚韩一区| 欧美夜福利tv在线| 亚洲欧美国产三级| 亚洲精品国产精品国自产观看| 久久一二三四| 日韩一区二区免费高清| 亚洲视频一区| 国产一区二区三区日韩| 一个色综合导航| 欧美在线3区| 亚洲精品一线二线三线无人区| 亚洲狼人精品一区二区三区| 午夜精品久久久久99热蜜桃导演| 欧美一区二区三区喷汁尤物| 最新中文字幕一区二区三区| 亚洲午夜精品在线| 99精品欧美| 午夜精品久久久99热福利| 99视频在线观看一区三区| 欧美一区亚洲一区| 欧美一级久久| 国产午夜精品全部视频播放| 欧美高清在线播放| 好看的av在线不卡观看| 久久精品视频播放| 久久久噜噜噜久噜久久| 国内精品视频666| 欧美成人午夜| 欧美综合国产| 中国女人久久久| 欧美亚洲在线观看| 一区二区亚洲精品国产| 亚洲国产成人av好男人在线观看| 亚洲欧美成人精品| 日韩视频专区| 国产精品久久久久久久一区探花| 欧美在线视频播放| av成人黄色| 久久天天躁狠狠躁夜夜av| 欧美一区二区播放| 久久久免费精品视频| 久久精品男女| 在线一区免费观看| 亚洲性感激情| 狠狠综合久久av一区二区小说| 久久久99爱| 在线观看日韩专区| 亚洲欧洲在线免费| 激情欧美一区二区三区在线观看| 日韩亚洲综合在线| 国产亚洲精品高潮| 久久岛国电影| 狂野欧美性猛交xxxx巴西| 亚洲每日在线| 韩国自拍一区| 国产欧美精品xxxx另类| 亚洲一区免费观看| 亚洲女人天堂成人av在线| 国产日韩精品一区二区三区| 久久久久一区二区三区| 一本色道**综合亚洲精品蜜桃冫| 欧美一区激情| 一区二区视频欧美| 国产一区二区三区在线观看免费 | 欧美三级电影大全| 久热精品在线| 国产久一道中文一区| 久久精品亚洲乱码伦伦中文| 久久天天狠狠| 欧美伊人久久久久久久久影院 | 久久精品99久久香蕉国产色戒| 国产精品美女www爽爽爽| 免费成人你懂的| 久久久久国产精品一区| 久久久www免费人成黑人精品| 99热这里只有成人精品国产| 欧美诱惑福利视频| 欧美大尺度在线| 亚洲人体1000| a91a精品视频在线观看| 老司机精品视频一区二区三区| 午夜精品国产更新| 亚洲天堂av在线免费| 欧美一区二区三区在线| 欧美在线播放一区二区| 亚洲欧美日韩国产综合在线| 亚洲久久一区二区| 国语自产精品视频在线看| 国产欧美一区二区色老头| 精品不卡视频| 午夜精品影院| 欧美激情在线狂野欧美精品| 一区二区三区久久久| 在线性视频日韩欧美| 午夜精品视频网站| 中国女人久久久| 亚洲欧美国产制服动漫| 老牛影视一区二区三区| 国产欧美日韩视频在线观看| 亚洲韩国一区二区三区| 亚洲性感激情| 久久久久一区二区三区四区| 欧美一区二区三区免费观看视频 | 久久激情综合| 久久精品国产亚洲aⅴ| 欧美不卡在线视频| 国产视频久久久久久久| 一本久久青青| 狠狠久久亚洲欧美| 国产精品色一区二区三区| 欧美一区二区三区在线播放| 国产精品久久久久av免费| 韩国成人福利片在线播放| 亚洲天天影视| 亚洲欧洲一区二区三区久久| 久久久久亚洲综合| 性欧美18~19sex高清播放| 欧美国产日韩视频| 欧美一区二区免费| 欧美午夜在线一二页| 欧美伊人久久大香线蕉综合69| 日韩网站免费观看| 亚洲精品一区中文| 99亚洲视频| 国产亚洲美州欧州综合国| 欧美激情va永久在线播放| 国产婷婷成人久久av免费高清 | 国产欧美综合一区二区三区| 欧美亚洲三区| 午夜欧美理论片| 欧美一区二区三区免费在线看| 国产精品成人观看视频免费| 亚洲欧美日韩国产一区二区三区| 亚洲国产精品久久久久秋霞蜜臀| 亚洲国产一区在线观看|