- •brief contents
- •about this book
- •The Windows Forms namespace
- •Part 1: Hello Windows Forms
- •Part 2: Basic Windows Forms
- •Part 3: Advanced Windows Forms
- •Who should read this book?
- •Conventions
- •Action
- •Result
- •Source code downloads
- •Author online
- •acknowledgments
- •about .NET
- •Casting the .NET
- •Windows Forms overview
- •about the cover illustration
- •Hello Windows Forms
- •1.1 Programming in C#
- •1.1.1 Namespaces and classes
- •1.1.2 Constructors and methods
- •1.1.3 C# types
- •1.1.4 The entry point
- •1.1.5 The Application class
- •1.1.6 Program execution
- •1.2 Adding controls
- •1.2.1 Shortcuts and fully qualified names
- •1.2.2 Fields and properties
- •1.2.3 The Controls property
- •1.3 Loading files
- •1.3.1 Events
- •1.3.2 The OpenFileDialog class
- •1.3.3 Bitmap images
- •1.4 Resizing forms
- •1.4.1 Desktop layout properties
- •1.4.2 The Anchor property
- •1.4.3 The Dock property
- •1.5 Recap
- •2.1 Programming with Visual Studio .NET
- •2.1.1 Creating a project
- •Action
- •Result
- •2.1.2 Executing a program
- •Action
- •Result
- •2.1.3 Viewing the source code
- •View the code generated by Visual Studio .NET
- •Action
- •Result
- •2.2 Adding controls
- •2.2.1 The AssemblyInfo file
- •Action
- •Results
- •2.2.2 Renaming a form
- •Action
- •Result
- •2.2.3 The Toolbox window
- •Action
- •Result
- •2.3 Loading files
- •2.3.1 Event handlers in Visual Studio .NET
- •Action
- •Result
- •2.3.2 Exception handling
- •Action
- •Result
- •Action
- •Results and Comments
- •2.4 Resizing forms
- •2.4.1 Assign the Anchor property
- •Action
- •Result
- •2.4.2 Assign the MinimumSize property
- •Action
- •Result
- •2.5 Recap
- •Basic Windows Forms
- •Menus
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •3.3 Click events
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •3.5 Context menus
- •Action
- •Result
- •Action
- •Result
- •3.6 Recap
- •Status bars
- •4.1 The Control class
- •4.2 The StatusBar class
- •Action
- •Result
- •Action
- •Result
- •4.3.1 Adding panels to a status bar
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •4.5 Recap
- •Reusable libraries
- •5.1 C# classes and interfaces
- •5.2 Class libraries
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •5.3 Interfaces revisited
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •5.4 Robustness issues
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Common file dialogs
- •Action
- •Results
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •6.3 Paint events
- •Action
- •Result
- •Action
- •Result
- •6.4 Context menus revisited
- •Action
- •Result
- •Action
- •Result
- •6.5 Files and paths
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •6.6 Save file dialogs
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •6.7 Open file dialogs
- •Action
- •Result
- •Action
- •Result
- •6.8 Recap
- •Drawing and scrolling
- •7.1 Form class hierarchy
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •7.4 Panels
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Dialog boxes
- •8.1 Message boxes
- •Action
- •Result
- •Action
- •Result
- •8.1.4 Creating A YesNoCancel dialog
- •Action
- •Result
- •Action
- •Result
- •8.2 The Form.Close method
- •8.2.1 The relationship between Close and Dispose
- •Action
- •Result
- •8.3 Modal dialog boxes
- •Action
- •Result
- •Action
- •Result
- •8.3.2 Preserving caption values
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Basic controls
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •9.1.2 Creating a derived form
- •Action
- •Result
- •9.2 Labels and text boxes
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •9.3.6 Adding AlbumEditDlg to our main form
- •Action
- •Result
- •Action
- •Result
- •9.4 Recap
- •List controls
- •10.1 List boxes
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •10.2 Multiselection list boxes
- •10.2.1 Enabling multiple selection
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •10.3 Combo boxes
- •Action
- •Result
- •Action
- •Result
- •10.4 Combo box edits
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •10.5 Owner-drawn lists
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •More controls
- •Action
- •Result
- •Action
- •Result
- •11.2 Tab pages
- •Action
- •Result
- •Action
- •Result
- •11.3.1 Dates and times
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •11.5 Recap
- •A .NET assortment
- •12.1 Keyboard events
- •Action
- •Result
- •Action
- •Result
- •12.2 Mouse events
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •12.3 Image buttons
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •12.4 Icons
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •12.5 Recap
- •Toolbars and tips
- •13.1 Toolbars
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •13.4.2 Creating tool tips
- •Action
- •Result
- •Action
- •Result
- •Advanced Windows Forms
- •List views
- •14.2 The ListView class
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •14.2.3 Populating a ListView
- •Action
- •Result
- •Action
- •14.3 ListView columns
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •14.6 Recap
- •Tree views
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •15.3 Dynamic tree nodes
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •15.4 Node selection
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •15.5 Fun with tree views
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Multiple document interfaces
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •16.3 Merged menus
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •16.4 MDI children
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •16.5 MDI child window management
- •Action
- •Result
- •Action
- •Result
- •16.6 Recap
- •Data binding
- •17.1 Data grids
- •Action
- •Result
- •Action
- •Result
- •17.2 Data grid customization
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Odds and ends .NET
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •18.2 Timers
- •Action
- •Result
- •Action
- •Result
- •18.3 Drag and drop
- •Action
- •Result
- •Action
- •Result
- •18.4 ActiveX controls
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •18.5 Recap
- •C# primer
- •A.1 C# programs
- •A.1.1 Assemblies
- •A.1.2 Namespaces
- •A.2 Types
- •A.2.1 Classes
- •A.2.2 Structures
- •A.2.3 Interfaces
- •A.2.4 Enumerations
- •A.2.5 Delegates
- •A.3 Language elements
- •A.3.1 Built-in types
- •A.3.2 Operators
- •A.3.3 Keywords
- •A.4 Special features
- •A.4.1 Exceptions
- •A.4.2 Arrays
- •A.4.3 Main
- •A.4.4 Boxing
- •A.4.5 Documentation
- •.NET namespaces
- •B.1 System.Collections
- •B.2 System.ComponentModel
- •B.3 System.Data
- •B.4 System.Drawing
- •B.5 System.Globalization
- •B.6 System.IO
- •B.7 System.Net
- •B.8 System.Reflection
- •B.9 System.Resources
- •B.10 System.Security
- •B.11 System.Threading
- •B.12 System.Web
- •B.13 System.Windows.Forms
- •B.14 System.XML
- •Visual index
- •C.1 Objects
- •C.2 Marshal by reference objects
- •C.3 Components
- •C.4 Common dialogs
- •C.7 Event data
- •C.8 Enumerations
- •For more information
- •bibliography
- •Symbols
- •Index
//This array contains 4 int values, which default to 0
//Here, the valid indexes are b[0], b[1], b[2], b[3] int[] b = new int[4];
//An array can be initialized directly or with the new keyword
//evens.Length will return 6
//foreach (int p in primes) iterates through the elements in primes int[] evens = { 2, 4, 6, 8, 10, 12 };
int[] primes = new int[] {2, 3, 5, 7, 11, 101, 9876543211 };
//This example shows a 2 by 2 string array
//Here, names[0,0] = "Katie" and names[1,1] = "Bianca"
string[,] names = { { "Katie", "Sydney" }, { "Edmund", "Bianca"} };
//This example shows an array of arrays.
//Here, x[0] is an int array of length three with values 1, 2, 3.
//Also, x[1][1] = 12 and x[2][4] = 25.
//Attempting to reference x[3] or x[1][2] will throw an exception int[][] x = { { 1, 2, 3 }, { 11, 12 }, { 21, 22, 23, 24, 25} };
A.4.3 MAIN
A program has to start somewhere. In C and C++ programs, the global procedure main is the defined entry point for the program. This starting point is referred to as the entry point for the program.
In C#, a class must define a static method called Main to serve as the entry point. The method must have one of the following signatures.
static void Main()
static void Main(string[] args)
static int Main()
static int Main(string[] args)
A program will return a value if the Main method returns a value. A program can receive command-line arguments by specifying an array of string objects as the only parameter to the Main method.
If two or more classes in a program contain a Main method, then the /main switch must be used with the C# compiler to specify which method to consider the entry point for the program.
A.4.4 BOXING
By definition, the object class is a reference type. However, it also serves as the ultimate base class for all types, including the built-in types. As a result, value types such as int and bool can be used wherever an object instance is required. For example, the ArrayList class represents a dynamically-sized array, and includes an Add method to add an object to the array. This method is declared as follows:
public virtual int Add(object value);
SPECIAL FEATURES |
671 |
Within the Add method, a reference type is expected. So what happens when a value type is passed into this method? Clearly, an explicit mechanism for treating value types as a reference type is required.
This mechanism is called boxing. Boxing implicitly copies the data in a value type into an object instance allocated on the heap. For example:
//Boxing of an integer constant object obj = 123;
//Boxing of an int type. ArrayList list = new ArrayList(); int x = 32768;
list.Add(x);
A boxed value is converted back into a value type through a process called unboxing. Conceptually, boxing and unboxing happens automatically and the programmer can remain blissfully unaware of this concept. Boxed values can be treated as their unboxed equivalents. For example:
int n = 5;
object obj = 123;
if (obj is int)
n = (int) obj;
These statements are perfectly legal, and result in the value of 123 for the variable n. The reason boxing is important is because of the performance implications involved. The boxing and unboxing of values takes time, and this can seriously impact the performance of an application.
Note in particular that boxing occurs when a structure, which is a value type, is cast to an interface, which is a reference type. For this reason, care should be taken when creating structures that support one or more interfaces. In such a situation, the performance implications of boxing might warrant using a class instead of a structure.
A.4.5 DOCUMENTATION
A final topic worth mentioning in this appendix is that of automated documentation. C# supports a set of XML-style tags that can be used in comments and extracted by the compiler. Such comments must begin with a triple-slash (///) and can occur before the declaration of most types and type members.
The C# compiler supports the /doc switch to generate the XML documentation file. Details on this process and the resulting output are available in the .NET documentation.
672 |
APPENDIX A C# PRIMER |
The following table provides a summary of the tags that are currently recognized by the compiler. An example using the <summary> tag appears in section 5.2.1 on page 134.
C# documentation tags
Tag |
Purpose |
|
|
<c> |
Specifies text that should be marked as code. |
<code> |
Specifies multiple lines that should be marked as code. |
<example> |
Documents an example of a type or method. |
<exception> |
Specifies documentation for an exception class. |
<include> |
Includes an external file to include in the documentation. |
<list> |
Specifies a list of items within another tag. This supports bulleted lists, |
|
numbered lists, and tables. |
<para> |
Starts a new paragraph within another tag. |
<param> |
Documents a parameter within a method or other construct. |
<paramref> |
Specifies text that should be marked as a parameter. |
<permission> |
Documents the accessibility level of a member. |
<remarks> |
Documents general comments about a type or type member. |
<returns> |
Documents the return value of a method. |
<see> |
Specifies a link in running text to another member or field accessible from the |
|
current file. |
<seealso> |
Specifies a link in a See Also section to another member of field accessible form |
|
the current file. |
<summary> |
Documents a short description of the member or type. |
<value> |
Documents a short description of a property. |
|
|
SPECIAL FEATURES |
673 |
A P P E N D I X B
.NET namespaces
B.1 |
System.Collections 675 |
B.8 |
System.Reflection 677 |
B.2 |
System.ComponentModel 675 |
B.9 |
System.Resources 677 |
B.3 |
System.Data 675 |
B.10 |
System.Security 678 |
B.4 |
System.Drawing 675 |
B.11 |
System.Threading 678 |
B.5 |
System.Globalization 676 |
B.12 |
System.Web 679 |
B.6 |
System.IO 676 |
B.13 |
System.Windows.Forms 679 |
B.7 |
System.Net 676 |
B.14 |
System.XML 679 |
This appendix provides an overview of some of the System namespaces provided by Microsoft in the .NET Framework, and discusses their relationship to Windows Forms applications. For a complete list of namespaces in .NET, see the .NET Framework Class Library documentation.
The System namespace contains the commonly-used types1 required by .NET programs and libraries, as well as services such as data type conversion, environment management, and mathematical operations. In particular, most of the classes mentioned in Appendix A that implement core functionality such as the built-in types, enumerations, and delegates are included in this namespace. Members of this namespace are discussed throughout the book as they are used in the sample programs.
1 The word type is used in the C# sense here, as defined in Appendix A. More generally, a type can be a class, structure, interface, enumeration, or a delegate. By definition, a namespace defines one or more types.
674