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

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>
              亚洲在线观看| 久久精品综合一区| 国产精品色网| 久久精品主播| 久久久久免费观看| 亚洲精品小视频| 99精品视频免费观看视频| 欧美视频精品在线观看| 欧美一区二区三区在| 久久亚洲欧美| 一区二区三区波多野结衣在线观看| 日韩一级不卡| 国产日韩欧美自拍| 亚洲第一黄色网| 欧美日韩91| 久久精品麻豆| 欧美国产精品久久| 久久国产精品免费一区| 可以看av的网站久久看| 亚洲一区bb| 欧美在线观看视频一区二区三区| 亚洲国产精品一区二区www在线| 最新69国产成人精品视频免费| 国产精品国内视频| 欧美成人午夜激情在线| 国产精品家庭影院| 欧美激情一区二区三区在线| 国产精品国产三级国产专播精品人| 久久夜色精品国产欧美乱极品| 欧美日韩成人| 蜜桃久久精品乱码一区二区| 国产精品高精视频免费| 欧美激情视频在线播放| 国产原创一区二区| 一本色道久久88综合亚洲精品ⅰ| 激情综合色丁香一区二区| 中国女人久久久| 亚洲激情电影中文字幕| 欧美在线播放高清精品| 午夜精品一区二区三区电影天堂 | 久久精品亚洲一区二区三区浴池| 亚洲美女中出| 久久婷婷国产综合精品青草| 亚洲欧美日韩综合| 欧美日韩国产色综合一二三四| 久久久欧美一区二区| 国产精品永久免费观看| 一本色道久久精品| 99亚洲视频| 欧美极品在线播放| 欧美顶级少妇做爰| 黄色成人av| 久久精品免费电影| 久久免费99精品久久久久久| 国产精品久久久久久户外露出| 亚洲精品在线视频| 99re6这里只有精品| 巨乳诱惑日韩免费av| 女女同性女同一区二区三区91| 国内自拍视频一区二区三区| 午夜精品一区二区三区在线播放 | 亚洲激情电影在线| 久久永久免费| 亚洲国产欧美另类丝袜| 亚洲区第一页| 欧美不卡激情三级在线观看| 欧美大成色www永久网站婷| 亚洲国产电影| 欧美精品一区二区久久婷婷| 亚洲精品网站在线播放gif| 一本色道久久综合亚洲精品小说| 欧美精品综合| 99视频精品全国免费| 亚洲一区二区三区乱码aⅴ| 欧美午夜宅男影院| 亚洲欧美日韩在线观看a三区| 久久av二区| 激情久久一区| 欧美黄色aa电影| 一本色道久久88精品综合| 欧美怡红院视频| 狠狠色丁香久久婷婷综合_中| 久久深夜福利免费观看| 亚洲精品国产精品国自产观看浪潮| 亚洲久久在线| 国产精品久久久爽爽爽麻豆色哟哟 | 国产精品永久入口久久久| 欧美一区二区视频97| 欧美成年人网| 亚洲一区二区三区视频播放| 国产欧美一区在线| 男女激情久久| 亚洲一区二区三区精品在线观看| 久久久久久穴| 99在线热播精品免费99热| 国产欧美精品一区二区色综合 | 久久爱另类一区二区小说| 欧美激情精品久久久久久| 亚洲欧美日韩精品久久亚洲区| 国内精品一区二区| 欧美日韩午夜在线视频| 欧美在线一二三四区| 亚洲人成在线观看| 久久精品在线| 亚洲一二三区在线观看| 激情视频一区二区| 国产精品每日更新在线播放网址| 久久蜜桃香蕉精品一区二区三区| 99视频一区二区| 欧美jizzhd精品欧美巨大免费| 亚洲资源av| 日韩视频第一页| 伊甸园精品99久久久久久| 国产精品久久看| 欧美激情一区二区在线| 久久久久久伊人| 亚洲欧美电影院| 亚洲精品中文字幕女同| 欧美好骚综合网| 久久婷婷国产麻豆91天堂| 亚洲天堂免费观看| 亚洲精品色婷婷福利天堂| 在线播放精品| 国语自产精品视频在线看一大j8| 国产精品久久一区二区三区| 欧美激情网友自拍| 蜜乳av另类精品一区二区| 欧美在线国产精品| 亚洲一区二区三区四区视频| 亚洲免费成人av电影| 亚洲欧洲三级| 亚洲国产精品123| 欧美华人在线视频| 欧美电影打屁股sp| 欧美大片免费久久精品三p| 久久久久久香蕉网| 快播亚洲色图| 麻豆精品视频在线观看| 久久中文字幕一区| 另类尿喷潮videofree| 快播亚洲色图| 欧美大尺度在线| 亚洲国产1区| 亚洲国产精品欧美一二99| 亚洲国产日韩一级| 亚洲精品少妇30p| 日韩一级黄色片| 一区二区三区国产在线| 亚洲天天影视| 午夜精品亚洲一区二区三区嫩草| 性视频1819p久久| 久久久.com| 欧美精品国产精品日韩精品| 欧美日韩爆操| 国产精品自拍网站| 伊大人香蕉综合8在线视| 最新亚洲电影| 一区二区三区久久网| 午夜亚洲激情| 久久婷婷国产综合国色天香| 欧美大片在线影院| 一本色道**综合亚洲精品蜜桃冫| 亚洲一区免费网站| 久久久久9999亚洲精品| 欧美成人国产一区二区| 欧美体内she精视频在线观看| 国产精品一区二区欧美| 亚洲高清电影| 亚洲在线国产日韩欧美| 久久久天天操| 日韩亚洲欧美综合| 久久gogo国模啪啪人体图| 欧美国产第二页| 国产三级欧美三级日产三级99| 在线日韩av片| 午夜在线观看免费一区| 欧美激情bt| 亚洲综合三区| 欧美久久久久久久久| 国产日韩亚洲欧美综合| 亚洲精选大片| 久久久久久久一区二区三区| 亚洲精品久久久久久下一站| 亚洲欧美资源在线| 欧美激情亚洲自拍| 狠狠色2019综合网| 亚洲综合三区| 亚洲国产精品t66y| 久久成人资源| 国产精品一区二区在线观看网站 | 国内精品久久久久国产盗摄免费观看完整版| 亚洲电影免费在线| 欧美中文字幕| 艳妇臀荡乳欲伦亚洲一区| 美女黄毛**国产精品啪啪| 国产精品日韩电影| 亚洲一区二区少妇| 91久久精品国产91久久性色| 久久精品国产精品亚洲| 国产麻豆9l精品三级站|