.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Code Snippets >> Windows Form Controls >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Draw line on Windows Form

Posted By:Deepika Haridas       Posted Date: October 13, 2010    Points: 15    Category: Windows Form Controls    URL: http://www.dotnetspark.com  

This code shows how you can draw a line in a simple Windows Form
 

Drawing simple lines on a Windows form is not easier as there is no Line control. Still you can make a line by using a Panel control and setting its width or height to a few pixels. But if you want a real line, the secret is in understanding the correct set of library methods you need.

To draw a line with code you need to use GDI +(i.e. the .NET graphics library)

In VB:


Private Sub btnOk_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
Dim g As Graphics = e.Graphics
Dim penLine As Pen = New Pen(Color.CornflowerBlue, 12)
g.DrawLine(penLine, 15, 0, 15, Me.Height)
penLine.Dispose()
End Sub


In C#:


private void btnOk_Paint(object sender,System.Windows.Forms.PaintEventArgs e)
{
Graphics g = e.Graphics;
Pen penLine = new Pen(Color.CornflowerBlue, 12);
g.DrawLine(penLine, 15, 0, 15, this.Height);
penLine.Dispose();
}

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend