Category Archives: extension methods

Initialize a TreeView control to display a directory hierarchy in C#

This example adds the following LoadFromDirectory extension method to the TreeView control. // Initialize the TreeView from a directory, // its subdirectories, and their files. public static void LoadFromDirectory(this TreeView trv, string directory, int folder_img, int file_img) { DirectoryInfo dir_info … Continue reading

Posted in controls, directories, extension methods, files, user interface | Tagged , , , , , , , , , , , , , | Leave a comment

Make subarray extension methods in C#

The Array class’s Copy method can copy a subarray from one array to another, but it requires that you figure out how many items you want to copy. It will also copy parts of a two-dimensional array, but it only … Continue reading

Posted in arrays, extension methods | 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

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

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

Use an improved extension method to calculate standard deviation in C#

The example Calculate standard deviation in C# makes an extension method that calculates standard deviation for an IEnumerable of integers. Unfortunately because that example works with integers, it cannot work with other data types. Rian Meier showed me this method, … Continue reading

Posted in algorithms, extension methods, mathematics | Tagged , , , , , , , , , , | Leave a comment

Remove non-printable ASCII characters from a string in C#

The following TrimNonAscii extension method removes the non-printable ASCII characters from a string. public static string TrimNonAscii(this string value) { string pattern = “[^ -~]+”; Regex reg_exp = new Regex(pattern); return reg_exp.Replace(value, “”); } In ASCII, the printable characters lie … Continue reading

Posted in extension methods, strings | Tagged , , , , , , , , , , , , | 7 Comments

Sort ListView controls by all columns or clicked columns in C#

The ListView control provides features to sort its values but by default it doesn’t use them. This example shows how you can add extension methods to the control to make it sort on all columns or on clicked columns. This … Continue reading

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

Add “extension properties” to classes in C#

Extension methods allow you to add new methods to existing classes even if you don’t have access to the classes’ source code. For example, the Randomize two-dimensional arrays in C# example shows how to add an extension method to two-dimensional … Continue reading

Posted in algorithms, classes, extension methods | Tagged , , , , , , , , , | 1 Comment