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

C++ Programmer's Cookbook

{C++ 基礎(chǔ)} {C++ 高級} {C#界面,C++核心算法} {設(shè)計模式} {C#基礎(chǔ)}

Event Handling in C#(轉(zhuǎn))

 

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 夢在天涯 閱讀(985) 評論(0)  編輯 收藏 引用 所屬分類: C#/.NET

公告

EMail:itech001#126.com

導(dǎo)航

統(tǒng)計

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

常用鏈接

隨筆分類

隨筆檔案

收藏夾

Blogs

c#(csharp)

C++(cpp)

Enlish

Forums(bbs)

My self

Often go

Useful Webs

Xml/Uml/html

搜索

  •  

積分與排名

  • 積分 - 1818830
  • 排名 - 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久久国产香蕉| 国产精品激情av在线播放| 亚洲欧美在线播放| 亚洲免费中文字幕| 黄色精品一二区| 亚洲国产成人在线| 国产精品99免费看 | 欧美高清一区二区| 亚洲乱码国产乱码精品精可以看 | 亚洲日本激情| 亚洲视频一区二区在线观看 | 久久这里有精品视频| 亚洲黄色免费电影| 日韩午夜免费| 国产主播精品| 91久久夜色精品国产九色| 国产精品草草| 免费成人av资源网| 欧美日韩中字| 欧美成人乱码一区二区三区| 欧美人与性动交α欧美精品济南到| 亚洲欧美视频一区二区三区| 久久久在线视频| 亚洲尤物视频网| 免费的成人av| 久久精品成人欧美大片古装| 欧美黄色免费| 久久天天躁狠狠躁夜夜爽蜜月| 欧美日韩国产成人高清视频| 久久亚洲精品视频| 欧美性大战久久久久久久蜜臀| 猛男gaygay欧美视频| 国产精品久久久久久妇女6080 | 亚洲精品少妇30p| 亚洲男人天堂2024| 99国内精品| 久久野战av| 久久国产精品99久久久久久老狼 | 亚洲欧美激情四射在线日 | 国产日韩在线看片| 亚洲破处大片| 亚洲国产精品999| 欧美在线二区| 亚洲女与黑人做爰| 欧美三级视频在线播放| 欧美电影免费观看高清完整版| 国产亚洲一本大道中文在线| 亚洲午夜电影| 一本久道久久综合婷婷鲸鱼| 欧美14一18处毛片| 免费在线亚洲| 一区二区在线视频| 久久精品一区二区国产| 亚洲欧洲av一区二区| 国产精品video| 一区二区av在线| 亚洲深夜福利网站| 欧美日韩国产亚洲一区| 日韩午夜剧场| 亚洲欧美日本视频在线观看| 欧美视频日韩视频| 在线视频日韩精品| 午夜亚洲伦理| 国产精品午夜久久| 性欧美暴力猛交另类hd| 久久久久天天天天| 在线免费日韩片| 欧美刺激午夜性久久久久久久| 欧美国产视频在线观看| 欧美激情女人20p| 亚洲第一在线综合网站| 亚洲毛片在线免费观看| 欧美国产免费| 99视频精品全国免费| 亚洲一线二线三线久久久| 国产精品日韩在线观看| 欧美在线免费视频| 欧美激情亚洲自拍| 一本色道久久88综合亚洲精品ⅰ| 欧美大尺度在线| 一区二区高清在线观看| 性欧美精品高清| 在线成人性视频| 欧美精品少妇一区二区三区| 99国产精品久久久| 久久久国产一区二区| 在线观看日韩欧美| 欧美天堂亚洲电影院在线播放| 亚洲综合二区| 欧美国产在线电影| 亚洲影视中文字幕| 有码中文亚洲精品| 欧美日韩在线免费观看| 午夜亚洲性色福利视频| 亚洲动漫精品| 久久精品国产欧美亚洲人人爽| 亚洲电影成人| 国产欧美精品一区| 欧美成人激情在线| 香蕉尹人综合在线观看| 亚洲国产天堂久久国产91| 欧美与欧洲交xxxx免费观看| 亚洲国产精品va| 国产日韩欧美成人| 欧美片第1页综合| 久久久91精品国产一区二区精品| 亚洲精品久久7777| 久久人人九九| 亚洲欧美视频一区| 亚洲免费观看视频| 在线观看亚洲一区| 国产欧美精品xxxx另类| 欧美激情精品久久久六区热门 | 欧美激情一区二区久久久| 午夜精品婷婷| 一本色道久久| 亚洲国产婷婷| 欧美成人福利视频| 久久激情网站| 午夜宅男久久久| 一区二区不卡在线视频 午夜欧美不卡'| 国产日本亚洲高清| 国产精品99一区二区| 欧美精品综合| 欧美高清视频一二三区| 久久久天天操| 久久久久久一区二区| 欧美一二三区精品| 午夜久久久久久| aa日韩免费精品视频一| 亚洲精品国产系列| 亚洲黄色高清| 亚洲日本免费| 亚洲精品中文字幕在线| 亚洲激情在线视频| 亚洲第一精品夜夜躁人人爽| 久久视频在线看| 乱码第一页成人| 老司机成人网| 蘑菇福利视频一区播放| 欧美成人乱码一区二区三区| 欧美不卡视频一区发布| 麻豆精品在线视频| 欧美大片免费观看| 欧美成人在线影院| 亚洲国产成人精品视频| 亚洲二区免费| 日韩一区二区免费看| 99精品欧美一区二区三区综合在线| 亚洲理论在线观看| 一区二区毛片| 午夜一区在线| 久久色中文字幕| 欧美激情一二区| 国产精品v亚洲精品v日韩精品| 国产精品日本| 国产在线乱码一区二区三区| 在线观看欧美日韩| 一本久道久久综合中文字幕 | 在线成人av网站| 亚洲人成在线观看| 亚洲婷婷综合色高清在线| 欧美制服丝袜| 欧美激情视频一区二区三区不卡| 亚洲黄色av| 午夜精品999| 毛片精品免费在线观看| 欧美日韩亚洲另类| 国产主播精品在线| 99精品国产热久久91蜜凸| 亚洲一区成人| 久久夜色精品国产欧美乱极品| 亚洲国内在线| 欧美一区二区三区视频| 欧美极品aⅴ影院| 国产精品永久在线| 亚洲精品久久在线| 久久国产精品99国产精| 欧美激情视频给我| 午夜精品亚洲| 欧美日韩精品免费观看| 国产在线一区二区三区四区| 一本一本久久a久久精品牛牛影视| 久久精品免费电影| 一本大道久久精品懂色aⅴ| 午夜影院日韩| 欧美性理论片在线观看片免费| 一区二区视频免费完整版观看| 亚洲一区二区三区中文字幕在线| 牛牛影视久久网| 欧美一区2区三区4区公司二百| 欧美伦理在线观看|