- •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#
VIEW INFORMATION ABOUT WINDOWS FORMS
Soon after Microsoft introduced Windows 3.0, it also released a new version of Basic called Visual Basic. Both the operating system and the programming
language became instant hits; Visual Basic became so popular because you can create a Windows program interface by dragging and dropping interface elements. C# continues this tradition by letting you create forms as an interface for users to enter information.
A form is an area on your screen that presents information to the user of your program so that the user can receive and enter information. Forms can take on several different guises including your standard windows, multiple document
interface, windows, dialog boxes, and surfaces so you can place different objects such as a graphic.
You can add objects such as buttons, controls, and fields into a form by dragging and dropping those elements from a default set of element templates. The form is an instance of a class, and this approach lets you create forms that can inherit from other forms because a class can inherit from another class. The form can inherit from the Form class template or from another form; inheriting from another form gives your form the extensibility it needs to add new features and functionality.
VIEW INFORMATION ABOUT WINDOWS FORMS
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
⁄ Click Start Programs |
|
|
|
|
|
|
|
|
|
||||
■ The Start page appears. |
|
¤ Click Help. |
|
‹ Click Index. |
|
|
|||||||
|
|
|
|||||||||||
Microsoft Visual Studio .NET |
|
|
|
|
|
|
|
|
|
||||
7.0 Microsoft Visual Studio |
|
|
|
|
|
|
|
|
|
||||
.NET 7.0. |
|
|
|
|
|
|
|
|
|
BUILDING FORMS 10
Microsoft added its form capability that it perfected in Visual Basic into C# as another tool that sets it apart from Visual C++ and Java. (Some Java development tools contain form creation capabilities, however.) Forms are not contained to C# and Visual Basic but are part of the Visual Studio .NET framework so you can import forms from one Visual Studio .NET programming language to another.
Forms act as the user interface for your program so users can interact with your program, the program database, the computer file system, and even the Windows registry. You can write forms in code, but the Windows Forms Designer makes it so easy that you do not have to worry about coding elements in your form.
If you used forms in Visual Basic 6 and want to use the same forms in C#, Visual Studio .NET handles forms differently. The MDE window online help contains a page with a comparison of form changes from Visual Basic 6 to Visual Studio .NET. If you have upgraded to Visual Studio .NET from a version of Visual Studio older than version 6, you can still compare forms in both versions by consulting the comparison help page along with your old Visual Studio documentation.
■ The Index window appears.
› Type forms in the Look for field.
ˇ Click to select Visual C# in the Filtered by drop-down list.
Á Scroll down the topics list until you reach the overview topic.
‡ Click the overview topic.
■ The Introduction to Windows Forms page appears so you can read more information about Windows forms.
197
C#
ADD A WINDOWS FORM IN THE WINDOWS FORM DESIGNER
You add Visual Studio .NET forms — what Microsoft terms Windows forms — in C# by using the Windows Form Designer. In many cases you do not have to
create a Windows form from scratch because C# creates one for you automatically when you open a new project, thus saving you time.
The form appears in the parent window so you can edit its properties and add information to the form. Form properties appear in the Properties window. If you add a new form in the Windows Form Designer, then the new form will appear in its own window with its own name.
When you view the form for the first time, you will notice that the form has dots throughout the form. These dots represent the grid, and you can use these dots as boundaries for objects that you add to the form. The gridlines help ensure that all the objects in your form look pleasing to the eye and that form objects do not interlap.
A box comprised of dashed lines appears around the perimeter of the form. This selection box lets you know that the current form is selected. The white boxes that appear within the selection box are the form handles. You can use these handles for resizing your form until it is the size you want.
ADD A WINDOWS FORM
‹ Click the Windows Application icon in the Templates pane.
› Type a name for the file.
ˇ Click OK.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Á Right-click FormOne in |
|
|
|
|
|
|
|
|
|
|
|
||
‡ Click Add. |
||||||
the Solution Explorer |
|
° Click Add Windows Form. |
||||
window. |
|
|||||
|
|
|
|
|
BUILDING FORMS 10
Your new form already contains all the standard elements of a Windows form including a title bar. From left to right, the title bar contains a multicolored logo for performing window functions. This built-in function makes it easier for you to program your form and gives the user a familiar, standard interface for your program.
You can program in code if you want to, but if you are uncertain about how to proceed, you can create a form and then view the underlying code to see the nuts and bolts of your form. In the Solution Explorer window, you can click the form with your right mouse button. In the pop-up menu that appears, click View Code. A new window appears that contains the code for your form. You can go back to the designer by clicking the form tab with the [Design] label after the form name.
Form2.cs
|
|
|
|
|
|
|
— Save the program as the |
■ The Add New Item |
|
|
|
■ The new form appears in |
|||
|
|
|
|||||
|
|
|
|||||
‚ Click Open. |
|||||||
window appears. |
|
|
|
the parent window. |
filename. |
||
|
· Type the new form name |
|
|
|
|
|
|
|
|
|
|
|
|
||
in the Name field. |
|
|
|
|
|
199