Monthly Archives: May 2012

Highlight the DataGridView row that is under the mouse in C#

Someone recently asked me how to change the style of the row under the mouse in a DataGridView. This example does that. When the program starts, the following code prepares the DataGridView for use. // The style to use when … Continue reading

Posted in controls | Leave a comment

Use accelerators on labels and buttons in C#

To place an accelerator on a menu item, you put an ampersand in front of the letter in the menu item’s caption that you want to be the accelerator. At run time, the caption is displayed with the accelerator letter … Continue reading

Posted in miscellany, productivity | Leave a comment

Compare the speeds of x++, x += 3, x = x + 3, and other statements in C#

This is another performance testing post (the last for a while, I promise). Assembly language usually has statements comparable to x++, x–, x +=, and other operators that add or subtract to a value and store the result in the … Continue reading

Posted in performance, syntax | Leave a comment

Make a simple class to log events in C#

Usually the debugger lets you examine how a program works quite effectively but sometimes it’s useful to be able to record an event for later study. For example, when you’re working with mouse events, stopping execution at a break point … Continue reading

Posted in classes, debugging, files | Leave a comment

Make a shaded ellipse control in C#

(Note: Before you can use the ShadedEllipse control in the example program, you must build the solution. So after you download the example, build it before you try to view the form.) This example walks through building a control that … Continue reading

Posted in controls, graphics | Leave a comment

Quickly insert standard menu items on a form in C#

Many applications need standard File, Edit, Tools, and Help menus. Visual Studio provides a simple way to build standard menu items. You can then edit the menu to add new items and remove items that you don’t need. Visual Studio … Continue reading

Posted in menus | Leave a comment

Draw a phi spiral (or golden spiral) in C#

The example Draw a nested series of golden rectangles in C# draws nested rectangles and connects their corners to make a square “spiral.” This example makes a smooth spiral. If you check the Circular Spiral box, the program approximates the … Continue reading

Posted in algorithms, graphics, mathematics | 3 Comments

Give a class default indexer properties in C#

An indexer property is a property that other code can use inside square brackets to access some value provided by the class, much as you can use the index of an array. Unlike an array, a class’s indexer need not … Continue reading

Posted in classes | Leave a comment

Activate another running program in C#

This is actually quite simple. First add a reference to the Microsoft.VisualBasic library. Then call Microsoft.VisualBasic.Interaction.AppActivate passing it the title of the program you want to activate as in the following code. // Activate the program. private void btnActivate_Click(object sender, … Continue reading

Posted in miscellany, system | Leave a comment

Use a message filter to make TextBox controls not display context menus in C#

The example Make a TextBox control without a context menu in C# shows how to make a class that inherits from TextBox but that doesn’t display a context menu. That works but is a bit heavy handed, requiring you to … Continue reading

Posted in controls | Leave a comment