Vb net draw line

Vb net draw line DEFAULT

Lesson 26 Introduction to Graphics

26.1 Introduction

Creating graphics is relatively easy in earlier versions of Visual Basic because they have built-in drawing tools. For example, In Visual Basic 6, the drawing tools are included in the toolbox where the programmer just needs to drag the shape controls into the form to create rectangle, square, ellipse, circle and more. However, its simplicity has the shortcomings, you don’t have many choices in creating customized drawings.

Since Visual Basic evolved into an object-oriented programming language under the VB.NET framework, shape controls are no longer available. Now the programmer needs to write code to create various shapes and drawings. Although the learning curve is steeper, the programmer can write powerful code to create all kinds of graphics. Visual Basic 2017 offers various graphics capabilities that enable programmers to write code that can create all kinds of shapes and even fonts. In this lesson, you will learn how to write code to draw lines and shapes in Visual Basic 2017.

26.2 Creating the Graphics Object

Before you can draw anything on a form, you need to create the Graphics object in Visual Basic 2017. A graphics object is created using the CreateGraphics() method. You can create a graphics object that draws to the form itself or a control.

To draw graphics on the default form, you can use the following statement:

Dim myGraphics As Graphics =me.CreateGraphics

To draw in a picture box, you can use the following statement:

Dim myGraphics As Graphics = PictureBox1.CreateGraphics

You can also use the text box as a drawing surface, the statement is:

Dim myGraphics As Graphics = TextBox1.CreateGraphics

The Graphics object that is created does not draw anything on the screen until you call the methods of the Graphics object. In addition, you need to create the Pen object as the drawing tool. We shall examine the code that can create a pen in the following section.

26.3 Creating a Pen

A Pen can be created using the following code:

myPen = New Pen(Brushes.Color, LineWidth)

where myPen is a Pen variable. You can use any variable name instead of myPen. The first argument of the pen object defines the color of the drawing line and the second argument defines the width of the drawing line. For example, the following code created a pen that can draw a dark magenta line and the width of the line is 10 pixels:

myPen = New Pen(Brushes.DarkMagenta, 10)

You can also create a Pen using the following statement:

Dim myPen As PenmyPen = New Pen(Drawing.Color.Blue, 5)

Where the first argument defines the color(here is blue, you can change that to red or whatever color you want) and the second argument is the width of the drawing line.

Having created the Graphics and the Pen objects, you are now ready to draw graphics on the screen which we will show you in the following section.

26.4 Drawing a Line

In this section, we will show you how to draw a straight line on the Form.

First of all, launch Visual basic 2017. In the start up page, drag a button into the form. Double click on the button and key in the following code.

Private Sub BtnDraw_Click(sender As Object, e As EventArgs) Handles BtnDraw.Click Dim myGraphics As Graphics = Me.CreateGraphics Dim myPen As Pen myPen = New Pen(Brushes.DarkMagenta, 20) myGraphics.DrawLine(myPen, 60, 180, 220, 50) End Sub

The second created the Graphics object and the third and fourth line create the Pen object. The fifth draw a line on the Form using the DrawLine method. The first argument uses the Pen object created by you, the second argument and the third arguments define the coordinate the starting point of the line, the fourth and the last arguments define the ending coordinate of the line. The syntax of the Drawline argument is

object.DrawLine(Pen, x1, y1, x2, y2)

For the above example, the starting coordinate is (60,80) and the ending coordinate is (220,50) Figure 26.1 shows the line created by the program.

VB2013_figure25.1

❮ Previous LessonNext Lesson ❯

Sours: https://www.vbtutor.net/vb2017/VB2017_Lesson26.html

Graphics.DrawLine Method

Definition

Important

Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.

Draws a line connecting the two points specified by the coordinate pairs.

Overloads

DrawLine(Pen, PointF, PointF)

Draws a line connecting two PointF structures.

DrawLine(Pen, Int32, Int32, Int32, Int32)

Draws a line connecting the two points specified by the coordinate pairs.

DrawLine(Pen, Single, Single, Single, Single)

Draws a line connecting the two points specified by the coordinate pairs.

DrawLine(Pen, Point, Point)

Draws a line connecting two Point structures.

DrawLine(Pen, PointF, PointF)

Draws a line connecting two PointF structures.

Parameters

pen
Pen

Pen that determines the color, width, and style of the line.

pt1
PointF

PointF structure that represents the first point to connect.

pt2
PointF

PointF structure that represents the second point to connect.

Exceptions

Examples

The following code example is designed for use with Windows Forms, and it requires PaintEventArgs, which is a parameter of the Paint event handler. The code performs the following actions:

  • Creates a black pen.

  • Creates points for the endpoints of the line.

  • Draws the line to the screen.

Remarks

This method draws a line connecting the two points specified by the and p parameters.

See also

DrawLine(Pen, Int32, Int32, Int32, Int32)

Draws a line connecting the two points specified by the coordinate pairs.

Parameters

pen
Pen

Pen that determines the color, width, and style of the line.

x1
Int32

The x-coordinate of the first point.

y1
Int32

The y-coordinate of the first point.

x2
Int32

The x-coordinate of the second point.

y2
Int32

The y-coordinate of the second point.

Exceptions

Examples

The following code example is designed for use with Windows Forms, and it requires PaintEventArgs, which is a parameter of the Paint event handler. The code performs the following actions:

  • Creates a black pen.

  • Creates the coordinates of the endpoints of the line.

  • Draws the line to the screen.

Remarks

This method draws a line connecting the two points specified by the , , , and parameters.

See also

DrawLine(Pen, Single, Single, Single, Single)

Draws a line connecting the two points specified by the coordinate pairs.

Parameters

pen
Pen

Pen that determines the color, width, and style of the line.

x1
Single

The x-coordinate of the first point.

y1
Single

The y-coordinate of the first point.

x2
Single

The x-coordinate of the second point.

y2
Single

The y-coordinate of the second point.

Exceptions

Examples

The following code example is designed for use with Windows Forms, and it requires PaintEventArgs, which is a parameter of the Paint event handler. The code performs the following actions:

  • Creates a black pen.

  • Creates the coordinates of the endpoints of the line.

  • Draws the line to the screen.

Remarks

This method draws a line connecting the two points specified by the , , , and parameters.

See also

DrawLine(Pen, Point, Point)

Draws a line connecting two Point structures.

Parameters

pen
Pen

Pen that determines the color, width, and style of the line.

pt1
Point

Point structure that represents the first point to connect.

pt2
Point

Point structure that represents the second point to connect.

Exceptions

Examples

The following code example is designed for use with Windows Forms, and it requires PaintEventArgs, which is a parameter of the Paint event handler. The code performs the following actions:

  • Creates a black pen.

  • Creates points for the endpoints of the line.

  • Draws the line to the screen.

See also

Sours: https://docs.microsoft.com/en-us/dotnet/api/system.drawing.graphics.drawline
  1. Royal distributing discount code
  2. Shes broken meme
  3. Nc gun shows schedule
  4. Cute love meme
  5. Symphony of eternity walkthrough
  1. Mar 21st, 2013, 10:00 AM#1

    ryntje is offline

    Thread Starter

    New Member

    Draw a line in VB.NET (VS2012)

    Hi all,

    It drives me crazy but I can't get a line drawn into a form. The form keeps blank.
    I'm sure it's easy but I'm still a junior developer so excuses me for that.


    Code:

    Imports System.Drawing Imports System.Drawing.Drawing2D . . . Public Sub DrawLine() Dim g As System.Drawing.Graphics = Me.CreateGraphics() Dim pen As New System.Drawing.Pen(System.Drawing.Color.Black, 10) g.DrawLine(pen, 20, 50, 50, 80) pen1.Dispose() g.Dispose() End Sub
    I debug the program but the Form (Me) keeps blank. What I'm doing wrong?
    There are several ways to do it but mainly I keep on reading the same code in several forums.

  2. Mar 21st, 2013, 10:14 AM#2


  3. Mar 21st, 2013, 10:22 AM#3

    ryntje is offline

    Thread Starter

    New Member

    Re: Draw a line in VB.NET (VS2012)

    Quite complex. Can you tell me how to get the line displayed?
    I tried to draw it in a picture box, but again now line is shown (or overwritten before it gets displayed)


  4. Mar 21st, 2013, 10:30 AM#4


  5. Mar 21st, 2013, 10:39 AM#5

    Re: Draw a line in VB.NET (VS2012)

    There are two aspects to showing a drawn line. One, the graphics object must be associated with a drawing surface. Two the line must be repainted every time that the graphics are updated for any reason. The Paint event does both of these for you automatically, eg.

    vb.net Code:

    1. Private Sub Form1_Paint(sender As System.Object, e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint

    2.         Dim pen As New System.Drawing.Pen(System.Drawing.Color.Black, 10)

    3.         e.Graphics.DrawLine(pen, 20, 50, 50, 80)

    4.     End Sub


    If you are drawing direct to the form, any control at the same place will of course obscure the graphics.The same applies to panels and other containers.

    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!


  6. Mar 22nd, 2013, 04:19 AM#6

    ryntje is offline

    Thread Starter

    New Member

    Re: Draw a line in VB.NET (VS2012)

    Ok. I've created a picture box PB_1 and created the code

    Code:

    Private Sub DrawLine(sender As System.Object, e As System.Windows.Forms.PaintEventArgs) Handles PB_1.Paint Dim pen As New System.Drawing.Pen(System.Drawing.Color.Black, 10) e.Graphics.DrawLine(pen, 20, 50, 50, 80) End Sub
    But now I want to call the routine.
    I can't be done by "DrawLine()" because you don't specify the argument for "sender" an "e"

    So how to call the DrawLine routine?

  7. Mar 22nd, 2013, 05:33 AM#7

    Re: Draw a line in VB.NET (VS2012)

    QuoteOriginally Posted by ryntjeView Post
    Ok. I've created a picture box PB_1 and created the code

    Code:

    Private Sub DrawLine(sender As System.Object, e As System.Windows.Forms.PaintEventArgs) Handles PB_1.Paint Dim pen As New System.Drawing.Pen(System.Drawing.Color.Black, 10) e.Graphics.DrawLine(pen, 20, 50, 50, 80) End Sub
    But now I want to call the routine.
    I can't be done by "DrawLine()" because you don't specify the argument for "sender" an "e"

    So how to call the DrawLine routine?
    You don't call a Paint event handler yourself. Windows calls it, because the OS has to make sure that the screen is correctly shared among all the processes that are running. And Windows provides the PaintEventArgs for you. When you want to trigger a Paint event in code, call Invalidate or Refresh for the control concerned. (It won't matter much here whether you use Invalidate or Refresh but it can make a difference when you are drawing thousands of lines or a large image.) This is the pattern, with a button click just as an example:

    Code:

    Private Sub Button1_Click(Sender As Object, e As EventArgs) Handles Button1.Click PB_1.Invalidate() 'trigger Paint event for the picture pox PB_1 End Sub Private Sub PB_1_Paint(sender As System.Object, e As System.Windows.Forms.PaintEventArgs) Handles PB_1.Paint Dim pen As New System.Drawing.Pen(System.Drawing.Color.Black, 10) e.Graphics.DrawLine(pen, 20, 50, 50, 80) End Sub
    (Note: I've changed the name of your Paint sub from DrawLine to the conventional name style for event handlers, controlname_eventname.)

    BB

  8. Mar 22nd, 2013, 11:42 AM#8

    Re: Draw a line in VB.NET (VS2012)

    If you only want to draw the line at certain times, or you want to draw multiple lines in multiple places, then the approach would be to build on what Boops said. What I would add would be a class called something like Line (though there very well may be such a class already in some library that I don't know about), which held the five arguments to the DrawLine method. You would then have a List (of Line) at form scope. In the Paint event, the routine would iterate through all the elements in the list and call DrawLine for each one.

    By doing this, you'd be decoupling the drawing from what you are drawing. The Paint event wouldn't be deciding what to draw, it would just be drawing everything in the list. If there was nothing in the list it would drawn nothing, if there was one line it would draw one line, and if there were many lines it would draw many lines. Other code would be managing the lines in the list. If you want more lines, you'd add them to the list. If you want less lines you'd remove them from the list. Managing what was in the list would be different from the drawing, and that would be a good thing.

    My usual boring signature: Nothing

  9. Dec 25th, 2013, 01:14 PM#9

    sureshNAIR70 is offline
    New Member

    Re: Draw a line in VB.NET (VS2012)

    I have a similar question and am hopeful someone could help me solve the same.
    I've tried boops code
    Private Sub Button1_Click(Sender As Object, e As EventArgs) Handles Button1.Click
    PB_1.Invalidate() 'trigger Paint event for the picture pox PB_1
    End Sub

    Private Sub PB_1_Paint(sender As System.Object, e As System.Windows.Forms.PaintEventArgs) Handles PB_1.Paint
    Dim pen As New System.Drawing.Pen(System.Drawing.Color.Black, 10)
    e.Graphics.DrawLine(pen, 20, 50, 50, 80)
    End Sub

    but the line gets drawn before the command button is clicked. could you help me restrict the drawing to fire only when the button is clicked?

    Secondly
    I have quite a few sub routines that i would wish to call on the command button click how should i go about with the same.
    Appreciate any help on the same.
    Best Regards


  10. Dec 25th, 2013, 04:16 PM#10


  11. Dec 25th, 2013, 04:21 PM#11

    AceInfinity is offline
    Fanatic Member AceInfinity's Avatar

    Re: Draw a line in VB.NET (VS2012)

    This is because the OnPaint() method fires early on when the form is created, you can't control that, what you can do is set a boolean that is evaluated the next time the control is drawn however, and use that to decide whether the line is drawn or not. You should create your own thread next time though.


  12. Dec 25th, 2013, 09:40 PM#12

    sureshNAIR70 is offline
    New Member

    Re: Draw a line in VB.NET (VS2012)

    QuoteOriginally Posted by AceInfinityView Post

    This is because the OnPaint() method fires early on when the form is created, you can't control that, what you can do is set a boolean that is evaluated the next time the control is drawn however, and use that to decide whether the line is drawn or not. You should create your own thread next time though.


    Thanks dunfiddlin/ AceInfinity for your replies.
    Can you help me with a simple example on how the boolean can be used?

  13. Dec 26th, 2013, 12:57 AM#13

    sureshNAIR70 is offline
    New Member

    Re: Draw a line in VB.NET (VS2012)

    Public Class Form1

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    1:
    Using bmp As New Bitmap(pb.Width, pb.Height)
    2:
    Using g As Graphics = Graphics.FromImage(bmp)
    3:
    g.FillRectangle(Brushes.AliceBlue, New Rectangle(10, 10, 100, 100))
    4:
    g.DrawRectangle(Pens.Black, New Rectangle(10, 10, 100, 100))
    5:
    pb.Image = CType(bmp.Clone, Image)
    6:
    End Using
    7:
    End Using

    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    1:
    Using bmp As New Bitmap(pb.Width, pb.Height)
    2:
    Using g As Graphics = Graphics.FromImage(bmp)
    3:
    g.FillRectangle(Brushes.AliceBlue, New Rectangle(2, 2, 50, 50))
    4:
    g.DrawRectangle(Pens.Red, New Rectangle(2, 2, 50, 50))
    5:
    pb.Image = CType(bmp.Clone, Image)
    6:
    End Using
    7:
    End Using

    End Sub
    End Class

    Here as you will notice the Button2.Click over writes Button1.Click
    What i want is to superimpose the second rectangle on the first one so I can see both the rectangles after Button2.Click. ( right now i am loosing the first rectangle)


  14. Dec 26th, 2013, 05:15 AM#14

    Re: Draw a line in VB.NET (VS2012)

    Don't use a new bitmap for each drawing, otherwise you're just replacing the previous bitmap with the new bitmap so only see the latest drawing.
    One way your example could be modified:

    Code:

    Public Class Form1 Dim bmp As Bitmap Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load bmp = New Bitmap(pb.Width, pb.Height) End Sub Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click Using g As Graphics = Graphics.FromImage(bmp) g.FillRectangle(Brushes.AliceBlue, New Rectangle(10, 10, 100, 100)) g.DrawRectangle(Pens.Black, New Rectangle(10, 10, 100, 100)) End Using pb.Invalidate() 'update the picturebox End Sub Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click Using g As Graphics = Graphics.FromImage(bmp) g.FillRectangle(Brushes.AliceBlue, New Rectangle(2, 2, 50, 50)) g.DrawRectangle(Pens.Red, New Rectangle(2, 2, 50, 50)) End Using pb.Invalidate() 'update the picturebox End Sub Private Sub pb_Paint(sender As Object, e As System.Windows.Forms.PaintEventArgs) Handles pb.Paint e.Graphics.DrawImage(bmp, 0, 0) End Sub End Class

  15. Dec 26th, 2013, 05:28 AM#15

    sureshNAIR70 is offline
    New Member

    Re: Draw a line in VB.NET (VS2012)

    Thanks Passel. This is exactly what i wanted.
    Could please send me your email Id so that i could send you the entire project file?
    KR
    SN


  16. Dec 26th, 2013, 11:39 AM#16


  17. Dec 26th, 2013, 12:42 PM#17

    sureshNAIR70 is offline
    New Member

    Re: Draw a line in VB.NET (VS2012)

    I understand.
    Is there a way i can post the project on the forum as the file size is 1.05MB after its zipped.
    I understand the file size limitation is 0.5mb


  18. Dec 26th, 2013, 04:15 PM#18


  19. Dec 27th, 2013, 12:17 AM#19

    sureshNAIR70 is offline
    New Member

    Re: Draw a line in VB.NET (VS2012)

    dunfiddlin & Aceinfinity. Thank you for your help the solution worked perfectly for me.
    I am getting used to the rules of the forum and excuse me for my ignorance.
    Any ways guys thanks a lot. You were a great help.
    KR
    SN

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

Forum Rules




Click Here to Expand Forum to Full Width


Sours: https://www.vbforums.com/

Thread: How can I draw a straight line in vb.net?

  1. July 26th, 2003, 06:20 AM#1

    Question How can I draw a straight line in vb.net?

    Morning all,

    Is there anyway to add a straight line or curved line to my application in vb.net?

    In vb6 you could use the line component from the toolbox to do this but that component seems to have been removed from the vb.net toolbox.


    Thanks in advance,

    B.


  2. July 26th, 2003, 08:16 AM#2

    There isn't a line control in VB.NET... but u could code for it.


    Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
    Dim blackPen As New Pen(Color.Black, 3)
    ' Create coordinates of points that define line.
    Dim x1 As Single = 100.0F
    Dim y1 As Single = 100.0F
    Dim x2 As Single = 500.0F
    Dim y2 As Single = 100.0F
    ' Draw line to screen.
    e.Graphics.DrawLine(blackPen, x1, y1, x2, y2)
    End Sub

    Back after a long hibernation.


  3. July 26th, 2003, 11:21 AM#3

    How would you call that sub?


  4. July 26th, 2003, 10:27 PM#4

    U don't need to call that sub. It fires automatically since it's a paint event.

    There are some other line controls (user drawn) available on the web as well that uses the pen technique.

    Back after a long hibernation.


  5. July 27th, 2003, 12:29 AM#5

    Originally posted by newvisva
    U don't need to call that sub. It fires automatically since it's a paint event.

    There are some other line controls (user drawn) available on the web as well that uses the pen technique.
    I figured that out but it paints on the form, I need something to paint on a textbox like a background, so you can read text that goes over it

  6. July 27th, 2003, 07:19 AM#6

    kasracer,

    I assume that u want to put a background pic/ paint the background of a textbox. It is not possible directly coz the textbox doesn't have a paint event.

    However, if u inherits a textbox, u could get to override the paint event which means u could draw. If u need a sample i could post it.

    Back after a long hibernation.


  7. July 27th, 2003, 10:22 AM#7

    Thanks newvisva that works great but I need to add several lines on different forms. Is it possible to put this code into a class so that I can reuse it on any form in my application.

    The idea would be to have a line control which I could drag and drop onto my forms from the toolbox, much like in vb6.

    Is this possible?

    B.


  8. July 27th, 2003, 12:42 PM#8

    you could do this ....
    in a module :

    Code:

    Module Module1 Public Function DrawLine(ByVal frm As Form) As Boolean Dim grph As Graphics = frm.CreateGraphics With grph Dim p As New Pen(Color.Red, 3) .DrawLine(p, 0, 50, frm.Width, 50) End With Return True End Function '/// this function will handle paint events for all forms End Module
    in your chosen forms :

    Code:

    Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint DrawLine(Me) End Sub
    string Signature = Censored;

  9. July 27th, 2003, 02:05 PM#9

    Originally posted by newvisva
    kasracer,

    I assume that u want to put a background pic/ paint the background of a textbox. It is not possible directly coz the textbox doesn't have a paint event.

    However, if u inherits a textbox, u could get to override the paint event which means u could draw. If u need a sample i could post it.
    Could you post a sample please? If I could figure out how to paint onto the background of the textbox, then I could figureout where to put it.

  10. July 27th, 2003, 03:28 PM#10

    kasracer , are you trying to draw lines in a textbox? or do something else?

    string Signature = Censored;

  11. July 27th, 2003, 04:13 PM#11

    Originally posted by dynamic_sysop
    kasracer , are you trying to draw lines in a textbox? or do something else?
    I want to draw a line in the background of the text box at a margin

  12. July 27th, 2003, 04:46 PM#12

    ok well here's a quick example of drawing a line across the textbox , you would have to use trial and error to get the position ( where it has 4's at present in the code example )

    Code:

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim g As Graphics = TextBox1.CreateGraphics Dim pn As New Pen(Color.Red, 2) Dim rct As New Rectangle(TextBox1.Location.X, TextBox1.Location.Y, TextBox1.Width, TextBox1.Height) With g .DrawLine(pn, 0, 4, rct.Width, 4) .Save() End With End Sub
    string Signature = Censored;

  13. July 27th, 2003, 05:46 PM#13

    I tried to use that code and I put it on when the textbox has focus (Since this will always be on the textbox)

    The line appears for like .01 of a second then the textbox covers it.


  14. July 27th, 2003, 06:03 PM#14

    Question. The RichTextBox does not support a transparent background. Is there ANYWAY to make it so it does? If there was a way to allow it's backgorund to be transparent, what I'm trying to do would be very simple


  15. July 27th, 2003, 06:16 PM#15

    you can make a richtextbox's back transparent

    Code:

    Private Declare Function GetWindowLong Lib "user32.dll" Alias "GetWindowLongA" (ByVal hwnd As IntPtr, ByVal nIndex As Integer) As Integer Private Declare Function SetWindowLong Lib "user32.dll" Alias "SetWindowLongA" (ByVal hwnd As IntPtr, ByVal nIndex As Integer, ByVal dwNewLong As Integer) As Integer Private Const GWL_EXSTYLE As Integer = -20 Private Const WS_EX_TRANSPARENT As Integer = CInt(&H20&) Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim x As Integer = GetWindowLong(RichTextBox1.Handle, GWL_EXSTYLE) x = x Or WS_EX_TRANSPARENT SetWindowLong(RichTextBox1.Handle, GWL_EXSTYLE, x) RichTextBox1.Visible = False RichTextBox1.Refresh() RichTextBox1.Visible = True End Sub
    it'll work
    string Signature = Censored;

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

Forum Rules




Sours: https://forums.codeguru.com/

Net line vb draw

How to draw a line in VB.NET

Basically, what you did wrong was to use the method.

This is something that you rarely, if ever, need to do. It's not as if the method is broken, of course. It does exactly what it says: it is documented as doing: returning a object representing the drawing surface of your form.

The problem is that whenever your form gets redrawn (which can happen for lots of reasons), the object basically gets reset. As a result, everything that you drew into the one that you obtained is erased.

A form is always redrawn when it is first loaded, so using never makes sense in the event handler method. It is also going to be redrawn any time that it is minimized and restored, covered up by another window, or even resized (some of these depend on your operating system, graphics drivers, and your form's properties, but that's beyond the point).

The only time that you might use is when you want to show immediate feedback to the user that should not persist across redraws. For example, in the handler for the event, when showing feedback for a drag-and-drop.

So, what is the solution? Always do your drawing inside of the event handler method. That way, it persists across redraws, since a "redraw" basically involves raising the event.

When the event is raised, the handler is passed an instance of the class, which contains a object that you can draw into.

So here's what your code should look like:

(Note also that in the above code, I am overriding the method, rather than handling the corresponding event. This is considered best practice for handling events in a derived class. But either way will work.)

Sours: https://stackoverflow.com/questions/17895619/how-to-draw-a-line-in-vb-net
Draw Graphics in VB.Net

How to draw a line in a VB.NET form?

hi guys!
can anyone tell me now to draw a line in VB.NET form?
I've been trying to do so , but nothings coming..

Hi there,

i was fetching the same thing, it about using the mouse down and mouse up and mouse move events on the form, you have to start drawing on mouse down and stop on mouse up, here is the code you have to put in a new form:

' True while we are drawing the new line.
Private m_Drawing As Boolean

' Buffer for erasing rubberband lines.
Private m_BufferBitmap As Bitmap
Private m_BufferGraphics As Graphics

' The mouse position.
Private m_X1 As Integer
Private m_Y1 As Integer
Private m_X2 As Integer
Private m_Y2 As Integer

' Start drawing a rubberband line.
Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseDown
' Do nothing if this isn'tthe left mouse button.
If e.Button <> MouseButtons.Left Then Exit Sub
m_Drawing = True

' Save a snapshot of the form.
SaveSnapshot()

' Save the current mouse position.
m_X1 = e.X
m_X2 = e.X
m_Y1 = e.Y
m_Y2 = e.Y
End Sub

Private Sub SaveSnapshot()
Dim new_bitmap As Bitmap

' Make a new bitmap that fits the form.
new_bitmap = New Bitmap(Me.Size.Width, Me.Size.Height, Me.CreateGraphics())
m_BufferGraphics = Graphics.FromImage(new_bitmap)

' Clear the new bitmap.
m_BufferGraphics.Clear(Me.BackColor)

' Copy the existing bitmap's contents into
' the new bitmap.
If Not (m_BufferBitmap Is Nothing) Then
m_BufferGraphics.DrawImage(m_BufferBitmap, 0, 0)
End If

' Save the new bitmap and graphics objects.
m_BufferBitmap = new_bitmap
End Sub

' Continue drawing the rubberband line.
Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseMove
' Do nothing if we're not drawing.
If Not m_Drawing Then Exit Sub

' Save the new point.
m_X2 = e.X
m_Y2 = e.Y

' Erase the previous line.
DrawForm(Me.CreateGraphics())

' Draw the new line directly on the form.
Me.CreateGraphics().DrawLine( _
Pens.Gray, m_X1, m_Y1, m_X2, m_Y2)
End Sub

' Redraw the saved buffer.
Private Sub DrawForm(ByVal gr As Graphics)
If Not (m_BufferBitmap Is Nothing) Then gr.DrawImage(m_BufferBitmap, 0, 0)
End Sub

' Finish drawing the new line.
Private Sub Form1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseUp
' Do nothing if we're not drawing.
If Not m_Drawing Then Exit Sub
m_Drawing = False

' Save the new point.
m_X2 = e.X
m_Y2 = e.Y

' Draw the new line permanently on the buffer.
m_BufferGraphics.DrawLine( _
Pens.Blue, m_X1, m_Y1, m_X2, m_Y2)

' Redraw to show the new line.
DrawForm(Me.CreateGraphics())
End Sub

' Redraw the form.
Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
DrawForm(e.Graphics)
End Sub


hope it work for you,

Sours: https://www.daniweb.com/programming/software-development/threads/24617/how-to-draw-a-line-in-a-vb-net-form

Now discussing:

Resting her hands on the floor, Ishra pushed her ass out to my face. Her pussy and pubis were covered with fine stubble. A large clitoris was clearly visible under the plump, shameless lips. I drove my penis into her pussy full length and rested against the uterus.



15790 15791 15792 15793 15794