Author Archives: Rod Stephens

Generate random polygons in C#

I needed a method for generating random polygons for a graphics program and the following code shows the result. // Make random polygons inside the bounding rectangle. private static Random rand = new Random(); public static PointF[] MakeRandomPolygon( int num_vertices, … Continue reading

Posted in algorithms, geometry, graphics | Tagged , , , , , , , , , | Leave a comment

Make extension methods to generate random double values in C#

This example adds extension methods to the Random class to generate double values within a specified range. The Random class provides methods for generating pseudo-random numbers. The Next method has three overloaded versions that produce a non-negative integer, an integer … Continue reading

Posted in extension methods, mathematics | Tagged , , , , , , , , , , | 1 Comment

Iterate over the items in arrays and lists in C#

You can use a foreach loop to iterate over the items in arrays, lists, collections, and other objects that have a GetEnumerator method. These loops are often more convenient than for or while loops because you don’t need to worry … Continue reading

Posted in arrays, lists, syntax | Tagged , , , , , , , , , , , , | Leave a comment

Print a ListView control’s contents on multiple pages in C#

This example continues the following two examples: Print a ListView with large contents in C# Print the contents of a ListView control in C# Read those examples for an overview of the basic procedure. This example prints the contents of … Continue reading

Posted in controls, extension methods, printing | Tagged , , , , , , , , , , , , , | 4 Comments

Print a ListView with large contents in C#

The example Print the contents of a ListView control in C# explains how to print the data in a ListView control when its View property is set to Details. One difficulty when printing this kind of data is that a … Continue reading

Posted in controls, drawing, graphics, printing | Tagged , , , , , , , , , , , , , , | Leave a comment

Print the contents of a ListView control in C#

The ListView control, like most controls, includes no support for printing. If you want to display a ListView control’s contents on a print out, you need to do all of the printing yourself. Unfortunately printing in C# is extremely flexible … Continue reading

Posted in controls, drawing, graphics, printing | Tagged , , , , , , , , , , , , | 24 Comments

Make rectangle extension methods in C#

This example shows how to add rectangle extension methods to the Rectangle and RectangleF structs. The Rectangle and RectangleF structs have several useful properties for determining their geometry such as Left, Right, Top, and Bottom, but they have no midpoint … Continue reading

Posted in drawing, extension methods, graphics | Tagged , , , , , , , , , , , , , , | Leave a comment

Use string extension methods to validate Social Security numbers in C#

This example defines three extension methods that return true if strings contain Social Security numbers. Recall that you must add extension methods to a static class and that the methods must be static. This example begins by defining a Matches … Continue reading

Posted in extension methods, extensions, regular expressions, strings | Tagged , , , , , , , , , | 2 Comments

Understand the @ symbol in C#

The @ symbol can be pretty confusing for some C# developers. I’ve even known a few who have been programming for quite a while but who still have incorrect assumptions about what @ does. In your code (and only in … Continue reading

Posted in strings, syntax | Tagged , , , , , , , , , , | 1 Comment

Center a form on the screen at run time in C#

You can center a form at design time by setting its StartPosition property to CenterScreen. Note that you cannot do this in the form’s Load event handler because the form’s position is already set when that event handler executes. You … Continue reading

Posted in forms | Tagged , , , , , , , | Leave a comment