Developing Your First Windows Forms Application


Development of a Windows Forms application involves a couple of more steps beyond what is typically needed to develop a console application. Every Windows Forms application is composed of one or more forms, which derive from the base class Form found in the System.Windows.Forms namespace. After the derived Form class has been created, the application itself can be started by using the Run() method of the Application class passing the reference of the instantiated form object. Apart from these two basic steps, developing a Form involves setting form-specific properties, controls, menus , and event handlers. For instance, the following Hello World style simple Form contains a single control, ExitButton; the button also has an event handler attached to it that exits the application once it has been "clicked."

 
 using System; using System.Windows.Forms; namespace MyCompany {  namespace MyApp  {    class HelloForm : Form {    private Button ExitButton;    public HelloForm()    {       this.Text = "C# Windows Forms Application";       ExitButton = new Button();       ExitButton.Text = "Exit!";       ExitButton.Click += new EventHandler(this.ExitButton_Click);       Controls.Add(ExitButton);    }    protected void ExitButton_Click(Object sender, EventArgs e)    {       Application.Exit();    }    public static void Main()    {       HelloForm hf = new HelloForm();       Application.Run(hf);    }    }  } } 

After you have typed this application using your favorite editor or development tool, you should be able to build this application using the command line (also known as SDK) C# compiler.

 
 csc HelloWorld.cs 

After it is compiled, you should be able to run the generated executable HelloWorld.exe to see an output similar to Figure 7.1. As mentioned earlier, Windows Forms applications are zero install . To test it, copy this application to another Windows-based machine (that has .NET Framework Redistributable installed), and you should be able to run it without any changes required.

Figure 7.1. Your first Windows Forms Application (C#).

Visually Designing Windows Forms Applications Using Visual Studio .NET

As mentioned earlier, Windows Forms allows development of Windows GUI applications using all supported .NET programming languages. A simple Windows Forms application in Visual Basic looks and feels similar to a traditional Visual Basic application with changes primarily in the language syntax.

 
 Imports System Imports System.Windows.Forms Namespace MyCompany.MyApplication  Public Class HelloForm    Inherits Form     Private ExitButton As Button     Public Sub New()    MyBase.New()    ExitButton = New Button()    ExitButton.Text = "Exit!"    AddHandler ExitButton.Click, AddressOf ExitButton_Click         Controls.Add(Me.ExitButton)         Me.Text = "Visual Basic .NET Windows Forms Application"     End Sub     Private Sub ExitButton_Click(ByVal sender As Object, ByVal e As EventArgs)         Application.Exit()     End Sub  End Class  Module RunForm     Sub Main()    Application.Run(New HelloForm())     End Sub  End Module End Namespace 

Using the .NET Framework SDK, Visual Basic programs can also be compiled using the command-line compiler vbc.exe . The Visual Basic compiler requires an explicit reference to the System.Windows.Forms DLL.

 
 vbc HelloWorld.vb /r:System.dll,System.Windows.Forms.dll 

Using Visual J#

Visual J++ developers enjoyed the similar benefits enjoyed by Visual Basic developers of rapid graphical application development. However, the challenge for developers was to learn the Java-based AWT (Abstract Windows Toolkit) model. Visual J# allows developers with Java programming language expertise to use the Windows Forms .NET class library to build Windows applications. As you can see, the following program looks a lot like the C# program (because of the language similarities between Java and C#), the key difference being the syntax used by J# for setting properties of form controls using the set_<Property> methods rather than the traditional Object.Property assignment.

 
 package MyCompany.MyApplication; import System.*; import System.Windows.Forms.*; public class HelloForm extends Form {    private Button ExitButton;    public HelloForm()    {       this.set_Text("Visual J# Windows Forms Application");       ExitButton = new Button();       ExitButton.set_Text("Exit!");       ExitButton.add_Click(new EventHandler(ExitButton_Click));       this.get_Controls().Add(ExitButton);    }    private void ExitButton_Click(Object sender, EventArgs eArgs)    {       Application.Exit();    }    public static void main(String[] args)    {       HelloForm hf = new HelloForm();       Application.Run(hf);    } } 

Similar to a Visual Basic .NET application, Visual J# applications can be compiled using the command-line compiler included with the SDK. Note that like any other Visual J# application, the preceding application uses the .NET Framework class library and won't run with a Java Virtual Machine (JVM).

 
 vjc HelloWorld.jsl /r:System.Windows.Forms.dll 

Using Managed C++

C++ programmers have probably had the most challenges for GUI application development. It is not that there weren't GUI frameworks present. There were MFC and a slew of third-party GUI frameworks from Borland (OWL), Rouge Wave, and so on. Apart from the existence of multiple frameworks (which implied no standardization), classical issues existed, such as DLL mismatch after the applications were deployed. With Managed Extensions to the C++ programming language, C++ programmers can enjoy the benefits of using the rich Windows Forms class library while still having the richness and efficiency of the C++ programming language.

 
 #using <mscorlib.dll> #using <System.dll> #using <System.Windows.Forms.dll> namespace MyApplication {    using namespace System;    using namespace System::Windows::Forms;    public __gc class HelloForm : public Form    {      public:       HelloForm(void)       {          this->ExitButton = new Button();          this->ExitButton->Text = "Exit!";          this->ExitButton->Click +=                               new EventHandler(this, ExitButton_Click);          this->Controls->Add(this->ExitButton);          this->Text = S"Managed C++ Windows Forms Application";       }    private:       Button* ExitButton;    private:       Void ExitButton_Click(Object* sender, EventArgs* e)       {          Application::Exit();       }    }; }; int main() {    System::Windows::Forms::Application::Run(                new MyApplication::HelloForm());       return 0; } 

As mentioned earlier in the book, Managed C++ programmers can compile the Windows Forms application using the C++ compiler with the /CLR switch.

 
 cl /CLR HelloWorld.cpp 


Microsoft.Net Kick Start
Microsoft .NET Kick Start
ISBN: 0672325748
EAN: 2147483647
Year: 2003
Pages: 195
Authors: Hitesh Seth

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net