Monthly Archives: October 2017

Make a button display the picture beneath it in C#

This example makes a button display copies of the picture on which it sits, making it appear that the button is part of the underlying image. The key to this example is the following SetButtonBackground method. // Make the Button … Continue reading

Posted in controls, graphics, image processing | Tagged , , , , , , , , , , , , | 1 Comment

Validate optional parameters in C#

The example Use named and optional parameters in C# explains how to use named and optional parameters to let the calling code omit any combination of parameters. In many applications, you may not want to allow the user to omit … Continue reading

Posted in methods, syntax, variables | Tagged , , , , , , , , , , , , , | 1 Comment

Use named and optional parameters in C#

Optional parameters is one of the few additions in recent versions of Visual Studio that are really useful. I usually try to post examples in an older version of C# because it’s easier for someone to move an older example … Continue reading

Posted in methods, syntax, variables | Tagged , , , , , , , , , , , , , | 1 Comment

Display system metric values with descriptions in C#

The example Get system metrics in C# shows how to get system metrics, but the the metrics’ terse names make the results hard to understand. This example displays descriptions of the metrics in addition to their values. Some of the … Continue reading

Posted in API, system | Tagged , , , , , , , , , , , , , , | Leave a comment

Get system metrics in C#

The GetSystemMetrics API function returns system metrics, values that give dimensions used by the system such as the default size of icons and the thickness of a resizable window’s borders. For example, the highlighted line in the picture on the … Continue reading

Posted in API, system | Tagged , , , , , , , , , , , , | 1 Comment

Convert an array of bytes into an integer and back in C#

The BitConverter class provides static methods that let you convert various data types to and from arrays of bytes. (I suppose it might have made more sense to call it the ByteConverter class.) You can use those methods to convert … Continue reading

Posted in classes | 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

Use Array.Copy to copy values in two-dimensional arrays in C#

The Array.Copy method can copy values from one array to another. For one-dimensional arrays, this is reasonably straightforward. You specify the source and destination arrays, the indices where copying should start in the two arrays, and the number of items … Continue reading

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

Determine whether a year is a leap year in C#

Detecting leap years is basically trivial because the DateTime data type provides an IsLeapYear method to do just this. The example uses the following code to display the leap years between the two entered dates. // List leap years between … Continue reading

Posted in miscellany, variables | Tagged , , , , , , , , , , | Leave a comment

Get a hard drive serial number in C#

The example Get a disk volume serial number in C# shows how you can find the serial number for a disk volume such as C:\. If you reformat or repartition the drive, however, the volume serial number may change. This … Continue reading

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