Tag Archives: extensions

Make a TextBox preview extender provider in C#

This example shows how to convert the TextBox preview techniques described in the last few posts into an extender provider. The previous examples showed the basic technique, but they were hard-wired to specific TextBox controls so they weren’t very flexible. … Continue reading

Posted in controls, extensions, user interface | 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 , , , , , , , , , | 5 Comments

Add an extension method to the Graphics class in C#

For some strange reason, the Graphics class’s DrawRectangle method cannot take a RectangleF as a parameter. It can take a Rectangle or four float values, and the FillRectangle method can take a RectangleF as a parameter, but the DrawRectangle method … Continue reading

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

Make string extensions to URL encode and decode strings in C#

When you URL encode a string, you convert special characters into numeric sequences thath are safe to use in a URL. When you URL decode a string, you reverse the process. The following code creates extension methods for the string … Continue reading

Posted in extension methods, extensions, strings | Tagged , , , , , , , , , , | Leave a comment

Convert between byte arrays and hexadecimal strings in C#

This example uses extension methods to convert between byte arrays and hexadecimal strings. It adds a ToHex extension method to the byte[] type and a ToBytes extension method to the string class. To create an extension method, create a public … Continue reading

Posted in algorithms, cryptography, extensions, strings | Tagged , , , , , , , , , , , , , , , | 2 Comments