O'Reilly logo

Windows Forms Programming in C# by Chris Sells

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Strings

Of course, deciding on a font is only half the fun. The real action is drawing strings after the font's been picked. For that, you use the DrawString method of the Graphics object:

using( Font font = new Font("Arial", 12) ) {
  // This will wrap at new line characters
  g.DrawString("line 1\nline 2", font, Brushes.Black, 10, 10);
}

The DrawString method takes, at a minimum, a string, a font, a brush to fill in the font characters, and a point. DrawString starts the drawing at the point and keeps going until it hits the edges of the region in the Graphics object. This includes translating new line characters as appropriate but does not include wrapping at word boundaries. To get the wrapping, you specify a layout rectangle:

 using( Font ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required