Visual C# 2010 Recipes - A Problem-Solution Approach

von: Allen Jones, Matthew MacDonald, Rakesh Rajan, Adam Freeman

Apress, 2010

ISBN: 9781430225263 , 1016 Seiten

Format: PDF, OL

Kopierschutz: Wasserzeichen

Windows PC,Mac OSX geeignet für alle DRM-fähigen eReader Apple iPad, Android Tablet PC's Online-Lesen für: Windows PC,Mac OSX,Linux

Preis: 66,99 EUR

Mehr zum Inhalt

Visual C# 2010 Recipes - A Problem-Solution Approach


 

"1-2. Create a Windows-Based Application from the Command Line (p. 5-6)

Problem
You need to use the C# command-line compiler to build an application that provides a Windows Forms– based GUI.

Solution

Create a class that extends the System.Windows.Forms.Form class. (This will be your application’s main form.) In one of your classes, ensure you implement a static method named Main. In the Main method, create an instance of your main form class and pass it to the static method Run of the System.Windows. Forms.Application class. Build your application using the command-line C# compiler, and specify the /target:winexe compiler switch.

Note If you own Visual Studio, you will most often use the Windows Application project template to create new Windows Forms–based applications. Building large GUI-based applications is a time-consuming undertaking that involves the correct instantiation, configuration, and wiring up of many forms and controls. Visual Studio automates much of the work associated with building graphical applications. Trying to build a large graphical application without the aid of tools such as Visual Studio will take you much longer, be extremely tedious, and result in a greater chance of bugs in your code.

However, it is also useful to know the essentials required to create a Windows-based application using the command line in case you are ever working on a machine without Visual Studio and want to create a quick utility to automate some task or get input from a user. In order to build a WPF application from the command line, you must use the MSBuild tool—see the MSBuild reference in the .NET Framework documentation.

How It Works


Building an application that provides a simple Windows GUI is a world away from developing a fullfledged Windows-based application. However, you must perform certain tasks regardless of whether you are writing the Windows equivalent of Hello World or the next version of Microsoft Word, including the following:

• For each form you need in your application, create a class that extends the System.Windows.Forms.Form class.

• In each of your form classes, declare members that represent the controls that will be on that form, such as buttons, labels, lists, and text boxes. These members should be declared private or at least protected so that other program elements cannot access them directly. If you need to expose the methods or properties of these controls, implement the necessary members in your form class, providing indirect and controlled access to the contained controls.

• Declare methods in your form class that will handle events raised by the controls contained by the form, such as button clicks or key presses when a text box is the active control. These methods should be private or protected and follow the standard .NET event pattern (described in recipe 13-11). It’s in these methods (or methods called by these methods) where you will define the bulk of your application’s functionality.

• Declare a constructor for your form class that instantiates each of the form’s controls and configures their initial state (size, color, position, content, and so on). The constructor should also wire up the appropriate event handler methods of your class to the events of each control.
• Declare a static method named Main—usually as a member of your application’s main form class. This method is the entry point for your application, and it can have the same signatures as those mentioned in recipe 1-1.

In the Main method, call Application.EnableVisualStyles to allow Windows theme support, create an instance of your application’s main form, and pass it as an argument to the static Application.Run method. The Run method makes your main form visible and starts a standard Windows message loop on the current thread, which passes the user input (key presses, mouse clicks, and so on) to your application form as events."