كيفية القيام بما يلي: مؤشر الأحداث في JScript

An حدث هو an إجراء, such كـ clicking a ماوس زر, pressing a المفتاح, changing بيانات, أو فتح a مستند أو نموذج, that a مستخدم typically performs. Furthermore, برنامج تعليمات برمجية can also perform an إجراء. An معالج الأحداث هو a أسلوب that هو حدود إلى an حدث. When the حدث هو مرفوع, the تعليمات برمجية within the معالج الأحداث هو executed. JScript .NET حدث handlers can be متصل إلى أحداث في أي نوع of .NET تطبيق (ASP.NET, Windows Forms, console, و so تشغيل). However, جديد أحداث cannot be declared في Jscript. فقط the أحداث that already exist can be utilized بواسطة Jscript تعليمات برمجية.

إلى إنشاء an معالج الأحداث for a زر عنصر تحكم's انقر حدث

  • اضف التعليمات البرمجية التالية:

    // Events.js
    import System;
    import System.Windows.Forms;
    
    class EventTestForm extends Form
    {
      var btn : Button;
    
      function EventTestForm()
      {
        btn = new Button;
        btn.Text = "Fire Event";
        Controls.Add(btn);
        // Connect the function to the event.
        btn.add_Click(ButtonEventHandler1);
        btn.add_Click(ButtonEventHandler2);
      }
    
      // Add an event handler to respond to the Click event raised
      // by the Button control.
      function ButtonEventHandler1(sender, e : EventArgs)
      {
        MessageBox.Show("Event is Fired!");
      }
    
      function ButtonEventHandler2(sender, e : EventArgs)
      {
        MessageBox.Show("Another Event is Fired!");
      }
    }
    
    Application.Run(new EventTestForm);
    

    ملاحظة

    Each معالج الأحداث provides الثاني معلمات. The أول معلمة, sender, provides a مرجع إلى the كائن that مرفوع the حدث. The ثانية معلمة, e في the مثال above, passes an كائن specific إلى the حدث that هو being handled. بواسطة referencing the كائن's خصائص (و, sometimes, its وظائف), you can obtain معلومات such كـ the الموقع of the ماوس for ماوس أحداث أو بيانات being transferred في يسحب وإفلات أحداث.

ترجمة تعليمات برمجية

  1. استخدم the الأمر-خط compiler, jsc.exe, provided مع ‏‫Visual Studio.

  2. نوع the following الأمر-خط directive إلى إنشاء a Windows تنفيذي (EXE) برنامج named أحداث.exe:

    jsc /الهدف:winexe أحداث.js

    ملاحظة

    Raising a مفرد حدث can النتيجة في calling multiple حدث handlers بواسطة إضافة ارتباطات كـ many دالات إلى the حدث كـ needed:

    btn.add_Click(ButtonEventHandler1); 
    btn.add_Click(ButtonEventHandler2);
    . . .
    

راجع أيضًا:

المهام

كيفية القيام بما يلي: يحول برمجياً تعليمات برمجية JScript من سطر الأوامر

موارد أخرى

كتابة وترجمة وتصحيح JScript تعليمات برمجية