Files
docs-desktop/dotnet-desktop-guide/framework/winforms/advanced/how-to-draw-a-custom-dashed-line.md
T
Andy De George c0ba284473 Initial winforms content migrated (#18)
* Merge winforms framework content to working branch (#14)

* Breadcrumb / TOC / Move net5 to net folder (#15)

* change path from net5 to net

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Add .net 5 winforms placeholder article (#16)

* added some metadata and adjusted net5 placeholder

* corrections

* corrections

* corrections

* Test1

* Swapping landing page vs concept

* fix links

* Fix links

* Fix desc
2020-09-01 16:26:21 -07:00

1.9 KiB

title, ms.date, dev_langs, helpviewer_keywords, ms.assetid
title ms.date dev_langs helpviewer_keywords ms.assetid
How to: Draw a Custom Dashed Line 03/30/2017
csharp
vb
lines [Windows Forms], custom
lines [Windows Forms], drawing
lines [Windows Forms], dashed
cd0ed96a-cce4-47b9-b58a-3bae2e3d1bee

How to: Draw a Custom Dashed Line

GDI+ provides several dash styles that are listed in the xref:System.Drawing.Drawing2D.DashStyle enumeration. If those standard dash styles do not suit your needs, you can create a custom dash pattern.

Example

To draw a custom dashed line, put the lengths of the dashes and spaces in an array and assign the array as the value of the xref:System.Drawing.Pen.DashPattern%2A property of a xref:System.Drawing.Pen object. The following example draws a custom dashed line based on the array {5, 2, 15, 4}. If you multiply the elements of the array by the pen width of 5, you get {25, 10, 75, 20}. The displayed dashes alternate in length between 25 and 75, and the spaces alternate in length between 10 and 20.

The following illustration shows the resulting dashed line. Note that the final dash has to be shorter than 25 units so that the line can end at (405, 5).

Illustration that shows a dashed line.

[!code-csharpSystem.Drawing.UsingAPen#51] [!code-vbSystem.Drawing.UsingAPen#51]

Compiling the Code

Create a Windows Form and handle the form's xref:System.Windows.Forms.Control.Paint event. Paste the preceding code into the xref:System.Windows.Forms.Control.Paint event handler.

See also