Draw, move, and delete line segments in VB.NET

[line editor]

This progra lets you draw, move, and delete line segments from a drawing. It’s a Visual Basic .NET version of the example Draw, move, and delete line segments in C# made by request.

Click and drag to draw line segments. If you click on a segment’s end point, you can drag it into a new position. If you click on a segment’s body, you can move the whole segment. Finally if you drag a segment onto the trash can icon, the program asks if you want to delete the segment.

See the original post for an explanation of how the program works.


Download Example   Follow me on Twitter   RSS feed   Donate




About RodStephens

Rod Stephens is a software consultant and author who has written more than 30 books and 250 magazine articles covering C#, Visual Basic, Visual Basic for Applications, Delphi, and Java.
This entry was posted in drawing, graphics and tagged , , , , , , , , , , , , , , , , , , , , , . Bookmark the permalink.

5 Responses to Draw, move, and delete line segments in VB.NET

  1. Marino Liranzo says:

    Hello Mr. Rod Stephens,

    I am working on a small application in which I draw straight lines and rectangles in a picturebox. I would like to know how to implement the Undo and ReDo functionalities when I am creating lines.

    If you could help me with an example of the code.

    Greetings
    Marino Liranzo

  2. Marino Liranzo says:

    Thank Mr. Rod Stephens,
    You are very kind.
    I will try to make my code starting from this example

    Greeting.
    Marino Liranzo

  3. Eddie Bole says:

    Hi Rod.

    I noticed that with this example one cannot draw a NEW line segment when over an existing line segment node point. I was thinking whether the left and right mouse buttons may be used to selectively perform different functions when positioned over the end of an existing line (like the right mouse button draws a new line whereas the left mouse button moves the end of an existing line). Also how would one add an additional feature like holding down ctrl while dragging to draw a horizontal line and ctrl + shift to draw a vertical line. I love this sort of vector drawing code. I apologize for asking so many questions, but I tend to have a habit of being inquisitive.

    Regards
    Eddie

    • RodStephens says:

      True. You also cannot start a new segment on top of the interior of an existing segment. (It tries to move the segment.)

      You could check the keyboard state to see if a key such as Shift or Alt is down and then take different actions if you like. I think the following tests whether the Shift key is down.

      if ((Control.ModifierKeys & Keys.Shift) != 0) ...

      If you want to know whether only the Shift key is down, and not something like Shift-X, then use something like the following.

      if (Control.ModifierKeys == Keys.Shift) ...

      Another approach would be to have a toolbox like VB, C#, and other tools do. One tool would be the drawing tool and another would be a selection tool. You could have others to draw ellipses, rectangles or whatever.

      You can uses similar key tests to tell if the user is pressing Shift, Ctrl, or whatever while dragging. Then you can add or toggle objects in a selection so you can later move the selection as a whole. You can keep the selected objects in a List so you can move them easily later.

      At this point, you’re pretty much building MS Paint, CorelDRAW, Inkscape, Microsoft Word’s drawing editor, or a similar tool. They’re fun to build, but can be a lot of work. I would experiment with one of them to see exactly how they handle all of these issues before you start building your own. There are a ton of details that you don’t notice until you’re trying to do the same thing.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.