Tag Archives: C# programming

Get font size in pixels in C#

One oddity of the Font class is that its Size property returns font size in the units that were used to create the font. The Font class’s Unit property tells you the units used to create the font, but if … Continue reading

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

Draw smooth text in a GraphicsPath in C#

You can set a Graphics object’s TextRenderingHint property to AntiAliasGridFit to draw smooth text. However, if you place text in a GraphicsPath object and draw the GraphicsPath, then the TextRenderingHint property doesn’t give you smooth text. Instead you need to … Continue reading

Posted in drawing, fonts, graphics | Tagged , , , , , , , , , , , , , | 1 Comment

Create a class outside of any namespace statement in C#

When you create a class in C#, Visual Studio automatically puts it inside a namespace statement. If your project’s name is howto_remove_namespace, then by default that namespace is howto_remove_namespace. But did you know that you can create classes outside of … Continue reading

Posted in classes, syntax | Tagged , , , , , , , | 2 Comments

Book Review: The Manga Guide to Linear Algebra

(Occasionally I post reviews of book that I think may interest programmers, in this case admittedly only those with a mathematical inclination.) The Manga Guide to Linear Algebra   $24.95, 264 pages, 2012 By Shin Takahashi No Starch Press ISBN … Continue reading

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

Get the screen’s resolution in C#

Normally a monitor displays 96 logical (or “notional”) dots per inch (dpi) but it’s not necessarily a good idea to assume that value. When this example starts, the following code displays the logical resolution of the screen in dpi. private … Continue reading

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

Make Ctrl+A select all of the text in a TextBox in C#

Often it’s convenient for the user to be able to press Ctrl+A to select all of the text in the TextBox that has the focus. Strangely that’s not the default behavior for the TextBox. Perhaps the TextBox doesn’t handle Ctrl+A … Continue reading

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

Generate random polygons in C#

I needed a method for generating random polygons for a graphics program and the following code shows the result. // Make random polygons inside the bounding rectangle. private static Random rand = new Random(); public static PointF[] MakeRandomPolygon( int num_vertices, … Continue reading

Posted in algorithms, geometry, graphics | Tagged , , , , , , , , , | Leave a comment

Make extension methods to generate random double values in C#

This example adds extension methods to the Random class to generate double values within a specified range. The Random class provides methods for generating pseudo-random numbers. The Next method has three overloaded versions that produce a non-negative integer, an integer … Continue reading

Posted in extension methods, mathematics | Tagged , , , , , , , , , , | 1 Comment

Iterate over the items in arrays and lists in C#

You can use a foreach loop to iterate over the items in arrays, lists, collections, and other objects that have a GetEnumerator method. These loops are often more convenient than for or while loops because you don’t need to worry … Continue reading

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

Print a ListView control’s contents on multiple pages in C#

This example continues the following two examples: Print a ListView with large contents in C# Print the contents of a ListView control in C# Read those examples for an overview of the basic procedure. This example prints the contents of … Continue reading

Posted in controls, extension methods, printing | Tagged , , , , , , , , , , , , , | 4 Comments