August 27, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Creating a App using JScript .NET and Windows Forms

  • February 22, 2002
  • By Essam Ahmed
  • Send Email »
  • More Articles »

In the second article of this series, I briefly described how the JScript .NET and the .NET Framework allow JScript developers to create Windows-style applications using Windows Forms. Windows Forms is a part of the .NET Framework that enables developers to take advantage of Windows' graphical interface capabilities.

A Windows Form represents a traditional graphical interface through which users interact with your application. Windows Forms offers a cohesive programming model that makes it easy to build forms that have rich capabilities and a high degree of interactivity. This article walks you through a sample application written in JScript .NET that implements a Windows Forms application that features and uses these types of controls:

  • Main menu
  • Context menu
  • Tab strip control
  • Tree View control
  • Checked List Box control
  • Drop Down menu
  • Progress bar
  • Picture box control
  • Timer
  • Month calendar control

Figure 1 shows the completed application featured in this article.

Figure 1 - Sample Application

Understanding the Windows Forms Programming Model

As with most other aspects of the programming with the .NET Framework, a Windows Form is an object that derives from a base class, which implements most of the form's behavior. Your job is to add controls and customize the behavior of the form for your application. The Windows Forms package primarily resides in the System.Windows.Forms namespace; however, you'll also require access to members of the System.Drawing namespace, which supports accurate the Windows Forms package by allowing you to accurately specify control placement and size.

Your application's form acts as a container for the controls, like buttons and text boxes, that reside in it. In fact, you must programmatically add your controls to the form's Controls collection for them to be visible at runtime. Whenever a user clicks on a control, or interacts with it in some way, the control raises events, which give your application the opportunity to respond to the user's actions. When you create a control, you can specify what events a control responds to and can provide your implementation for the event. For example, the Stop button at the bottom of the form in Figure 1 stops the progress bar from updating - the code to implement that behavior resides in the buttons Click event handler.

To create a basic form, you need to import some namespaces and define a class that represents the form and contains its controls, as shown in the following listing:

import System;
import System.Drawing;
import System.Windows.Forms;

public class sampleForm extends System.Windows.Forms.Form {

  private var btnProgressControl : System.Windows.Forms.Button;

  public function sampleForm() {
    this.btnProgressControl = new System.Windows.Forms.Button();
    this.btnProgressControl.Text = " Stop ";
    this.btnProgressControl.add_Click(btnProgressControl_OnClick);
  }

  function btnProgressControl_OnClick( source:Object, e: EventArgs )
  {
    //...
  }
}

Application.Run(new sampleForm);

The sampleForm class extends the .NET Class Library's Form class, which provides basic forms support. The class's constructor initializes a Button object by setting its Text property (the button's caption) and defines the button's Click event handler. The event handler is a function that takes two arguments, as shown in the listing. You can name the event handler function anything you want, but the function's signature (the function's argument types) must appear as shown (you can use any names for the arguments but the types have to be Object and EventArgs). Associate the event handler with the button's click event by calling the Button object's add_Click method, passing it the name of the event handler function. The application starts executing through the Application object's static Run method, which takes an instance of Windows Form object, makes it visible, and processes system messages.





Page 1 of 4



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel