Tag Archives: C#

Search files for target text in C#

The .NET tools available in the System.IO namespace and normal string methods make it easy to search files for target text. When you click the Search button, the following code displays the lines in a file that contain a particular … Continue reading

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

Write Console window output to multiple places in C#

This example captures output sent to the Console window and displays it in multiple controls plus the Console window. It is based on an excellent reply by user Servy on Stack Overflow. The following class is a TextWriter that represents … Continue reading

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

Redirect Console window output to a TextBox in C#

This example captures output sent to the Console window and displays it in a TextBox control. It is based on an excellent reply by user Servy on Stack Overflow. The Console object has a SetOut method that lets you change … Continue reading

Posted in console | Tagged , , , , , , , , | 1 Comment

Get a weather forecast from openweathermap.org in C#

This example displays a weather forecast from openweathermap.org. To get a forecast or other data, you build a URL describing your request and navigate to it. You then parse the returned XML or JSON data to see what openweathermap.org has … Continue reading

Posted in internet, web, XML | Tagged , , , , , , , , , , , | Leave a comment

Draw rose curves in C#

A rose curve is a curve generated by the polar coordinate equation r = cos(n / d * θ). Here n and d should be relatively prime, so the fraction n / d cannot be simplified. When you click the … Continue reading

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

Find two sets where the median of one is the mean of the other and vice versa in C#

For an upcoming book, I wanted to find sets where the mean (average) of one is the median (middle value) of the other and vice versa. I wanted to avoid trivial examples such as {1, 1, 1} and where the … Continue reading

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

Trim images in C#

This example shows how you can trim images to remove unnecessary whitespace around their edges. Sometimes when I make pictures for a book, it’s hard to tell where the edges of the picture begin, so I risk either including more … Continue reading

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

Find Mersenne primes in C#

Mersenne primes are prime numbers of the from 2n – 1 for some integer n. For example, 22 – 1 = 4 – 1 = 3 and 3 is prime, so 3 is a Mersenne prime. This example uses a … Continue reading

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

Display a progress bar with text in WPF and C#

As is so often the case in WPF, the solution is simple after you spend a few hours figuring out what controls to use. This example uses a normal Grid control that holds a StackPanel. The following code shows the … Continue reading

Posted in controls, user interface, wpf, XAML | Tagged , , , , , , , , , , , , , | 1 Comment

Convert between ragged arrays and two-dimensional arrays

Ragged arrays are arrays that hold other arrays, as opposed to two-dimensional arrays. They are called “ragged arrays” because they work like two-dimensional arrays (or higher-dimensional arrays) where each row in the array can hold a different number of elements. … Continue reading

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