Jones / MacDonald / Rajan | Visual C# 2010 Recipes | E-Book | www.sack.de
E-Book

E-Book, Englisch, 1016 Seiten

Jones / MacDonald / Rajan Visual C# 2010 Recipes

A Problem-Solution Approach
1. ed
ISBN: 978-1-4302-2526-3
Verlag: Apress
Format: PDF
Kopierschutz: 1 - PDF Watermark

A Problem-Solution Approach

E-Book, Englisch, 1016 Seiten

ISBN: 978-1-4302-2526-3
Verlag: Apress
Format: PDF
Kopierschutz: 1 - PDF Watermark



Mastering the development of .NET 4.0 applications in C# is less about knowing the Visual C# 2010 language and more about knowing how to use the functionality of the .NET framework class library most effectively. Visual C# 2010 Recipes explores the breadth of the .NET Framework class library and provides specific solutions to common and interesting programming problems. Each recipe is presented in a succinct problem/solution format and is accompanied by a working code sample to help you understand the concept and quickly apply it. When you are facing a Visual C# 2010 problem, this book likely contains a recipe providing the solution-or at least points you in the right direction. Even if you are simply looking to broaden your knowledge of the .NET framework class library, Visual C# 2010 Recipes is the perfect resource to assist you. This is an updated reference for .NET 4.0 programmers. All code samples come as stand-alone Visual Studio 2010 solutions for your convenience.

Allen Jones has more than 15 years of experience covering a wide range of information technology disciplines in a variety of sectors; however, his true passion has always been software development. Allen is chief architect at SmithBayes, a U.K.-based software firm that develops high-end strategic decision support software derived from technology used in Formula 1 motor racing.

Jones / MacDonald / Rajan Visual C# 2010 Recipes jetzt bestellen!

Weitere Infos & Material


1;Contents at a Glance;5
2;Table of Contents;7
3;About the Authors;21
4;About the Technical Reviewer;22
5;Acknowledgments;23
6;Introduction;24
7;CHAPTER 1 Application Development;25
7.1;1-1. Create a Console Application from the Command Line;26
7.1.1;Problem;26
7.1.2;Solution;26
7.1.3;How It Works;27
7.1.3.1;The Code;27
7.1.3.2;Usage;28
7.2;1-2. Create a Windows-Based Application from the Command Line;29
7.2.1;Problem;29
7.2.2;Solution;29
7.2.3;How It Works;29
7.2.3.1;The Code;30
7.2.3.2;Usage;32
7.3;1-3. Create and Use a Code Module;33
7.3.1;Problem;33
7.3.2;Solution;33
7.3.3;How It Works;33
7.3.3.1;Usage;34
7.4;1-4. Create and Use a Code Library from the Command Line;35
7.4.1;Problem;35
7.4.2;Solution;35
7.4.3;How It Works;35
7.4.3.1;Usage;35
7.5;1-5. Access Command-Line Arguments;36
7.5.1;Problem;36
7.5.2;Solution;36
7.5.3;How It Works;36
7.5.3.1;The Code;37
7.5.3.2;Usage;37
7.6;1-6. Include Code Selectively at Build Time;38
7.6.1;Problem;38
7.6.2;Solution;38
7.6.3;How It Works;39
7.6.3.1;The Code;40
7.6.3.2;Usage;41
7.6.4;Notes;41
7.7;1-7. Access a Program Element That Has the Same Name As a Keyword;42
7.7.1;Problem;42
7.7.2;Solution;42
7.7.3;How It Works;42
7.7.3.1;The Code;42
7.8;1-8. Create and Manage Strongly Named Key Pairs;43
7.8.1;Problem;43
7.8.2;Solution;43
7.8.3;How It Works;43
7.9;1-9. Give an Assembly a Strong Name;45
7.9.1;Problem;45
7.9.2;Solution;45
7.9.3;How It Works;46
7.9.3.1;The Code;46
7.9.3.2;Usage;47
7.10;1-10. Verify That a Strongly Named Assembly Has Not Been Modified;47
7.10.1;Problem;47
7.10.2;Solution;47
7.10.3;How It Works;47
7.11;1-11. Delay-Sign an Assembly;48
7.11.1;Problem;48
7.11.2;Solution;49
7.11.3;How It Works;49
7.12;1-12. Sign an Assembly with an Authenticode Digital Signature;50
7.12.1;Problem;50
7.12.2;Solution;50
7.12.3;How It Works;51
7.12.3.1;Usage;51
7.13;1-13. Create and Trust a Test Software Publisher Certificate;52
7.13.1;Problem;52
7.13.2;Solution;52
7.13.3;How It Works;52
7.14;1-14. Manage the Global Assembly Cache;53
7.14.1;Problem;53
7.14.2;Solution;54
7.14.3;How It Works;54
7.15;1-15. Prevent People from Decompiling Your Code;54
7.15.1;Problem;54
7.15.2;Solution;54
7.15.3;How It Works;55
7.16;1-16. Manipulate the Appearance of the Console;55
7.16.1;Problem;55
7.16.2;Solution;56
7.16.3;How It Works;56
7.16.3.1;The Code;57
7.17;1-17. Create a Static Class;58
7.17.1;Problem;58
7.17.2;Solution;58
7.17.3;How It Works;58
7.17.3.1;The Code;58
7.18;1-18. Create an Anonymous Type;59
7.18.1;Problem;59
7.18.2;Solution;59
7.18.3;How It Works;59
7.18.3.1;The Code;60
7.19;1-19. Create an ExpandoObject Dynamic Type;61
7.19.1;Problem;61
7.19.2;Solution;61
7.19.3;How It Works;61
7.19.3.1;The Code;62
7.20;1-20. Define an Automatically Implemented Property;63
7.20.1;Problem;63
7.20.2;Solution;63
7.20.3;How It Works;63
7.20.3.1;The Code;63
7.21;1-21. Overload an Operator;65
7.21.1;Problem;65
7.21.2;Solution;65
7.21.3;How It Works;65
7.21.3.1;The Code;66
7.22;1-22. Define a Conversion Operator;68
7.22.1;Problem;68
7.22.2;Solution;68
7.22.3;How It Works;68
7.22.3.1;The Code;69
7.23;1-23. Handle an Event with an Anonymous Function;70
7.23.1;Problem;70
7.23.2;Solution;70
7.23.3;How It Works;70
7.23.3.1;The Code;71
7.24;1-24. Implement a Custom Indexer;72
7.24.1;Problem;72
7.24.2;Solution;72
7.24.3;How It Works;72
7.24.3.1;The Code;72
8;CHAPTER 2 Data Manipulation;76
8.1;2-1. Manipulate the Contents of a String Efficiently;77
8.1.1;Problem;77
8.1.2;Solution;77
8.1.3;How It Works;77
8.1.3.1;The Code;78
8.2;2-2. Encode a String Using Alternate Character Encoding;79
8.2.1;Problem;79
8.2.2;Solution;79
8.2.3;How It Works;79
8.2.3.1;The Code;80
8.2.3.2;Usage;81
8.3;2-3. Convert Basic Value Types to Byte Arrays;82
8.3.1;Problem;82
8.3.2;Solution;82
8.3.3;How It Works;82
8.3.3.1;The Code;83
8.4;2-4. Base64 Encode Binary Data;84
8.4.1;Problem;84
8.4.2;Solution;85
8.4.3;How It Works;85
8.4.3.1;The Code;85
8.5;2-5. Validate Input Using Regular Expressions;88
8.5.1;Problem;88
8.5.2;Solution;89
8.5.3;How It Works;89
8.5.3.1;The Code;91
8.5.3.2;Usage;92
8.5.4;Notes;92
8.6;2-6. Use Compiled Regular Expressions;93
8.6.1;Problem;93
8.6.2;Solution;93
8.6.3;How It Works;93
8.6.3.1;The Code;94
8.7;2-7. Create Dates and Times from Strings;95
8.7.1;Problem;95
8.7.2;Solution;95
8.7.3;How It Works;96
8.7.3.1;The Code;96
8.8;2-8. Add, Subtract, and Compare Dates and Times;97
8.8.1;Problem;97
8.8.2;Solution;98
8.8.3;How It Works;98
8.8.3.1;The Code;99
8.9;2-9. Sort an Array or a Collection;100
8.9.1;Problem;100
8.9.2;Solution;100
8.9.3;How It Works;100
8.9.3.1;The Code;101
8.10;2-10. Copy a Collection to an Array;102
8.10.1;Problem;102
8.10.2;Solution;102
8.10.3;How It Works;103
8.10.3.1;The Code;103
8.11;2-11. Use a Strongly Typed Collection;104
8.11.1;Problem;104
8.11.2;Solution;104
8.11.3;How It Works;105
8.11.3.1;The Code;105
8.12;2-12. Create a Generic Type;106
8.12.1;Problem;106
8.12.2;Solution;106
8.12.3;How It Works;106
8.12.3.1;The Code;107
8.13;2-13. Store a Serializable Object to a File;110
8.13.1;Problem;110
8.13.2;Solution;110
8.13.3;How It Works;110
8.13.3.1;The Code;110
8.14;2-14. Serialize an Object Using JSON;113
8.14.1;Problem;113
8.14.2;Solution;114
8.14.3;How It Works;114
8.14.3.1;The Code;115
8.15;2-15. Read User Input from the Console;116
8.15.1;Problem;116
8.15.2;Solution;116
8.15.3;How It Works;116
8.15.3.1;The Code;117
8.16;2-16. Using Large Integer Values;119
8.16.1;Problem;119
8.16.2;Solution;119
8.16.3;How It Works;119
8.16.3.1;The Code;119
8.17;2-17. Select Collection or Array Elements;120
8.17.1;Problem;120
8.17.2;Solution;120
8.17.3;How It Works;120
8.17.3.1;The Code;121
8.18;2-18. Remove Duplicate Items from an Array or Collection;123
8.18.1;Problem;123
8.18.2;Solution;123
8.18.3;How It Works;123
8.18.3.1;The Code;123
9;CHAPTER 3 Application Domains, Reflection, and Metadata;125
9.1;3-1. Create an Application Domain;126
9.1.1;Problem;126
9.1.2;Solution;126
9.1.3;How It Works;126
9.1.3.1;The Code;127
9.2;3-2. Create Types That Can Be Passed Across Application Domain Boundaries;128
9.2.1;Problem;128
9.2.2;Solution;128
9.2.3;How It Works;128
9.2.3.1;The Code;129
9.3;3-3. Avoid Loading Unnecessary Assemblies into Application Domains;131
9.3.1;Problem;131
9.3.2;Solution;131
9.3.3;How It Works;131
9.3.3.1;The Code;132
9.4;3-4. Create a Type That Cannot Cross Application Domain Boundaries;133
9.4.1;Problem;133
9.4.2;Solution;133
9.4.3;How It Works;133
9.5;3-5. Load an Assembly into the Current Application Domain;133
9.5.1;Problem;133
9.5.2;Solution;133
9.5.3;How It Works;134
9.5.3.1;The Code;135
9.6;3-6. Execute an Assembly in a Different Application Domain;136
9.6.1;Problem;136
9.6.2;Solution;136
9.6.3;How It Works;136
9.6.3.1;The Code;137
9.6.3.2;Usage;138
9.7;3-7. Instantiate a Type in a Different Application Domain;138
9.7.1;Problem;138
9.7.2;Solution;138
9.7.3;How It Works;138
9.7.3.1;The Code;139
9.8;3-8. Pass Data Between Application Domains;143
9.8.1;Problem;143
9.8.2;Solution;143
9.8.3;How It Works;143
9.8.3.1;The Code;144
9.9;3-9. Unload Assemblies and Application Domains;146
9.9.1;Problem;146
9.9.2;Solution;146
9.9.3;How It Works;146
9.9.3.1;The Code;146
9.10;3-10. Retrieve Type Information;147
9.10.1;Problem;147
9.10.2;Solution;147
9.10.3;How It Works;147
9.10.3.1;The Code;148
9.11;3-11. Test an Object’s Type;149
9.11.1;Problem;149
9.11.2;Solution;149
9.11.3;How It Works;149
9.11.3.1;The Code;150
9.12;3-12. Instantiate an Object Using Reflection;151
9.12.1;Problem;151
9.12.2;Solution;152
9.12.3;How It Works;152
9.12.3.1;The Code;152
9.13;3-13. Create a Custom Attribute;155
9.13.1;Problem;155
9.13.2;Solution;155
9.13.3;How It Works;155
9.13.3.1;The Code;156
9.13.3.2;Usage;157
9.14;3-14. Inspect the Attributes of a Program Element Using Reflection;158
9.14.1;Problem;158
9.14.2;Solution;158
9.14.3;How It Works;158
9.14.3.1;The Code;159
9.15;3-15. Programmatically Discover the Members of a Type;159
9.15.1;Problem;159
9.15.2;Solution;160
9.15.3;How It Works;160
9.15.3.1;The Code;160
9.16;3-16. Invoke a Type Member Using Reflection;162
9.16.1;Problem;162
9.16.2;Solution;162
9.16.3;How It Works;162
9.16.3.1;The Code;162
9.17;3-17. Dynamically Invoke a Type Member;164
9.17.1;Problem;164
9.17.2;Solution;164
9.17.3;How It Works;164
9.17.3.1;The Code;164
9.18;3-18. Create a Custom Dynamic Type;165
9.18.1;Problem;165
9.18.2;Solution;165
9.18.3;How It Works;165
9.18.3.1;The Code;167
10;CHAPTER 4 Threads, Processes, and Synchronization;170
10.1;4-1. Execute a Method Using the Thread Pool;172
10.1.1;Problem;172
10.1.2;Solution;172
10.1.3;How It Works;172
10.1.3.1;The Code;172
10.1.4;Notes;174
10.2;4-2. Execute a Method Asynchronously;175
10.2.1;Problem;175
10.2.2;Solution;175
10.2.3;How It Works;176
10.2.3.1;The Code;177
10.3;4-3. Execute a Method Periodically;184
10.3.1;Problem;184
10.3.2;Solution;185
10.3.3;How It Works;185
10.3.3.1;The Code;185
10.4;4-4. Execute a Method at a Specific Time;187
10.4.1;Problem;187
10.4.2;Solution;187
10.4.3;How It Works;187
10.4.3.1;The Code;187
10.5;4-5. Execute a Method by Signaling a WaitHandle Object;188
10.5.1;Problem;188
10.5.2;Solution;188
10.5.3;How It Works;188
10.5.3.1;The Code;189
10.6;4-6. Execute a Method Using a New Thread;190
10.6.1;Problem;190
10.6.2;Solution;190
10.6.3;How It Works;190
10.6.3.1;The Code;191
10.7;4-7. Synchronize the Execution of Multiple Threads Using a Monitor;193
10.7.1;Problem;193
10.7.2;Solution;193
10.7.3;How It Works;193
10.7.3.1;The Code;195
10.8;4-8. Synchronize the Execution of Multiple Threads Using an Event;199
10.8.1;Problem;199
10.8.2;Solution;199
10.8.3;How It Works;199
10.8.3.1;The Code;200
10.9;4-9. Synchronize the Execution of Multiple Threads Using a Mutex;203
10.9.1;Problem;203
10.9.2;Solution;203
10.9.3;How It Works;203
10.9.3.1;The Code;203
10.10;4-10. Synchronize the Execution of Multiple Threads Using a Semaphore;206
10.10.1;Problem;206
10.10.2;Solution;206
10.10.3;How It Works;206
10.10.3.1;The Code;206
10.11;4-11. Synchronize Access to a Shared Data Value;208
10.11.1;Problem;208
10.11.2;Solution;208
10.11.3;How It Works;208
10.11.3.1;The Code;209
10.12;4-12. Know When a Thread Finishes;211
10.12.1;Problem;211
10.12.2;Solution;211
10.12.3;How It Works;211
10.12.3.1;The Code;211
10.13;4-13. Terminate the Execution of a Thread;212
10.13.1;Problem;212
10.13.2;Solution;213
10.13.3;How It Works;213
10.13.3.1;The Code;213
10.14;4-14. Create a Thread-Safe Collection Instance;214
10.14.1;Problem;214
10.14.2;Solution;215
10.14.3;How It Works;215
10.14.3.1;The Code;216
10.15;4-15. Start a New Process;216
10.15.1;Problem;216
10.15.2;Solution;217
10.15.3;How It Works;217
10.15.3.1;The Code;218
10.16;4-16. Terminate a Process;220
10.16.1;Problem;220
10.16.2;Solution;220
10.16.3;How It Works;220
10.16.3.1;The Code;221
10.17;4-17. Ensure That Only One Instance of an Application Can Execute Concurrently;222
10.17.1;Problem;222
10.17.2;Solution;222
10.17.3;How It Works;222
10.17.3.1;The Code;223
11;CHAPTER 5 Files, Directories, and I/O;225
11.1;5-1. Retrieve Information About a File, Directory, or Drive;226
11.1.1;Problem;226
11.1.2;Solution;226
11.1.3;How It Works;226
11.1.3.1;The Code;228
11.1.3.2;Usage;230
11.2;5-2. Set File and Directory Attributes;231
11.2.1;Problem;231
11.2.2;Solution;231
11.2.3;How It Works;231
11.2.3.1;The Code;232
11.3;5-3. Copy, Move, or Delete a File or Directory;233
11.3.1;Problem;233
11.3.2;Solution;233
11.3.3;How It Works;233
11.3.3.1;The Code;234
11.4;5-4. Calculate the Size of a Directory;236
11.4.1;Problem;236
11.4.2;Solution;236
11.4.3;How It Works;236
11.4.3.1;The Code;236
11.5;5-5. Retrieve Version Information for a File;237
11.5.1;Problem;237
11.5.2;Solution;237
11.5.3;How It Works;237
11.5.3.1;The Code;238
11.5.3.2;Usage;238
11.6;5-6. Show a Just-in-Time Directory Tree in the TreeView Control;239
11.6.1;Problem;239
11.6.2;Solution;239
11.6.3;How It Works;240
11.6.3.1;The Code;240
11.7;5-7. Read and Write a Text File;242
11.7.1;Problem;242
11.7.2;Solution;242
11.7.3;How It Works;242
11.7.3.1;The Code;243
11.8;5-8. Read and Write a Binary File;244
11.8.1;Problem;244
11.8.2;Solution;245
11.8.3;How It Works;245
11.8.3.1;The Code;245
11.9;5-9. Read a File Asynchronously;246
11.9.1;Problem;246
11.9.2;Solution;246
11.9.3;How It Works;247
11.9.3.1;The Code;247
11.9.3.2;Usage;248
11.10;5-10. Find Files That Match a Wildcard Expression;250
11.10.1;Problem;250
11.10.2;Solution;250
11.10.3;How It Works;250
11.10.3.1;The Code;250
11.11;5-11. Test Two Files for Equality;251
11.11.1;Problem;251
11.11.2;Solution;251
11.11.3;How It Works;252
11.11.3.1;The Code;252
11.12;5-12. Manipulate Strings Representing File Names;253
11.12.1;Problem;253
11.12.2;Solution;253
11.12.3;How It Works;253
11.13;5-13. Determine If a Path Is a Directory or a File;255
11.13.1;Problem;255
11.13.2;Solution;255
11.13.3;How It Works;255
11.13.3.1;The Code;255
11.14;5-14. Work with Relative Paths;256
11.14.1;Problem;256
11.14.2;Solution;256
11.14.3;How It Works;256
11.14.3.1;The Code;256
11.14.3.2;Usage;257
11.15;5-15. Create a Temporary File;258
11.15.1;Problem;258
11.15.2;Solution;258
11.15.3;How It Works;258
11.15.3.1;The Code;258
11.16;5-16. Get the Total Free Space on a Drive;259
11.16.1;Problem;259
11.16.2;Solution;259
11.16.3;How It Works;259
11.16.3.1;The Code;259
11.17;5-17. Show the Common File Dialog Boxes;261
11.17.1;Problem;261
11.17.2;Solution;261
11.17.3;How It Works;261
11.17.3.1;The Code;262
11.18;5-18. Use an Isolated Store;263
11.18.1;Problem;263
11.18.2;Solution;264
11.18.3;How It Works;264
11.18.3.1;The Code;264
11.19;5-19. Monitor the File System for Changes;266
11.19.1;Problem;266
11.19.2;Solution;266
11.19.3;How It Works;266
11.19.3.1;The Code;267
11.20;5-20. Access a COM Port;268
11.20.1;Problem;268
11.20.2;Solution;268
11.20.3;How It Works;268
11.20.3.1;The Code;268
11.21;5-21. Get a Random File Name;269
11.21.1;Problem;269
11.21.2;Solution;269
11.21.3;How It Works;269
11.21.3.1;The Code;270
11.22;5-22. Manipulate the Access Control List of a File or Directory;270
11.22.1;Problem;270
11.22.2;Solution;270
11.22.3;How It Works;271
11.22.3.1;The Code;271
11.23;5-23. Compress Data;273
11.23.1;Problem;273
11.23.2;Solution;273
11.23.3;How It Works;273
11.23.3.1;The Code;273
11.24;5-24. Log Data to a File;274
11.24.1;Problem;274
11.24.2;Solution;274
11.24.3;How It Works;275
11.24.3.1;The Code;275
11.25;5-25. Process a Log File;276
11.25.1;Problem;276
11.25.2;Solution;276
11.25.3;How It Works;276
11.25.3.1;The Code;276
11.26;5-26. Communicate Between Processes;277
11.26.1;Problem;277
11.26.2;Solution;277
11.26.3;How It Works;277
11.26.3.1;The Code;277
12;CHAPTER 6 XML Processing;280
12.1;6-1. Show the Structure of an XML Document in a TreeView;280
12.1.1;Problem;280
12.1.2;Solution;281
12.1.3;How It Works;281
12.1.3.1;The Code;282
12.1.3.2;Usage;284
12.2;6-2. Insert Nodes in an XML Document;285
12.2.1;Problem;285
12.2.2;Solution;285
12.2.3;How It Works;285
12.2.3.1;The Code;286
12.3;6-3. Quickly Append Nodes in an XML Document;287
12.3.1;Problem;287
12.3.2;Solution;288
12.3.3;How It Works;288
12.3.3.1;The Code;288
12.4;6-4. Find Specific Elements by Name;290
12.4.1;Problem;290
12.4.2;Solution;290
12.4.3;How It Works;290
12.4.3.1;The Code;290
12.5;6-5. Get XML Nodes in a Specific XML Namespace;291
12.5.1;Problem;291
12.5.2;Solution;292
12.5.3;How It Works;292
12.5.3.1;The Code;292
12.6;6-6. Find Elements with an XPath Search;293
12.6.1;Problem;293
12.6.2;Solution;294
12.6.3;How It Works;294
12.6.3.1;The Code;294
12.6.4;Notes;295
12.7;6-7. Read and Write XML Without Loading an Entire Document into Memory;297
12.7.1;Problem;297
12.7.2;Solution;297
12.7.2.1;How It Works;297
12.7.2.2;The Code;298
12.8;6-8. Validate an XML Document Against a Schema;300
12.8.1;Problem;300
12.8.2;Solution;300
12.8.3;How It Works;300
12.8.3.1;The Code;302
12.9;6-9. Use XML Serialization with Custom Objects;305
12.9.1;Problem;305
12.9.2;Solution;305
12.9.3;How It Works;306
12.9.3.1;The Code;307
12.10;6-10. Create a Schema for a .NET Class;309
12.10.1;Problem;309
12.10.2;Solution;309
12.10.3;How It Works;309
12.10.3.1;Usage;309
12.11;6-11. Generate a Class from a Schema;310
12.11.1;Problem;310
12.11.2;Solution;310
12.11.3;How It Works;310
12.11.3.1;Usage;310
12.12;6-12. Perform an XSL Transform;311
12.12.1;Problem;311
12.12.2;Solution;311
12.12.3;How It Works;311
12.12.3.1;The Code;311
12.13;6-13. Load XML with LINQ;315
12.13.1;Problem;315
12.13.2;Solution;315
12.13.3;How It Works;315
12.13.3.1;The Code;315
12.14;6-14. Create a New XML Tree with LINQ;317
12.14.1;Problem;317
12.14.2;Solution;317
12.14.3;How It Works;317
12.14.3.1;The Code;318
12.15;6-15. Query XML with LINQ;319
12.15.1;Problem;319
12.15.2;Solution;320
12.15.3;How It Works;320
12.15.3.1;The Code;320
12.16;6-16. Modify an XML Tree with LINQ;322
12.16.1;The Problem;322
12.16.2;The Solution;322
12.16.3;How It Works;322
12.16.3.1;The Code;322
13;CHAPTER 7 Windows Forms;325
13.1;7-1. Add a Control Programmatically;327
13.1.1;Problem;327
13.1.2;Solution;327
13.1.3;How It Works;327
13.1.3.1;The Code;327
13.2;7-2. Store Data with a Control;329
13.2.1;Problem;329
13.2.2;Solution;329
13.2.3;How It Works;330
13.2.3.1;The Code;330
13.3;7-3. Process All the Controls on a Form;332
13.3.1;Problem;332
13.3.2;Solution;332
13.3.3;How It Works;332
13.3.3.1;The Code;332
13.4;7-4. Track the Visible Forms in an Application;333
13.4.1;Problem;333
13.4.2;Solution;333
13.4.3;How It Works;333
13.4.3.1;The Code;334
13.4.4;Notes;336
13.5;7-5. Find All MDI Child Forms;337
13.5.1;Problem;337
13.5.2;Solution;337
13.5.3;How It Works;337
13.5.3.1;The Code;337
13.6;7-6. Save Configuration Settings for a Form;340
13.6.1;Problem;340
13.6.2;Solution;340
13.6.3;How It Works;340
13.6.3.1;The Code;341
13.7;7-7. Force a List Box to Scroll to the Most Recently Added Item;343
13.7.1;Problem;343
13.7.2;Solution;343
13.7.3;How It Works;343
13.7.3.1;The Code;343
13.8;7-8. Restrict a Text Box to Accept Only Specific Input;344
13.8.1;Problem;344
13.8.2;Solution;344
13.8.3;How It Works;344
13.8.3.1;The Code;345
13.8.4;Notes;347
13.9;7-9. Use an Autocomplete Combo Box or Text Box;347
13.9.1;Problem;347
13.9.2;Solution;347
13.9.3;How It Works;347
13.9.3.1;The Code;348
13.10;7-10. Sort a List View by Any Column;350
13.10.1;Problem;350
13.10.2;Solution;350
13.10.3;How It Works;350
13.10.3.1;The Code;350
13.11;7-11. Lay Out Controls Automatically;353
13.11.1;Problem;353
13.11.2;Solution;353
13.11.3;How It Works;353
13.12;7-12. Use Part of a Main Menu for a Context Menu;354
13.12.1;Problem;354
13.12.2;Solution;354
13.12.3;How It Works;354
13.12.3.1;The Code;354
13.13;7-13. Make a Multilingual Form;356
13.13.1;Problem;356
13.13.2;Solution;356
13.13.3;How It Works;356
13.13.3.1;The Code;358
13.14;7-14. Create a Form That Cannot Be Moved;359
13.14.1;Problem;359
13.14.2;Solution;359
13.14.3;How It Works;359
13.14.3.1;The Code;360
13.15;7-15. Make a Borderless Form Movable;361
13.15.1;Problem;361
13.15.2;Solution;361
13.15.3;How It Works;361
13.15.3.1;The Code;362
13.16;7-16. Create an Animated System Tray Icon;364
13.16.1;Problem;364
13.16.2;Solution;364
13.16.3;How It Works;364
13.16.3.1;The Code;364
13.17;7-17. Validate an Input Control;365
13.17.1;Problem;365
13.17.2;Solution;366
13.17.3;How It Works;366
13.17.3.1;The Code;366
13.18;7-18. Use a Drag-and-Drop Operation;368
13.18.1;Problem;368
13.18.2;Solution;368
13.18.3;How It Works;369
13.18.3.1;The Code;369
13.19;7-19. Update the User Interface in a Multithreaded Application;372
13.19.1;Problem;372
13.19.2;Solution;372
13.19.3;How It Works;372
13.19.3.1;The Code;373
13.20;7-20. Display a Web Page in a Windows-Based Application;374
13.20.1;Problem;374
13.20.2;Solution;374
13.20.3;How It Works;375
13.20.3.1;The Code;376
13.21;7-21. Display WPF Windows in a Windows Forms Application;378
13.21.1;Problem;378
13.21.2;Solution;378
13.21.3;How It Works;378
13.21.3.1;The Code;378
13.22;7-22. Display WPF Controls in Windows Forms;381
13.22.1;Problem;381
13.22.2;Solution;381
13.22.3;How It Works;381
13.22.3.1;The Code;382
14;CHAPTER 8 Graphics, Multimedia, and Printing;386
14.1;8-1. Find All Installed Fonts;387
14.1.1;Problem;387
14.1.2;Solution;387
14.1.3;How It Works;387
14.1.3.1;The Code;387
14.2;8-2. Perform Hit Testing with Shapes;389
14.2.1;Problem;389
14.2.2;Solution;389
14.2.3;How It Works;389
14.2.3.1;The Code;390
14.3;8-3. Create an Irregularly Shaped Control;393
14.3.1;Problem;393
14.3.2;Solution;393
14.3.3;How It Works;393
14.3.3.1;The Code;394
14.4;8-4. Create a Movable Sprite;396
14.4.1;Problem;396
14.4.2;Solution;396
14.4.3;How It Works;396
14.4.3.1;The Code;396
14.5;8-5. Create a Scrollable Image;400
14.5.1;Problem;400
14.5.2;Solution;400
14.5.3;How It Works;400
14.5.3.1;The Code;400
14.6;8-6. Perform a Screen Capture;402
14.6.1;Problem;402
14.6.2;Solution;402
14.6.3;How It Works;402
14.6.3.1;The Code;402
14.7;8-7. Use Double Buffering to Increase Redraw Speed;403
14.7.1;Problem;403
14.7.2;Solution;403
14.7.3;How It Works;404
14.7.3.1;The Code;404
14.8;8-8. Show a Thumbnail for an Image;406
14.8.1;Problem;406
14.8.2;Solution;406
14.8.3;How It Works;406
14.8.3.1;The Code;406
14.9;8-9. Play a Simple Beep or System Sound;408
14.9.1;Problem;408
14.9.2;Solution;408
14.9.3;How It Works;408
14.9.3.1;The Code;408
14.10;8-10. Play a WAV File;409
14.10.1;Problem;409
14.10.2;Solution;409
14.10.3;How It Works;409
14.10.3.1;The Code;409
14.11;8-11. Play a Sound File;410
14.11.1;Problem;410
14.11.2;Solution;411
14.11.3;How It Works;411
14.11.3.1;The Code;411
14.12;8-12. Play a Video;412
14.12.1;Problem;412
14.12.2;Solution;412
14.12.3;How It Works;412
14.12.3.1;The Code;413
14.13;8-13. Retrieve Information About Installed Printers;415
14.13.1;Problem;415
14.13.2;Solution;415
14.13.3;How It Works;415
14.13.3.1;The Code;416
14.13.3.2;Usage;417
14.14;8-14. Print a Simple Document;418
14.14.1;Problem;418
14.14.2;Solution;418
14.14.3;How It Works;418
14.14.3.1;The Code;420
14.15;8-15. Print a Multipage Document;421
14.15.1;Problem;421
14.15.2;Solution;421
14.15.3;How It Works;422
14.15.3.1;The Code;422
14.16;8-16. Print Wrapped Text;425
14.16.1;Problem;425
14.16.2;Solution;425
14.16.3;How It Works;425
14.16.3.1;The Code;425
14.17;8-17. Show a Dynamic Print Preview;427
14.17.1;Problem;427
14.17.2;Solution;427
14.17.3;How It Works;427
14.17.3.1;The Code;428
14.18;8-18. Manage Print Jobs;429
14.18.1;Problem;429
14.18.2;Solution;429
14.18.3;How It Works;430
14.18.3.1;The Code;430
14.19;8-19. Perform Text-to-Speech;434
14.19.1;Problem;434
14.19.2;Solution;434
14.19.3;How It Works;435
14.19.3.1;The Code;435
14.20;8-20. Recognize Characters in an Image (OCR);436
14.20.1;Problem;436
14.20.2;Solution;437
14.20.3;How It Works;437
14.20.3.1;The Code;438
15;CHAPTER 9 Database Access;440
15.1;9-1. Connect to a Database;442
15.1.1;Problem;442
15.1.2;Solution;442
15.1.3;How It Works;442
15.1.3.1;The Code;443
15.2;9-2. Use Connection Pooling;445
15.2.1;Problem;445
15.2.2;Solution;445
15.2.3;How It Works;445
15.2.3.1;The Code;446
15.2.4;Notes;448
15.3;9-3. Create a Database Connection String Programmatically;448
15.3.1;Problem;448
15.3.2;Solution;448
15.3.3;How It Works;448
15.3.3.1;The Code;449
15.4;9-4. Store a Database Connection String Securely;450
15.4.1;Problem;450
15.4.2;Solution;450
15.4.3;How It Works;450
15.4.3.1;The Code;451
15.5;9-5. Execute a SQL Command or Stored Procedure;453
15.5.1;Problem;453
15.5.2;Solution;454
15.5.3;How It Works;454
15.5.3.1;The Code;455
15.6;9-6. Use Parameters in a SQL Command or Stored Procedure;458
15.6.1;Problem;458
15.6.2;Solution;458
15.6.3;How It Works;458
15.6.3.1;The Code;459
15.7;9-7. Process the Results of a SQL Query Using a Data Reader;462
15.7.1;Problem;462
15.7.2;Solution;462
15.7.3;How It Works;462
15.7.3.1;The Code;464
15.8;9-8. Obtain an XML Document from a SQL Server Query;465
15.8.1;Problem;465
15.8.2;Solution;465
15.8.3;How It Works;466
15.8.3.1;The Code;467
15.9;9-9. Perform Asynchronous Database Operations Against SQL Server;469
15.9.1;Problem;469
15.9.2;Solution;469
15.9.3;How It Works;469
15.9.3.1;The Code;471
15.10;9-10. Write Database-Independent Code;473
15.10.1;Problem;473
15.10.2;Solution;473
15.10.3;How It Works;473
15.10.3.1;The Code;476
15.11;9-11. Discover All Instances of SQL Server on Your Network;477
15.11.1;Problem;477
15.11.2;Solution;477
15.11.3;How It Works;478
15.11.3.1;The Code;478
15.12;9-12. Create an In-Memory Cache;479
15.12.1;Problem;479
15.12.2;Solution;479
15.12.3;How It Works;479
15.12.3.1;The Code;480
15.13;9-13. Create a DataSet Programmatically;483
15.13.1;Problem;483
15.13.2;Solution;483
15.13.3;How It Works;483
15.13.3.1;The Code;483
15.14;9-14. Perform a LINQ Query;485
15.14.1;Problem;485
15.14.2;Solution;486
15.14.3;How It Works;486
15.14.3.1;The Code;486
15.15;9-15. Perform a LINQ Query with Entity Types;488
15.15.1;Problem;488
15.15.2;Solution;488
15.15.3;How It Works;488
15.15.3.1;The Code;489
15.16;9-16. Compare LINQ DataSet Results;490
15.16.1;Problem;490
15.16.2;Solution;490
15.16.3;How It Works;491
15.16.4;The Code;491
16;CHAPTER 10 Networking;495
16.1;10-1. Obtain Information About the Local Network Interface;496
16.1.1;Problem;496
16.1.2;Solution;496
16.1.3;How It Works;496
16.1.3.1;The Code;498
16.2;10-2. Detect Changes in Network Connectivity;500
16.2.1;Problem;500
16.2.2;Solution;500
16.2.3;How It Works;500
16.2.3.1;The Code;500
16.3;10-3. Download Data over HTTP or FTP;502
16.3.1;Problem;502
16.3.2;Solution;502
16.3.3;How It Works;502
16.3.3.1;The Code;503
16.3.4;Notes;505
16.4;10-4. Download a File and Process It Using a Stream;506
16.4.1;Problem;506
16.4.2;Solution;506
16.4.3;How It Works;506
16.4.3.1;The Code;507
16.5;10-5. Respond to HTTP Requests from Within Your Application;508
16.5.1;Problem;508
16.5.2;Solution;509
16.5.3;How It Works;509
16.5.3.1;The Code;510
16.6;10-6. Get an HTML Page from a Site That Requires Authentication;513
16.6.1;Problem;513
16.6.2;Solution;513
16.6.3;How It Works;513
16.6.3.1;The Code;514
16.7;10-7. Send E-mail Using SMTP;515
16.7.1;Problem;515
16.7.2;Solution;516
16.7.3;How It Works;516
16.7.3.1;The Code;518
16.8;10-8. Resolve a Host Name to an IP Address;520
16.8.1;Problem;520
16.8.2;Solution;520
16.8.3;How It Works;521
16.8.3.1;The Code;521
16.8.3.2;Usage;522
16.9;10-9. Ping an IP Address;523
16.9.1;Problem;523
16.9.2;Solution;523
16.9.3;How It Works;523
16.9.3.1;The Code;524
16.9.3.2;Usage;525
16.10;10-10. Communicate Using TCP;526
16.10.1;Problem;526
16.10.2;Solution;526
16.10.3;How It Works;526
16.10.3.1;The Code;527
16.10.3.2;Usage;530
16.11;10-11. Create a Multithreaded TCP Server That Supports Asynchronous Communications;531
16.11.1;Problem;531
16.11.2;Solution;531
16.11.3;How It Works;531
16.11.3.1;The Code;532
16.12;10-12. Communicate Using UDP;539
16.12.1;Problem;539
16.12.2;Solution;539
16.12.3;How It Works;540
16.12.3.1;The Code;540
16.13;10-13. Create a SOAP-Based Web Service;542
16.13.1;Problem;542
16.13.2;Solution;542
16.13.3;How It Works;543
16.13.3.1;The Code;543
16.14;10-14. Call a WCF Service Using a Dynamically Generated Service Proxy;548
16.14.1;Problem;548
16.14.2;Solution;549
16.14.3;How It Works;549
16.14.3.1;The Code;549
16.15;10-15. Process the Content of an Atom or RSS Feed;550
16.15.1;Problem;550
16.15.2;Solution;550
16.15.3;How It Works;550
16.15.3.1;The Code;552
16.16;10-16. Manipulate URIs;554
16.16.1;Problem;554
16.16.2;Solution;554
16.16.3;How It Works;554
16.16.3.1;The Code;556
17;CHAPTER 11 Security and Cryptography;558
17.1;11-1. Allow Partially Trusted Code to Use Your Strongly Named Assembly;559
17.1.1;Problem;559
17.1.2;Solution;559
17.1.3;How It Works;560
17.1.3.1;The Code;561
17.2;11-2. Disable Code Access Security;562
17.2.1;Problem;562
17.2.2;Solution;562
17.2.3;How It Works;562
17.3;11-3. Disable Execution Permission Checks;563
17.3.1;Problem;563
17.3.2;Solution;563
17.3.3;How It Works;563
17.3.3.1;The Code;563
17.4;11-4. Ensure the Runtime Grants Specific Permissions to Your Assembly;564
17.4.1;Problem;564
17.4.2;Solution;565
17.4.3;How It Works;565
17.4.3.1;The Code;566
17.5;11-5. Limit the Permissions Granted to Your Assembly;567
17.5.1;Problem;567
17.5.2;Solution;567
17.5.3;How It Works;567
17.5.3.1;The Code;568
17.6;11-6. View the Permissions Required by an Assembly;569
17.6.1;Problem;569
17.6.2;Solution;569
17.6.3;How It Works;569
17.6.3.1;The Code;570
17.6.3.2;Usage;571
17.7;11-7. Determine at Runtime If Your Code Has a Specific Permission;573
17.7.1;Problem;573
17.7.2;Solution;573
17.7.3;How It Works;573
17.7.3.1;The Code;574
17.8;11-8. Restrict Who Can Extend Your Classes and Override Class Members;574
17.8.1;Problem;574
17.8.2;Solution;575
17.8.3;How It Works;575
17.8.3.1;The Code;576
17.9;11-9. Inspect an Assembly’s Evidence;577
17.9.1;Problem;577
17.9.2;Solution;577
17.9.3;How It Works;577
17.9.3.1;The Code;577
17.10;11-10. Determine If the Current User Is a Member of a Specific Windows Group;579
17.10.1;Problem;579
17.10.2;Solution;579
17.10.3;How It Works;579
17.10.3.1;The Code;581
17.10.3.2;Usage;582
17.11;11-11. Restrict Which Users Can Execute Your Code;583
17.11.1;Problem;583
17.11.2;Solution;583
17.11.3;How It Works;583
17.11.3.1;The Code;585
17.12;11-12. Impersonate a Windows User;587
17.12.1;Problem;587
17.12.2;Solution;587
17.12.3;How It Works;587
17.12.3.1;The Code;588
17.12.3.2;Usage;590
17.13;11-13. Create a Cryptographically Random Number;590
17.13.1;Problem;590
17.13.2;Solution;590
17.13.3;How It Works;591
17.13.3.1;The Code;591
17.14;11-14. Calculate the Hash Code of a Password;592
17.14.1;Problem;592
17.14.2;Solution;592
17.14.3;How It Works;593
17.14.3.1;The Code;594
17.14.3.2;Usage;595
17.15;11-15. Calculate the Hash Code of a File;596
17.15.1;Problem;596
17.15.2;Solution;596
17.15.3;How It Works;596
17.15.3.1;The Code;597
17.15.3.2;Usage;597
17.16;11-16. Verify a Hash Code;598
17.16.1;Problem;598
17.16.2;Solution;598
17.16.3;How It Works;598
17.16.3.1;The Code;599
17.17;11-17. Ensure Data Integrity Using a Keyed Hash Code;601
17.17.1;Problem;601
17.17.2;Solution;601
17.17.3;How It Works;601
17.17.3.1;The Code;602
17.17.3.2;Usage;603
17.18;11-18. Work with Security-Sensitive Strings in Memory;604
17.18.1;Problem;604
17.18.2;Solution;604
17.18.3;How It Works;604
17.18.3.1;The Code;605
17.19;11-19. Encrypt and Decrypt Data Using the Data Protection API;607
17.19.1;Problem;607
17.19.2;Solution;607
17.19.3;How It Works;608
17.19.3.1;The Code;609
18;CHAPTER 12 Unmanaged Code Interoperability;611
18.1;12-1. Call a Function in an Unmanaged DLL;611
18.1.1;Problem;611
18.1.2;Solution;612
18.1.3;How It Works;612
18.1.3.1;The Code;612
18.2;12-2. Get the Handle for a Control, Window, or File;615
18.2.1;Problem;615
18.2.2;Solution;615
18.2.3;How It Works;615
18.2.3.1;The Code;615
18.3;12-3. Call an Unmanaged Function That Uses a Structure;617
18.3.1;Problem;617
18.3.2;Solution;617
18.3.3;How It Works;617
18.3.3.1;The Code;617
18.4;12-4. Call an Unmanaged Function That Uses a Callback;620
18.4.1;Problem;620
18.4.2;Solution;620
18.4.3;How It Works;620
18.4.3.1;The Code;621
18.5;12-5. Retrieve Unmanaged Error Information;622
18.5.1;Problem;622
18.5.2;Solution;622
18.5.3;How It Works;622
18.5.3.1;The Code;622
18.6;12-6. Use a COM Component in a .NET Client;624
18.6.1;Problem;624
18.6.2;Solution;624
18.6.3;How It Works;624
18.6.3.1;The Code;625
18.7;12-7. Release a COM Component Quickly;627
18.7.1;Problem;627
18.7.2;Solution;627
18.7.3;How It Works;627
18.8;12-8. Use Optional Parameters;628
18.8.1;Problem;628
18.8.2;Solution;628
18.8.3;How It Works;628
18.8.3.1;The Code;628
18.9;12-9. Use an ActiveX Control in a .NET Client;629
18.9.1;Problem;629
18.9.2;Solution;629
18.9.3;How It Works;629
18.10;12-10. Expose a .NET Component Through COM;630
18.10.1;Problem;630
18.10.2;Solution;630
18.10.3;How It Works;630
19;CHAPTER 13 Commonly Used Interfaces and Patterns;632
19.1;13-1. Implement a Custom Serializable Type;633
19.1.1;Problem;633
19.1.2;Solution;633
19.1.3;How It Works;633
19.1.3.1;The Code;636
19.2;13-2. Implement a Cloneable Type;639
19.2.1;Problem;639
19.2.2;Solution;639
19.2.3;How It Works;639
19.2.3.1;The Code;641
19.3;13-3. Implement a Comparable Type;644
19.3.1;Problem;644
19.3.2;Solution;644
19.3.3;How It Works;644
19.3.3.1;The Code;645
19.3.3.2;Usage;647
19.4;13-4. Implement an Enumerable Collection;649
19.4.1;Problem;649
19.4.2;Solution;649
19.4.3;How It Works;649
19.4.3.1;The Code;650
19.5;13-5. Implement an Enumerable Type Using a Custom Iterator;653
19.5.1;Problem;653
19.5.2;Solution;653
19.5.3;How It Works;653
19.5.3.1;The Code;654
19.6;13-6. Implement a Disposable Class;660
19.6.1;Problem;660
19.6.2;Solution;660
19.6.3;How It Works;660
19.6.3.1;The Code;662
19.7;13-7. Implement a Formattable Type;664
19.7.1;Problem;664
19.7.2;Solution;664
19.7.3;How It Works;664
19.7.3.1;The Code;665
19.7.3.2;Usage;668
19.8;13-8. Implement a Custom Exception Class;668
19.8.1;Problem;668
19.8.2;Solution;668
19.8.3;How It Works;668
19.8.3.1;The Code;670
19.9;13-9. Implement a Custom Event Argument;672
19.9.1;Problem;672
19.9.2;Solution;672
19.9.3;How It Works;672
19.9.3.1;The Code;673
19.10;13-10. Implement the Singleton Pattern;674
19.10.1;Problem;674
19.10.2;Solution;674
19.10.3;How It Works;674
19.10.3.1;The Code;675
19.10.3.2;Usage;675
19.11;13-11. Implement the Observer Pattern;676
19.11.1;Problem;676
19.11.2;Solution;676
19.11.3;How It Works;676
19.11.3.1;The Code;676
19.11.3.2;Usage;681
19.12;13-12. Implement a Parallel Producer-Consumer Pattern;682
19.12.1;Problem;682
19.12.2;Solution;682
19.12.3;How It Works;682
19.12.3.1;The Code;683
19.13;13-13. Perform Lazy Object Initialization;684
19.13.1;Problem;684
19.13.2;Solution;684
19.13.3;How It Works;685
19.13.3.1;The Code;685
19.14;13-14. Use Optional Parameters;686
19.14.1;Problem;686
19.14.2;Solution;686
19.14.3;How It Works;686
19.14.3.1;The Code;687
19.15;13-15. Add a Method to a Type Without Modifying It;688
19.15.1;Problem;688
19.15.2;Solution;688
19.15.3;How It Works;688
19.15.3.1;The Code;689
19.16;13-16. Call an Object Member Dynamically;690
19.16.1;Problem;690
19.16.2;Solution;690
19.16.3;How It Works;690
19.16.3.1;The Code;690
19.17;13-17. Create a Variant Generic Type;692
19.17.1;Problem;692
19.17.2;Solution;692
19.17.3;How It Works;692
19.17.3.1;The Code;694
20;CHAPTER 14 Windows Integration;696
20.1;14-1. Access Runtime Environment Information;697
20.1.1;Problem;697
20.1.2;Solution;697
20.1.3;How It Works;697
20.1.3.1;The Code;699
20.2;14-2. Retrieve the Value of an Environment Variable;701
20.2.1;Problem;701
20.2.2;Solution;701
20.2.3;How It Works;701
20.2.3.1;The Code;702
20.3;14-3. Write an Event to the Windows Event Log;703
20.3.1;Problem;703
20.3.2;Solution;703
20.3.3;How It Works;703
20.3.3.1;The Code;704
20.4;14-4. Read and Write to the Windows Registry;705
20.4.1;Problem;705
20.4.2;Solution;705
20.4.3;How It Works;705
20.4.3.1;The Code;706
20.5;14-5. Search the Windows Registry;708
20.5.1;Problem;708
20.5.2;Solution;708
20.5.3;How It Works;708
20.5.3.1;The Code;710
20.5.3.2;Usage;712
20.6;14-6. Create a Windows Service;712
20.6.1;Problem;712
20.6.2;Solution;712
20.6.3;How It Works;712
20.6.3.1;The Code;714
20.6.3.2;Usage;717
20.7;14-7. Create a Windows Service Installer;717
20.7.1;Problem;717
20.7.2;Solution;717
20.7.3;How It Works;717
20.7.3.1;The Code;718
20.7.3.2;Usage;719
20.8;14-8. Create a Shortcut on the Desktop or Start Menu;719
20.8.1;Problem;719
20.8.2;Solution;719
20.8.3;How It Works;720
20.8.3.1;The Code;720
20.9;14-9. Create a Windows 7 Jump List;722
20.9.1;Problem;722
20.9.2;Solution;722
20.9.3;How It Works;722
20.9.3.1;The Code;723
20.10;14-10. Use Windows Search;724
20.10.1;Problem;724
20.10.2;Solution;724
20.10.3;How It Works;725
20.10.3.1;The Code;726
20.11;14-11. Check Internet Connectivity;729
20.11.1;Problem;729
20.11.2;Solution;729
20.11.3;How It Works;729
20.11.3.1;The Code;729
20.12;14-12. Display a Task Dialog;730
20.12.1;Problem;730
20.12.2;Solution;730
20.12.3;How It Works;730
20.12.3.1;The Code;731
20.13;14-13. Write Custom Performance Counters;733
20.13.1;Problem;733
20.13.2;Solution;734
20.13.3;How It Works;734
20.13.3.1;The Code;734
20.14;14-14. Read Performance Counters;737
20.14.1;Problem;737
20.14.2;Solution;737
20.14.3;How It Works;737
20.14.3.1;The Code;738
20.15;14-15. Obtain Elevated Privileges;739
20.15.1;Problem;739
20.15.2;Solution;739
20.15.3;How It Works;739
20.15.3.1;The Code;740
21;CHAPTER 15 Parallel Programming;742
21.1;15-1. Perform Simple Parallel Tasks;742
21.1.1;Problem;742
21.1.2;Solution;742
21.1.3;How It Works;743
21.1.3.1;The Code;743
21.2;15-2. Return a Result from a Task;745
21.2.1;Problem;745
21.2.2;Solution;745
21.2.3;How It Works;745
21.2.3.1;The Code;745
21.3;15-3. Wait for Tasks to Complete;747
21.3.1;Problem;747
21.3.2;Solution;747
21.3.3;How It Works;747
21.3.3.1;The Code;747
21.4;15-4. Parallel Process a Collection;749
21.4.1;Problem;749
21.4.2;Solution;749
21.4.3;How It Works;749
21.4.3.1;The Code;750
21.5;15-5. Chain Tasks Together;751
21.5.1;Problem;751
21.5.2;Solution;751
21.5.3;How It Works;751
21.5.3.1;The Code;751
21.6;15-6. Write a Cooperative Algorithm;752
21.6.1;Problem;752
21.6.2;Solution;752
21.6.3;How It Works;753
21.6.3.1;The Code;753
21.7;15-7. Handle Exceptions in Tasks;754
21.7.1;Problem;754
21.7.2;Solution;755
21.7.3;How It Works;755
21.7.3.1;The Code;755
21.8;15-8. Cancel a Task;756
21.8.1;Problem;756
21.8.2;Solution;756
21.8.3;How It Works;757
21.8.3.1;The Code;757
21.9;15-9. Share Data Between Tasks;758
21.9.1;Problem;758
21.9.2;Solution;758
21.9.3;How It Works;758
21.9.3.1;The Code;759
22;CHAPTER 16 Using LINQ;761
22.1;16-1. Perform a Simple LINQ Query;761
22.1.1;Problem;761
22.1.2;Solution;762
22.1.3;How It Works;762
22.1.3.1;The Code;763
22.2;16-2. Filter Items from a Data Source;767
22.2.1;Problem;767
22.2.2;Solution;767
22.2.3;How It Works;767
22.2.3.1;The Code;767
22.3;16-3. Filter a Data Source by Type;769
22.3.1;Problem;769
22.3.2;Solution;769
22.3.3;How It Works;769
22.3.3.1;The Code;770
22.4;16-4. Filter Ranges of Elements;771
22.4.1;Problem;771
22.4.2;Solution;771
22.4.3;How It Works;771
22.4.3.1;The Code;771
22.5;16-5. Select Multiple Member Values;772
22.5.1;Problem;772
22.5.2;Solution;772
22.5.3;How It Works;773
22.5.3.1;The Code;773
22.6;16-6. Filter and Select from Multiple Data Sources;774
22.6.1;Problem;774
22.6.2;Solution;774
22.6.3;How It Works;774
22.6.3.1;The Code;775
22.7;16-7. Use Permutations of Data Sources;777
22.7.1;Problem;777
22.7.2;Solution;777
22.7.3;How It Works;777
22.7.3.1;The Code;778
22.8;16-8. Concatenate Data Sources;779
22.8.1;Problem;779
22.8.2;Solution;779
22.8.3;How It Works;780
22.8.3.1;The Code;780
22.9;16-9. Group Result Elements by Attribute;781
22.9.1;Problem;781
22.9.2;Solution;781
22.9.3;How It Works;781
22.9.3.1;The Code;782
22.10;16-10. Sort Query Results;786
22.10.1;Problem;786
22.10.2;Solution;786
22.10.3;How It Works;786
22.10.3.1;The Code;786
22.11;16-11. Compare Data Sources;788
22.11.1;Problem;788
22.11.2;The Solution;788
22.11.3;How It Works;788
22.11.3.1;The Code;788
22.12;16-12. Aggregate Data Sources;790
22.12.1;Problem;790
22.12.2;Solution;790
22.12.3;How It Works;790
22.12.3.1;The Code;791
22.13;16-13. Share Values Within a Query;792
22.13.1;Problem;792
22.13.2;Solution;793
22.13.3;How It Works;793
22.13.3.1;The Code;793
22.14;16-14. Create Custom LINQ Extension Methods;794
22.14.1;Problem;794
22.14.2;Solution;794
22.14.3;How It Works;794
22.14.3.1;The Code;795
22.15;16-15. Convert from IEnumerable<>;796
22.15.1;Problem;796
22.15.2;Solution;796
22.15.3;How It Works;796
22.15.3.1;The Code;796
23;CHAPTER 17 Windows Presentation Foundation;801
23.1;17-1. Create and Use a Dependency Property;802
23.1.1;Problem;802
23.1.2;Solution;802
23.1.3;How It Works;803
23.1.3.1;The Code;805
23.2;17-2. Create and Use an Attached Property;807
23.2.1;Problem;807
23.2.2;Solution;808
23.2.3;How It Works;808
23.2.3.1;The Code;808
23.3;17-3. Define Application-Wide Resources;811
23.3.1;Problem;811
23.3.2;Solution;811
23.3.3;How It Works;811
23.3.3.1;The Code;812
23.4;17-4. Debug Data Bindings Using an IValueConverter;813
23.4.1;Problem;813
23.4.2;Solution;813
23.4.3;How It Works;813
23.4.3.1;The Code;813
23.5;17-5. Debug Bindings Using Attached Properties;815
23.5.1;Problem;815
23.5.2;Solution;815
23.5.3;How It Works;816
23.5.3.1;The Code;817
23.6;17-6. Arrange UI Elements in a Horizontal or Vertical Stack;817
23.6.1;Problem;817
23.6.2;Solution;817
23.6.3;How It Works;817
23.6.3.1;The Code;818
23.7;17-7. Dock UI Elements to the Edges of a Form;819
23.7.1;Problem;819
23.7.2;Solution;819
23.7.3;How It Works;820
23.7.3.1;The Code;820
23.8;17-8. Arrange UI Elements in a Grid;821
23.8.1;Problem;821
23.8.2;Solution;821
23.8.3;How It Works;822
23.8.3.1;The Code;822
23.9;17-9. Position UI Elements Using Exact Coordinates;823
23.9.1;Problem;823
23.9.2;Solution;824
23.9.3;How It Works;824
23.9.3.1;The Code;824
23.10;17-10. Get Rich Text Input from a User;825
23.10.1;Problem;825
23.10.2;Solution;825
23.10.3;How It Works;825
23.10.3.1;The Code;827
23.11;17-11. Display a Control Rotated;830
23.11.1;Problem;830
23.11.2;Solution;830
23.11.3;How It Works;830
23.11.3.1;The Code;830
23.12;17-12. Create a User Control;832
23.12.1;Problem;832
23.12.2;Solution;832
23.12.3;How It Works;833
23.12.3.1;The Code;833
23.13;17-13. Support Application Commands in a User Control;834
23.13.1;Problem;834
23.13.2;Solution;835
23.13.3;How It Works;835
23.13.3.1;The Code;835
23.14;17-14. Create a Lookless Custom Control;838
23.14.1;Problem;838
23.14.2;Solution;839
23.14.3;How It Works;839
23.14.3.1;The Code;841
23.15;17-15. Create a Two-Way Binding;845
23.15.1;Problem;845
23.15.2;Solution;845
23.15.3;How It Works;845
23.15.3.1;The Code;847
23.16;17-16. Bind to a Command;848
23.16.1;Problem;848
23.16.2;Solution;848
23.16.3;How It Works;848
23.16.3.1;The Code;849
23.17;17-17. Use Data Templates to Display Bound Data;856
23.17.1;Problem;856
23.17.2;Solution;856
23.17.3;How It Works;856
23.17.3.1;The Code;857
23.18;17-18. Bind to a Collection with the Master-Detail Pattern;860
23.18.1;Problem;860
23.18.2;Solution;860
23.18.3;How It Works;860
23.18.3.1;The Code;861
23.19;17-19. Change a Control’s Appearance on Mouseover;866
23.19.1;Problem;866
23.19.2;Solution;866
23.19.3;How It Works;867
23.19.3.1;The Code;867
23.20;17-20. Change the Appearance of Alternate Items in a List;868
23.20.1;Problem;868
23.20.2;Solution;868
23.20.3;How It Works;868
23.20.3.1;The Code;868
23.21;17-21. Drag Items from a List and Drop Them on a Canvas;870
23.21.1;Problem;870
23.21.2;Solution;870
23.21.3;How It Works;870
23.21.3.1;The Code;871
23.22;17-22. Display the Progress of a Long-Running Operation and Allow the User to Cancel It;874
23.22.1;Problem;874
23.22.2;Solution;874
23.22.3;How It Works;875
23.22.3.1;The Code;875
23.23;17-23. Draw Two-Dimensional Shapes;878
23.23.1;Problem;878
23.23.2;Solution;878
23.23.3;How It Works;879
23.23.3.1;The Code;881
23.24;17-24. Create Reusable Shapes;883
23.24.1;Problem;883
23.24.2;Solution;883
23.24.3;How It Works;883
23.24.3.1;The Code;884
23.25;17-25. Draw or Fill a Shape Using a Solid Color;885
23.25.1;Problem;885
23.25.2;Solution;885
23.25.3;How It Works;885
23.25.3.1;The Code;886
23.26;17-26. Fill a Shape with a Linear or Radial Color Gradient;887
23.26.1;Problem;887
23.26.2;Solution;888
23.26.3;How It Works;888
23.26.3.1;The Code;889
23.27;17-27. Fill a Shape with an Image;891
23.27.1;Problem;891
23.27.2;Solution;891
23.27.3;How It Works;891
23.27.3.1;The Code;893
23.28;17-28. Fill a Shape with a Pattern or Texture;894
23.28.1;Problem;894
23.28.2;Solution;894
23.28.3;How It Works;894
23.28.3.1;The Code;895
23.29;17-29. Animate the Property of a Control;898
23.29.1;Problem;898
23.29.2;Solution;898
23.29.3;How It Works;898
23.29.3.1;The Code;900
23.30;17-30. Animate Several Properties in Parallel;901
23.30.1;Problem;901
23.30.2;Solution;902
23.30.3;How It Works;902
23.30.3.1;The Code;902
23.31;17-31. Create a Keyframe-Based Animation;904
23.31.1;Problem;904
23.31.2;Solution;904
23.31.3;How It Works;904
23.31.3.1;The Code;905
23.32;17-32. Animate an Object Along a Path;906
23.32.1;Problem;906
23.32.2;Solution;906
23.32.3;How It Works;907
23.32.3.1;The Code;908
23.33;17-33. Play a Media File;910
23.33.1;Problem;910
23.33.2;Solution;910
23.33.3;How It Works;910
23.33.3.1;The Code;910
23.34;17-34. Query Keyboard State;914
23.34.1;Problem;914
23.34.2;Solution;914
23.34.3;How It Works;915
23.34.3.1;The Code;915
24;Index;917


"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."



Ihre Fragen, Wünsche oder Anmerkungen
Vorname*
Nachname*
Ihre E-Mail-Adresse*
Kundennr.
Ihre Nachricht*
Lediglich mit * gekennzeichnete Felder sind Pflichtfelder.
Wenn Sie die im Kontaktformular eingegebenen Daten durch Klick auf den nachfolgenden Button übersenden, erklären Sie sich damit einverstanden, dass wir Ihr Angaben für die Beantwortung Ihrer Anfrage verwenden. Selbstverständlich werden Ihre Daten vertraulich behandelt und nicht an Dritte weitergegeben. Sie können der Verwendung Ihrer Daten jederzeit widersprechen. Das Datenhandling bei Sack Fachmedien erklären wir Ihnen in unserer Datenschutzerklärung.