Tag Archives: C#

Draw a scrolling family tree in C#

The example Draw a family tree in C# shows how to draw a family tree. Unfortunately if the tree is too big, you can’t make the form big enough to display it all. One solution would be to use smaller … Continue reading

Posted in algorithms, classes, generic, graphics, OOP | 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 , , , , , , , , , , , , | 5 Comments

See what processes have a file locked in C#

In older versions of Windows, it was impossible to determine what processes had a file locked. The system kept track of the number of locks on a file and processes were responsible for incrementing and decrementing the count to represent … Continue reading

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

Use “banker’s rounding” and “normal rounding” in C#

By default the Math.Round method uses “banker’s rounding.” In banker’s rounding a number with a final digit of 5 is rounded to the nearest even number rather than to the next larger number as you might expect. The idea is … Continue reading

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

See if a file is locked in C#

Some applications lock files so you cannot write, read, delete, or otherwise mess with them. For example, when you open a file in Microsoft Word, it locks the file so you cannot delete it or open it for writing with … Continue reading

Posted in files, system | Tagged , , , , , , , , , , , , , , | 3 Comments

Map between host names and IP addresses in C#

When you enter a host name and click Go, the program uses the following code to look up the host and display the IP addresses associated with it. using System.Net; … // Display the entered host’s IP address. private void … Continue reading

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

List available culture codes in C#

This example shows how you can make a list of the culture codes that are available on the system. Several C# Helper examples deal with globalization including Localize a program in C#. To localize a program, you need to specify … Continue reading

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

Make a cannon game in C#

This simple cannon game randomly positions a target house. Then when you click the Shoot button, it gets angle and speed information for the cannon and enables a timer. To keep the graphics and timing simple, the program uses a … Continue reading

Posted in algorithms, animation, games, graphics | Tagged , , , , , , , , , , , , , | Leave a comment

Convert a bitmap into a cursor in C#

Converting a bitmap into a cursor isn’t easy, but you can do it by using the CreateIconIndirect API function. This example uses the following BitmapToCursor method to create a cursor from a bitmap. // Create a cursor from a bitmap. … Continue reading

Posted in graphics, image processing | Tagged , , , , , , , , , , , , , | Leave a comment

Use a bitmap for an icon in C#

You can’t set a form’s Icon property equal to a bitmap. Fortunately it’s easy enough to create an icon from a bitmap. This example uses the following code to make a form’s icon display the image in a bitmap. // … Continue reading

Posted in forms, graphics, image processing | Tagged , , , , , , , , , , | Leave a comment