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

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>
              亚洲国产欧美精品| 在线成人激情| 欧美一区二区精品久久911| 在线视频欧美日韩精品| 日韩亚洲欧美高清| 在线亚洲激情| 亚洲欧美日本国产有色| 亚洲综合视频1区| 欧美亚洲一区| 欧美成人高清| 国产精品女同互慰在线看| 国产欧美日本一区二区三区| 国产综合亚洲精品一区二| 亚洲黄色尤物视频| 亚洲主播在线| 女主播福利一区| 亚洲欧洲美洲综合色网| 亚洲天堂男人| 久热这里只精品99re8久| 欧美日韩免费一区| 免费在线欧美黄色| 一本大道久久a久久精品综合| 国产精品99久久久久久有的能看 | 亚洲国产精品ⅴa在线观看| 亚洲第一在线综合网站| 亚洲桃花岛网站| 老司机aⅴ在线精品导航| 国产精品成人免费| 亚洲国产精品va在线看黑人动漫| 一区二区日韩| 欧美肥婆bbw| 欧美一级视频一区二区| 欧美日韩精品一区二区在线播放 | 91久久国产综合久久| 亚洲免费视频在线观看| 免费在线观看成人av| 国产日韩亚洲欧美| 亚洲一区二区在线播放| 亚洲第一福利在线观看| 欧美影院一区| 国产精品家教| 亚洲视频久久| 亚洲精品久久久久久久久久久久| 久久精品在线免费观看| 国产精品美女午夜av| 一区二区三区精品| 最新日韩精品| 欧美a级理论片| 国语自产精品视频在线看一大j8 | 欧美高清在线观看| 亚洲电影免费观看高清| 久久久91精品国产一区二区精品| 在线一区二区视频| 欧美日韩成人一区| 正在播放欧美一区| 一本久久青青| 国产精一区二区三区| 午夜在线观看免费一区| 亚洲小视频在线| 国产精品久久7| 亚洲专区在线视频| 中文一区二区| 国产一区二区黄| 欧美在线视频免费观看| 午夜一级在线看亚洲| 国产三区精品| 久久人人九九| 毛片一区二区| 99在线热播精品免费| 极品少妇一区二区三区| 日韩视频第一页| 亚洲精品一区在线观看| 欧美精品一区二区在线播放| 中国日韩欧美久久久久久久久| 亚洲韩国精品一区| 欧美日韩在线亚洲一区蜜芽| 亚洲免费在线播放| 亚洲一二三区视频在线观看| 国产一区二区三区的电影| 美女免费视频一区| 欧美电影免费观看高清| 亚洲图片欧洲图片av| 午夜欧美大尺度福利影院在线看| 夜夜嗨av色一区二区不卡| 欧美日韩成人综合天天影院| 午夜精品999| 免费成人黄色av| 亚洲在线1234| 久久精品电影| 亚洲神马久久| 久久久久久久久久久久久女国产乱| 亚洲精品午夜精品| 亚洲永久在线| 99精品国产在热久久婷婷| 亚洲欧美乱综合| 亚洲美女在线国产| 午夜精品在线视频| 99re6这里只有精品视频在线观看| 一区二区三区免费网站| 亚洲二区免费| 亚洲欧美日韩精品一区二区| 亚洲国产视频一区| 亚洲欧美国产精品桃花| 亚洲精品免费在线观看| 亚洲欧美综合v| 一本色道久久综合亚洲91| 午夜精品久久| 亚洲精品在线观| 久久www成人_看片免费不卡| 99精品国产在热久久| 久久一区视频| 欧美一区二区在线播放| 欧美日韩国产专区| 男人插女人欧美| 国产日产精品一区二区三区四区的观看方式 | 国产精品日韩在线观看| 欧美mv日韩mv亚洲| 国产精品久久毛片a| 亚洲福利在线看| 国产亚洲亚洲| 亚洲在线网站| 亚洲一区视频| 国产精品爱啪在线线免费观看| 欧美r片在线| 国产中文一区二区三区| 亚洲制服欧美中文字幕中文字幕| 亚洲天堂网在线观看| 欧美精品自拍| 亚洲精品专区| 久久久av毛片精品| 欧美激情第一页xxx| 久久激情五月丁香伊人| 国产精品免费久久久久久| 一区二区国产日产| 夜夜爽av福利精品导航| 欧美成人激情视频免费观看| 毛片一区二区三区| 狠狠色狠色综合曰曰| 亚洲精品网址在线观看| 久久伊人亚洲| 欧美激情性爽国产精品17p| 亚洲高清视频在线| 免费欧美网站| 亚洲片国产一区一级在线观看| 亚洲三级免费| 欧美午夜精品久久久久久久| 亚洲图色在线| 久久精品国产久精国产爱| 国产欧美精品在线播放| 欧美在线不卡| 亚洲国产黄色片| 中文欧美日韩| 国产日韩欧美日韩大片| 久久精品官网| 亚洲精品乱码久久久久久久久 | 欧美日韩国产精品专区| av成人老司机| 久久aⅴ国产紧身牛仔裤| 精东粉嫩av免费一区二区三区| 久久五月激情| 在线亚洲伦理| 久久久久久久久久久久久9999| 亚洲春色另类小说| 欧美日韩中文字幕| 久久aⅴ国产欧美74aaa| 亚洲国产成人tv| 亚洲欧美在线免费| 在线观看日韩欧美| 欧美性视频网站| 久久免费国产精品1| 一区二区三区国产在线观看| 久久久精品网| 亚洲女人av| 亚洲国产精品t66y| 国产区在线观看成人精品| 欧美精品自拍偷拍动漫精品| 亚洲在线观看视频网站| 亚洲高清在线观看一区| 欧美专区18| 在线一区日本视频| 狠狠色狠狠色综合系列| 国产精品国产a级| 欧美成人福利视频| 久久精品中文字幕一区| 亚洲欧美日韩国产中文| 亚洲精品中文字幕在线| 欧美a级理论片| 久久精品免费观看| 亚洲欧美精品在线| 99视频精品免费观看| 伊人久久久大香线蕉综合直播| 国产精品二区在线| 欧美日韩1区2区| 欧美黑人一区二区三区| 欧美在线播放一区二区| 亚洲天堂视频在线观看| 日韩亚洲欧美在线观看| 亚洲精品女人| 欧美成人tv| 欧美成人国产一区二区|