Monthly Archives: February 2015

Make a bouncing ball animation in C#

This example shows how to make a bounding ball animation. When the form loads, the following code executes. // Some drawing parameters. private const int BallWidth = 50; private const int BallHeight = 50; private int BallX, BallY; // Position. … Continue reading

Posted in animation, audio, multimedia | Tagged , , , , , , , , , , , , | 4 Comments

Make a WPF line editor C#

This example is a “simple” WPF line editor that lets you add, move, and delete Line objects. The example Draw, move, and delete line segments in C# is a Windows Forms application that does something. The WPF version is a … Continue reading

Posted in algorithms, drawing, graphics, wpf | Tagged , , , , , , , , , , , , , , , , , , , | 18 Comments

Measure character positions when drawing long strings in C#

The example Measure character positions in a drawn string in C# uses the Graphics class’s MeasureCharacterRanges method to find the positions where characters in a string will be drawn. Unfortunately this method requires you to call a StringFormat object’s SetMeasurableCharacterRanges … Continue reading

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

Play tones with specific frequencies in C#

This example shows how you can play tones with a given frequency and duration. When the user clicks the Play button, the following code plays a tone. private void btnPlay_Click(object sender, EventArgs e) { int freq = int.Parse(txtFrequency.Text); int duration … Continue reading

Posted in audio, multimedia | Tagged , , , , , , , , , , , , | Leave a comment

Drag and drop images while displaying a preview in C#

The example Drag and drop images in C# explains how to drag and drop images. See that example for the basic ideas. (Pay special attention to the second paragraph, which discusses the AllowDrop property for the PictureBox control.) This example … Continue reading

Posted in Drag and Drop, graphics, image processing, interoperability | Tagged , , , , , , , , , , , , , , , , , , | Leave a comment

Drag and drop text with feedback in C#

These examples explain the basics of drag and drop: Drag and drop text in C# Drag and drop images in C# Those examples use these methods and events: The DoDragDrop method to start a drag The DragEnter event to tell … Continue reading

Posted in Drag and Drop, interoperability | Tagged , , , , , , , , , , , , , , , , | Leave a comment

Drag and drop images in C#

This example shows how to drag and drop images in C#. The example Drag and drop text in C# explains basic drag and drop operations. See that example for the fundamentals. One of the first things you must do before … Continue reading

Posted in Drag and Drop, graphics, image processing, interoperability | Tagged , , , , , , , , , , , , , , | 6 Comments

Drag and drop text in C#

This example shows how to let a program drag and drop text both between its controls and with drag and drop enabled controls in other applications. First, to receive a drop, you must set a control’s AllowDrop property to true. … Continue reading

Posted in Drag and Drop, interoperability | Tagged , , , , , , , , , , | 2 Comments

Add a ToString extension method to the TimeSpan structure in C#

Until .NET Framework version 4.0, you could not pass the TimeSpan structure’s ToString method a format string, so the result was always in the form hh:mm:ss.fffffff where hh is hours, mm is minutes, ss is seconds, and fffffff is 7 … Continue reading

Posted in extension methods, parsing | Tagged , , , , , , , , , , | 2 Comments

Find elapsed time in years, months, days, hours, minutes, and seconds in C#

The TimeSpan structure represents an elapsed time. It has Days, Hours, Minutes, Seconds, and Milliseconds properties that tell you about the time that has passed. For example, if two dates are 3 days and 4 hours apart, then Days = … Continue reading

Posted in parsing, variables | Tagged , , , , , , , , , , , , , , , , , | Leave a comment