Microsoft KB Archive/309480

= How to create similar functionality to the Line and Shape controls in Windows Forms by using Visual Basic .NET or Visual Basic 2005 =

Article ID: 309480

Article Last Modified on 5/11/2007

-

APPLIES TO


 * Microsoft Visual Basic 2005
 * Microsoft Visual Basic .NET 2003 Standard Edition
 * Microsoft Visual Basic .NET 2002 Standard Edition

-



This article was previously published under Q309480





For a Microsoft Visual Basic 6.0 version of this article, see 142172.

IN THIS TASK
SUMMARY
 * Create the Project and Add the Code



SUMMARY
This article describes how to draw lines and shapes in Microsoft Visual Basic .NET or in Microsoft Visual Basic 2005 by using the included sample code. The functionality that is demonstrated is similar to that which is provided by the Line and Shape controls that are available in Microsoft Visual Basic 6.0. The Line and Shape controls are not included in Visual Basic .NET or in Visual Basic 2005; however, the System.Drawing namespace is available and this namespace provides more functionality, versatility, and control over lines and shapes.

back to the top

Create the Project and Add the Code
 Start Visual Studio .NET or Visual Studio 2005. Create a new Windows Application in Visual Basic .NET or in Visual Basic 2005. Add four buttons and one label to Form1 (the location of the buttons and the label is not important). Double-click Form1 to open the code window.  Use the IMPORTS statement on the namespaces so that you are not required to qualify declarations in those namespaces later in your code.

Add the following code to General Declarations section in Form1 (which is the section above all other code in the window): Imports System.ComponentModel Imports System.Drawing   Paste the following code after the code that is generated by Windows Form Designer:NOTE: Please see the comments in the code for a brief explanation of the functionality. 'Declare the point variable to use in the Form1 Mouseup event. Dim p As Point Dim p2 As Point Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click 'Call the Sub to draw lines. DrawLines End Sub Public Sub DrawLines 'Create a graphics object. Dim myGraphics As System.Drawing.Graphics

'Set the graphics object of Form1 to the graphics object created to use Form1 to do our drawing. myGraphics = Me.CreateGraphics

'Create the Pen object. A Pen object draws a line of specified width and style. 'The line drawn by a Pen object can be filled in a variety of fill styles, 'including solid colors and textures. The fill style depends on the brush or texture 'that is used as the fill object. Dim BluePen As New Pen(Color.Blue, 5)

'Specify a cap style that the Pen object will use at the end of a line. BluePen.StartCap = Drawing.Drawing2D.LineCap.ArrowAnchor

'This code block demonstrates how to draw straight lines of different varieties. 'Use the DashStyle property of the Pen object to draw several varieties of dashed lines. Dim x1, y1, x2, y2 As Single x1 = 166.0F x2 = 424.0F BluePen.DashStyle = Drawing.Drawing2D.DashStyle.Solid 'This draws a line to the screen. myGraphics.DrawLine(BluePen, x1, 170, x2, 170) BluePen.DashStyle = Drawing.Drawing2D.DashStyle.Dash 'This draws a Dash line to the screen. myGraphics.DrawLine(BluePen, x1, 190, x2, 190) BluePen.DashStyle = Drawing.Drawing2D.DashStyle.DashDot 'This draws a Dash Dot line to the screen. myGraphics.DrawLine(BluePen, x1, 210, x2, 210) BluePen.DashStyle = Drawing.Drawing2D.DashStyle.DashDotDot 'This draws a Dash Dot Dot line to the screen. myGraphics.DrawLine(BluePen, x1, 230, x2, 230) BluePen.DashStyle = Drawing.Drawing2D.DashStyle.Dot 'This draws a Dotted line to the screen. myGraphics.DrawLine(BluePen, x1, 250, x2, 250)

End Sub

Private Sub Form1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseUp 'Generate a line on Form1 based on the user clicking two areas of Form1. If p.X <= 0 Then p.X = e.X 'This is the X coordinate of the mouse on Form1 and is set to the variable p that is type point. p.Y = e.Y 'This is the y coordinate of the mouse on Form1 and is set to the variable p that is type point. Else p2.X = e.X 'This is the X coordinate of the mouse on Form1 and is set to the variable p that is type point. p2.Y = e.Y 'This is the y coordinate of the mouse on Form1 and is set to the variable p that is type point.

'Create a graphics object. Dim myGraphics As System.Drawing.Graphics

'Set the graphics object of Form1 to the graphics object created to use Form1 to do our drawing. myGraphics = Me.CreateGraphics

'Create the pen object. A Pen object draws a line of specified width and style. 'The line drawn by a Pen object can be filled in a variety of fill styles, 'including solid colors and textures. The fill style depends on the brush or texture 'that is used as the fill object. Dim BluePen As New Pen(Color.Blue, 5) 'Draw the line. myGraphics.DrawLine(BluePen, p.X, p.Y, p2.X, p2.Y)

p.X = 0 p.Y = 0 End If

End Sub Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click DrawShapes End Sub Private Sub DrawShapes 'Create a graphics object. Dim myGraphics As System.Drawing.Graphics

'Set the graphics object of Form1 to the graphics object created to use Form1 to do the drawing. myGraphics = Me.CreateGraphics

'Create the pen object. A Pen object draws a line of specified width and style. 'The line drawn by a Pen object can be filled in a variety of fill styles, 'including solid colors and textures. The fill style depends on the brush or texture 'that is used as the fill object. Dim BluePen As New Pen(Color.Blue, 5)

'The following code block draws shapes. myGraphics.DrawArc(BluePen, 200, 20, 100, 200, 45, 270) myGraphics.DrawBezier(BluePen, 100, 100, 200, 10, 350, 50, 500, 100) myGraphics.DrawRectangle(BluePen, 175, 200, 195, 200)

'Create points that define the polygon. Dim myPointArray As Point = _ {New Point(300, 123), New Point(472, 41), New Point(472, 156)} 'Draw the polygon. myGraphics.DrawPolygon(BluePen, myPointArray)

'Create a new Font object and set it equal to a FontStyle. Dim font As New Font(&quot;Times New Roman&quot;, 80, FontStyle.Regular, GraphicsUnit.Pixel, 1)

'Create a new Brush object and set its value. Dim mybrush As Brush = Brushes.YellowGreen

'Use the following code to draw text to the screen. myGraphics.DrawString(&quot;.NET&quot;, font, mybrush, 160, 200)

End Sub

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click Me.Refresh'This clears the form End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 'This code formats the controls and Form1. 'Button1 '       Me.Button1.Location = New System.Drawing.Point(0, 0) Me.Button1.Name = &quot;Button1&quot; Me.Button1.Size = New System.Drawing.Size(88, 32) Me.Button1.TabIndex = 0 Me.Button1.Text = &quot;Draw Lines&quot; '       'Label1 '       Me.Label1.FlatStyle = System.Windows.Forms.FlatStyle.Flat Me.Label1.Location = New System.Drawing.Point(0, 112) Me.Label1.Name = &quot;Label1&quot; Me.Label1.Size = New System.Drawing.Size(120, 40) Me.Label1.TabIndex = 1 Me.Label1.Text = &quot;Click two places on the form to draw a line.&quot; '       'Button2 '       Me.Button2.Location = New System.Drawing.Point(0, 32) Me.Button2.Name = &quot;Button2&quot; Me.Button2.Size = New System.Drawing.Size(88, 32) Me.Button2.TabIndex = 2 Me.Button2.Text = &quot;Draw Shapes&quot; '       'Button3 '       Me.Button3.Location = New System.Drawing.Point(120, 0) Me.Button3.Name = &quot;Button3&quot; Me.Button3.Size = New System.Drawing.Size(80, 32) Me.Button3.TabIndex = 3 Me.Button3.Text = &quot;Clear the Form&quot; '       'Button4 '       Me.Button4.Location = New System.Drawing.Point(0, 64) Me.Button4.Name = &quot;Button4&quot; Me.Button4.Size = New System.Drawing.Size(88, 32) Me.Button4.TabIndex = 4 Me.Button4.Text = &quot;Draw Filled Shapes&quot; '       'Form1 '       Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(544, 406) Me.Controls.AddRange(New System.Windows.Forms.Control {Me.Button4, Me.Button3, Me.Button2, Me.Label1, Me.Button1}) Me.Name = &quot;Form1&quot; Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen Me.ResumeLayout(False) End Sub

Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click DrawFilledShapes

End Sub Private Sub DrawFilledShapes 'Create the pen object. A Pen object draws a line of specified width and style. 'The line drawn by a Pen object can be filled in a variety of fill styles, 'including solid colors and textures. The fill style depends on the brush or texture 'that is used as the fill object. Dim BluePen As New Pen(Color.Blue, 5)

'Create a graphics object. Dim myGraphics As System.Drawing.Graphics

'Set the graphics object of Form1 to the graphics object created to use Form1 to do the drawing. myGraphics = Me.CreateGraphics

'The following code initializes a new SolidBrush object of the specified color. Dim solidBrush As New SolidBrush(Color.FromArgb(255, 255, 0, 0))

'Fill an Ellipse area on the screen. myGraphics.FillEllipse(solidBrush, 70, 170, 100, 60)

'Fill the rectangle that you created previously. myGraphics.FillRectangle(solidBrush, 175, 200, 195, 200) End Sub Note You must change the code in Visual Basic 2005. By default, Visual Basic creates two files for the project when you create a Windows Forms project. If the form is named Form1, the two files that represent the form are named Form1.vb and Form1.Designer.vb. You write the code in the Form1.vb file. The Windows Forms Designer writes the code in the Form1.Designer.vb file. The Windows Forms Designer uses the partial keyword to divide the implementation of Form1 into two separate files. This behavior prevents the designer-generated code from being interspersed with your code.

For more information about the new Visual Basic 2005 language enhancements, visit the following Microsoft Developer Network (MSDN) Web site:

http://msdn2.microsoft.com/en-us/library/ms379584(vs.80).aspx

For more information about partial classes and the Windows Forms Designer, visit the following MSDN Web site:

http://msdn2.microsoft.com/en-us/library/ms171843.aspx

</li> Save the project.</li> On the Debug menu, click Start, and then run the project.</li></ol>

back to the top

Additional query words: shape line drawing graphics

Keywords: kbvs2005applies kbvs2005swept kbhowtomaster KB309480

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.