Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
C# ПІДРУЧНИКИ / c# / Hungry Minds - Visual C# Blueprint.pdf
Скачиваний:
101
Добавлен:
12.02.2016
Размер:
9.71 Mб
Скачать

C#

SET THE FORM TYPE

When you open a new Windows application, C# opens a new Windows form. This form inherits information from the Windows form template

that automatically appears when you open a new Windows application so you do not have to create a form from scratch. You can also set up an inheriting form that inherits from another form in your C# project.

A Windows form looks very much like the standard window that you see in Windows. C# builds Windows forms around the Windows framework so you can access various Windows features including the files on the user’s computer and the Windows registry. Windows forms also let you create graphics in your form with code using the Visual Studio .NET GDI classes.

You use Windows forms for developing Windows applications where the client computer and the user enters information into your program. Programs that use Windows forms rely on the computer the program runs on as well as a network for processing power and accessing data.

Inherited forms let you obtain features from another form so you do not have to add form elements repeatedly. Inheriting also promotes consistency between forms. Before you can inherit a form, the inherited form must already have been compiled into an executable file and a reference to the namespace must have been added into the class that inherits the form.

SET THE FORM TYPE

Click the Windows Application icon in the Templates pane.

Type a name for the file.

ˇ Click OK.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Á Right-click FormTwo in

 

 

 

 

 

 

 

 

 

 

Click Add.

the Solution Explorer

 

° Click Add Inherited Form.

window.

 

 

 

 

 

 

BUILDING FORMS 10

You can inherit a form in code rather than add a form from the Solution Explorer window.

TYPE THIS:

using System;

using System.Drawing; using System.Collections;

using System.ComponentModel; using System.Windows.Forms; using System.Data;

namespace MyFormProgram

{

public class Form1 : System.Windows.Forms.Form // Enter rest of your form here.

RESULT:

Entering a form in code means that you must add six System names, the name of your program in the namespace argument, and finally the class name and type. This is a lot of typing, and it is usually a good idea to create a Windows application and let the MDE window do the work for you.

Form2.cs

 

 

 

 

 

 

The Add New Item

 

 

 

 

 

 

 

 

 

Click Open.

window appears.

The Inheritance Picker

 

· Type the new form name

 

window appears.

 

in the Name field.

 

 

 

Form1

FormTwo

C:\Documents and Sett

Click to select the EXE or DLL file you want the form to inherit from.

Note: If you have not built your form, you will receive a message stating that there are no files to inherit from.

± Click OK.

The inheriting form appears in the parent window.

201

C#

CHOOSE THE STARTUP WINDOWS FORM

AWindows form does not automatically start when you start your program. You must tell your program what form you want it to display when your program first

launches. You can do this by setting the properties of your form in the Solution Explorer window.

The Properties pages contain information about the common and configuration properties in your program, but the General tab contains the information you need to know. Specifically, the Startup Object information lets you select the class in your project that you want to start first when the user starts your program.

Because a Windows form is a class, you determine what class you want to take precedence over any other class in your form. Each class has a main method that starts the class, but with so many different classes, C# will not start any of these form classes ahead of any others until you set the Startup Object property in the Property Pages window.

After you choose the startup Windows form for your program and compile it, the form will appear in the location that you specify in the MDE Properties window. If you do not specify a location, then the form will appear in the upper-left corner of the screen.

CHOOSE THE STARTUP WINDOWS FORM

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Á Right-click FormStart in

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Windows

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Click Properties.

 

 

 

 

 

 

 

 

 

in the

the Solution Explorer

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

window.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

for the file.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

BUILDING FORMS 10

If you prefer to compile your C# classes from the command line instead of from the MDE window, you can specify the form class that you want to start in the command-line arguments.

C# uses the csc command for compiling your C# classes from the command line, and you can compile multiple classes on the same line. However, if you compile multiple form classes, then you must specify the class that will appear first by using the /main argument. The /main argument specifies the class with the Main method that appears when the user first starts the program. The /main argument includes the argument itself, a colon, and the class name.

TYPE THIS:

csc class1.cs class2.cs /main:Class2

RESULT:

The command line above tells C# to compile the class1.cs and class2.cs filenames and open Class2 (the class that contains the form) when the program runs.

The FormStart Property Pages window appears.

° Click beside the (Not set) text field.

· Click to select the startup object from the drop-down list.

The form you selected as the startup appears beside the Startup Object field.

Click OK.

The FormOne Property Pages window closes and the MDE window reappears with the form in the parent window.

203

Соседние файлы в папке c#