- •maranGraphics
- •CREDITS
- •ACKNOWLEDGMENTS
- •ABOUT THE AUTHORS
- •AUTHORS’ ACKNOWLEDGMENTS
- •TABLE OF CONTENTS
- •HOW TO USE THIS BOOK
- •INTRODUCTION TO C#
- •START VISUAL STUDIO .NET
- •OPEN A NEW C# PROJECT
- •OPEN A C# WEB PROJECT
- •SET JSCRIPT .NET AS THE DEFAULT SCRIPT LANGUAGE
- •EXPLORE THE CLASS VIEW WINDOW
- •VIEW THE CONTENTS WINDOW
- •GET HELP USING THE INDEX WINDOW
- •SEARCH FOR HELP
- •ADD COMPONENTS FROM THE TOOLBOX
- •ADD A TASK TO THE TASK LIST
- •CHANGE FORM PROPERTIES IN THE PROPERTIES WINDOW
- •ADD A CUSTOM TOOLBAR
- •DELETE A TOOLBAR
- •CHANGE THE VISUAL STUDIO ENVIRONMENT
- •MANAGE OPEN WINDOWS
- •OPEN A PROJECT
- •VIEW THE MAIN METHOD
- •COMBINE PROGRAM TYPES
- •ADD REFERENCE TYPES
- •ADD OPERATORS
- •INSERT ATTRIBUTES
- •ENTER CLASSES
- •ADD COMMENTS TO CODE
- •WRITE YOUR FIRST PROGRAM
- •ENTER XML DOCUMENTATION
- •ACCESS DOCUMENTATION
- •LOG A BUG REPORT
- •VIEW INFORMATION ABOUT C# BUILDING BLOCKS
- •PROGRAM CLASSES
- •ADD A CLASS
- •EMPLOY CLASS INHERITANCE
- •PROGRAM INSTANCE CONSTRUCTORS
- •INSERT DESTRUCTORS
- •PROGRAM STRUCTS
- •DISPLAY HEAP AND STACK INFORMATION
- •FIND TYPE INFORMATION
- •PROGRAM CONSTANT EXPRESSIONS
- •SPECIFY VALUE TYPES
- •PROGRAM NUMERIC TYPES
- •PROGRAM THE BOOLEAN TYPE
- •DECLARE REFERENCE TYPES
- •ENTER REFERENCE TYPE DECLARATIONS
- •CONVERT VALUE TYPES TO REFERENCE TYPES
- •PROGRAM POINTER TYPES
- •INSERT THE VOID TYPE
- •ADD INTERFACE PROPERTIES
- •ADD AN INTERFACE INDEX
- •VIEW INFORMATION ABOUT METHODS
- •ADD A METHOD
- •ADD STATIC METHODS
- •INCLUDE NON-STATIC METHODS
- •ENTER DELEGATES
- •PROGRAM EVENTS
- •ADD AN EVENT-HANDLING METHOD
- •VIEW INFORMATION ABOUT ARRAYS
- •ENTER SINGLE-DIMENSIONAL ARRAYS
- •ADD MULTIDIMENSIONAL ARRAYS
- •PROGRAM ARRAY-OF-ARRAYS
- •ITERATE THROUGH ARRAY ELEMENTS
- •SORT ARRAYS
- •SEARCH ARRAYS
- •IMPLEMENT A COLLECTIONS CLASS
- •PROGRAM STRUCTS
- •ADD AN INDEXER
- •INCLUDE ENUMERATIONS
- •CREATE STRING LITERALS AND VARIABLES
- •ASSIGN VALUES TO STRINGS
- •CONCATENATE STRINGS
- •COMPARE STRINGS
- •SEARCH FOR SUBSTRINGS
- •REPLACE CHARACTERS
- •EXTRACT SUBSTRINGS
- •CHANGE THE CHARACTER CASE
- •TRIM SPACES
- •REMOVE CHARACTERS
- •SPLIT A STRING
- •JOIN STRINGS
- •PAD STRINGS
- •VIEW INFORMATION ABOUT PROPERTIES
- •COMPARE PROPERTIES AND INDEXERS
- •PROGRAM PROPERTY ACCESSORS
- •DECLARE ABSTRACT PROPERTIES
- •INCLUDE PROPERTIES ON INTERFACES
- •VIEW INFORMATION ABOUT WINDOWS FORMS
- •ADD A WINDOWS FORM IN THE WINDOWS FORM DESIGNER
- •SET THE FORM TYPE
- •CHOOSE THE STARTUP WINDOWS FORM
- •CREATE A MODAL FORM
- •LAYOUT A FORM
- •SET A FORM LOCATION
- •CHANGE FORM PROPERTIES
- •CREATE A TRANSPARENT FORM
- •AN INTRODUCTION TO WEB FORMS AND CONTROLS
- •CREATE AN ASP.NET WEB SITE
- •CREATE A WEB FORM
- •ADD SERVER CONTROLS TO A WEB FORM
- •READ AND CHANGE PROPERTIES FROM OBJECTS ON A WEB FORM
- •USING SERVER-SIDE COMPONENTS ON WEB FORMS
- •INTRODUCING DATA ACCESS WITH ADO.NET
- •DISPLAY DATA WITH THE DATAGRID CONTROL
- •CONFIGURE THE DATAGRID CONTROL
- •INSERT DATA INTO A SQL DATABASE
- •UPDATE DATA FROM A SQL DATABASE
- •DELETE DATA FROM A SQL DATABASE
- •EXECUTE A STORED PROCEDURE IN A SQL DATABASE
- •READ XML FROM A FILE
- •SAVE XML TO A FILE
- •QUERY XML WITH XPATH
- •APPLY XSL TO XML
- •INTRODUCTION TO DISTRIBUTED APPLICATIONS
- •CREATE AN APPLICATION WITH PRIVATE ASSEMBLIES
- •CREATE AN APPLICATION WITH SHARED ASSEMBLIES
- •VERSION A SHARED ASSEMBLY
- •CONFIGURE A CLIENT FOR A VERSIONED ASSEMBLY
- •CREATE A WEB SERVICE
- •USING A WEB SERVICE
- •INTRODUCTION TO EXCEPTION HANDLING
- •THROWING AN EXCEPTION
- •HANDLING EXCEPTIONS WITH THE CATCH BLOCK
- •USING THE FINALLY BLOCK
- •WRITE ERRORS TO THE APPLICATION LOG
- •BASIC EXAMPLES
- •WHAT’S ON THE CD-ROM
- •USING THE E-VERSION OF THIS BOOK
- •INDEX
- •Symbols & Numbers
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