Monthly Archives: May 2015

List the unique words in a Microsoft Word file in C#

The example List unique words in a text file in C# shows how to list the unique the words in a text file. This example shows how to list the unique words in a Microsoft Word file. Before you start, … Continue reading

Posted in files, Office, strings, Word | Tagged , , , , , , , , , , , , | Leave a comment

Rotate around a point other than the origin in C#

You can use a Matrix object to rotate, scale, translate, or skew a drawing. Its RotateAt method adds a rotation around a specific point to a Matrix. The following RotateAroundPoint method returns a new Matrix that represents rotation around a … Continue reading

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

Make an infinite series of menus in C#

The example Create menu items at run time with images, shortcut keys, and event handlers in C# explains how to create menu items at run time. This example uses that technique to add new menu items to any submenu as … Continue reading

Posted in menus, user interface | Tagged , , , , , , , , , | Leave a comment

Make generic Min and Max methods in C#

This example shows how you can make generic Min and Max methods to find the minimum and maximum values in a sequence of parameters. The Math namespace’s Min and Max methods are very useful, but they have two big drawbacks. … Continue reading

Posted in algorithms, arrays, calculations, generic, methods | Tagged , , , , , , , , , , , , , , | Leave a comment

Create menu items at run time with images, shortcut keys, and event handlers in C#

This example shows how you can create menu items at run time. The following code adds new menu items to the Tools menu when the program starts. // Create some tool menu items. private void Form1_Load(object sender, EventArgs e) { … Continue reading

Posted in menus | Tagged , , , , , , , , , , , | 1 Comment

Let the user draw rubber band ellipses (or other shapes) in C#

The following code shows how the program lets the user draw rubber band ellipses. // The ellipses to draw. private List<Rectangle> Ellipses = new List<Rectangle>(); // The points for the new ellipse we are drawing. private Point StartPoint, EndPoint; private … Continue reading

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

Make Windows shutdown, reboot, log off, lock, hibernate, and sleep in C#

This example shows how a C# program can make Windows shutdown, reboot, lock, log off, or hibernate. Some of those operations require you to use methods defined in external libraries, so the code uses DllImport attributes to define them. DllImport … Continue reading

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

Initialize arrays with ranges or repeated values in C#

This example uses the Enumerable class’s shared methods to initialize arrays to ranges or repeated values. The methods return IEnumerable results. You can then use the result’s ToArray and ToList methods to convert the IEnumerable into an array or list. … Continue reading

Posted in arrays, variables | Leave a comment

Print a grid of values with alternating row colors in C#

The following code shows how the program prints its grid. The complete code isn’t too complicated but it’s long so here it’s broken into several methods. // Draw the grid. private void pdocGrid_PrintPage(object sender, PrintPageEventArgs e) { // Make some … Continue reading

Posted in graphics, printing | Tagged , , , , , , , , , , | 1 Comment

Make a countdown timer in C#

When the program starts, it sets the event name and time. It then enables the Timer control. // Initialize information about the event. private const string EventName = “End of the World”; private DateTime EventDate = DateTime.Now + new TimeSpan(1, … Continue reading

Posted in miscellany | Tagged , , , , , , , , | 1 Comment