Lineshape power packs

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

41 Replies - 17071 Views - Last Post: 13 November 2010 - 01:20 PM Rate Topic: -----

#1 jaysnanavati  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 40
  • Joined: 25-December 08

Lineshape power packs

Posted 28 October 2010 - 08:00 AM

Hi everybody,

I have a form, where I am using the vb.net powerpacks to generate lines at run time.


   Sub line()



        Dim ShapeCon As New ShapeContainer

        Dim testLine As New LineShape

        AddHandler testLine.Click, AddressOf linedown

        ShapeCon.Parent = Me

        testLine.Parent = ShapeCon

        testLine.StartPoint = linearray(linecounter).startpos

        testLine.EndPoint = linearray(linecounter).endpos

        distancelabelposition()

    End Sub


I am reading the starting and ending positions from an array.

I am successful in having the line created. However I have been trying for days now to somehow have these lines saved in an array.

For example: when each line is created, it is stored in an array which I can access in order to remove lines and change lines starting and ending positions which are already on the canvas.

Please help me out.

Thanks in advance.

Is This A Good Question/Topic? 0
  • +

Replies To: Lineshape power packs

#2 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1049
  • View blog
  • Posts: 4,076
  • Joined: 02-July 08

Re: Lineshape power packs

Posted 28 October 2010 - 08:13 AM

Why not use GDI+ to draw the lines? In the paint event there is a e.Graphics.DrawLines method or loop thru the array and e.Graphics.DrawLine.

This post has been edited by hawkvalley1: 28 October 2010 - 08:14 AM

Was This Post Helpful? 1
  • +
  • -

#3 jaysnanavati  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 40
  • Joined: 25-December 08

Re: Lineshape power packs

Posted 28 October 2010 - 09:27 AM

Thanks, hawkvalley1 . That is a really good idea. However I am not being able to implement it. I am trying to follow:

http://msdn.microsof...y/f956fzw1.aspx

but, it just would now draw on the canvas. I have the following code


Public Class Form1
    Dim pen As Pen, _
 pt1 As Point, _
 pt2 As Point

    Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        DrawLinePoint(e)
    End Sub
    Sub DrawLinePoint(ByVal e As PaintEventArgs)

        ' Create pen.
        Dim blackPen As New Pen(Color.Black, 3)

        ' Create points that define line.
        Dim point1 As New Point(100, 100)
        Dim point2 As New Point(500, 100)

        ' Draw line to screen.
        e.Graphics.DrawLine(blackPen, point1, point2)
    End Sub
End Class



I think I am probably missing something really simple here but please help me out.

Thanks
Was This Post Helpful? 0
  • +
  • -

#4 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1605
  • View blog
  • Posts: 5,162
  • Joined: 25-September 09

Re: Lineshape power packs

Posted 28 October 2010 - 10:38 AM

Try calling DrawLinePoint from the Form_Paint event. You are passing a system.eventarg to a routine and trying to use it as a system.painteventarg.
Was This Post Helpful? 1
  • +
  • -

#5 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1049
  • View blog
  • Posts: 4,076
  • Joined: 02-July 08

Re: Lineshape power packs

Posted 28 October 2010 - 10:42 AM

When using GDI+ use need to have a container for the things you are drawing. I like using a class object for my lines and store them in a List(Of <lineobject>) this way it remembers what to draw when it needs to draw.
Friend Class Line
  Friend x1 As Integer
  Friend y1 As Integer
  Friend x2 As Integer
  Friend y2 As Integer
  Friend cColor As Color
End Class

Dim Lines As New List(Of Line)

Dim aLine As New Line With {.x1 = 12, .y1 = 12,.x2 = 22, .y2 = 22, .cColor = Color.Black}
Dim bLine As New Line With {.x1 = 24, .y1 = 24,.x2 = 22, .y2 = 22, .cColor = Color.Green}


You also now have a way to edit those values from the List.

Your static variables will work also.

Nice catch Charlie!

This post has been edited by hawkvalley1: 28 October 2010 - 10:53 AM

Was This Post Helpful? 1
  • +
  • -

#6 jaysnanavati  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 40
  • Joined: 25-December 08

Re: Lineshape power packs

Posted 28 October 2010 - 11:24 AM

Thanks CharlieMay , I got the line drawn woo!.

However, looking at hawkvalley1's solution:

I have no idea what that code does and means, I mean I have never used a friend class. What does it do? , when do we use it? why do we use it?

Also what I am looking for is to store each line in an array and then change their starting points and ending points in runtime and the ability to remove lines. However as far as declaring new lines by : Dim aLine As New Line With {.x1 = 12, .y1 = 12, .x2 = 22, .y2 = 22, .cColor = Color.Black}

I am not able to add new lines on runtime. Is there anyway to do it like we did for the buttons--> me.controls.add(bt1) and then use the ctype to address them.

Also say if I do go with the Dim aLine As New Line With {.x1 = 12, .y1 = 12, .x2 = 22, .y2 = 22, .cColor = Color.Black} , how do I get it do draw?

Thanks for all your input both charliemay and hawkhawkvalley1, I highly appreciate it :)
Was This Post Helpful? 0
  • +
  • -

#7 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1049
  • View blog
  • Posts: 4,076
  • Joined: 02-July 08

Re: Lineshape power packs

Posted 28 October 2010 - 01:16 PM

Friend is an access modifier like Public and Private - to which it is more like Public except it is not visible outside the assembly which we rarely need. My idea allows you to store the lines and change the points when needed. You can also add to them in runtime.
With {.x1 = 12, .y1 = 12, .x2 = 22, .y2 = 22, .cColor = Color.Black} is just an intializer, also could be done like.

Dim line As New Line
line.x1 = 12
line.x2 = 12
'etc...

Was This Post Helpful? 1
  • +
  • -

#8 jaysnanavati  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 40
  • Joined: 25-December 08

Re: Lineshape power packs

Posted 29 October 2010 - 06:36 AM

Great!, I understand that now however how do I draw the line, I mean I tried e.graphics.drawline(aline)

but it does not work.
Was This Post Helpful? 0
  • +
  • -

#9 jaysnanavati  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 40
  • Joined: 25-December 08

Re: Lineshape power packs

Posted 29 October 2010 - 06:52 AM

View Posthawkvalley1, on 28 October 2010 - 12:16 PM, said:

Friend is an access modifier like Public and Private - to which it is more like Public except it is not visible outside the assembly which we rarely need. My idea allows you to store the lines and change the points when needed. You can also add to them in runtime.
With {.x1 = 12, .y1 = 12, .x2 = 22, .y2 = 22, .cColor = Color.Black} is just an intializer, also could be done like.

Dim line As New Line
line.x1 = 12
line.x2 = 12
'etc...


I have also tried

Me.Lines.Add(aLine)

but no luck :(
Was This Post Helpful? 0
  • +
  • -

#10 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1049
  • View blog
  • Posts: 4,076
  • Joined: 02-July 08

Re: Lineshape power packs

Posted 29 October 2010 - 07:29 AM

In your paint event you loop thru the Lines:

For Each l As Line In Lines
   'you don't have to use the line color thing I just added that 
   'for fun
   e.Graphics.DrawLine(New Pen(l.cColor), l.x1, l.y1, l.x2, l.y2)
Next

Was This Post Helpful? 1
  • +
  • -

#11 jaysnanavati  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 40
  • Joined: 25-December 08

Re: Lineshape power packs

Posted 06 November 2010 - 01:20 PM

Hi, sorry for the very late reply, but I have tried the following and I do not get anything :(


Public Class Form1
    Dim Lines As New List(Of Line)
    Friend Class Line
        Friend x1 As Integer
        Friend y1 As Integer
        Friend x2 As Integer
        Friend y2 As Integer
        Friend cColor As Color
    End Class

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub
 

    Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
        drawline(e)
    End Sub

    Sub drawline(ByVal e)
        Dim aLine As New Line With {.x1 = 12, .y1 = 12, .x2 = 22, .y2 = 22, .cColor = Color.Black}


        For Each l As Line In Lines
            'you don't have to use the line color thing I just added that 
            'for fun
            e.Graphics.DrawLine(New Pen(l.cColor), l.x1, l.y1, l.x2, l.y2)
        Next

    End Sub
End Class



However if I try this for the drawline sub
  
Sub drawline(ByVal e)
        Dim aLine As New Line With {.x1 = 12, .y1 = 12, .x2 = 22, .y2 = 22, .cColor = Color.Black}


       
        e.Graphics.DrawLine(aLine)


    End Sub




I get lines, however I get 3 lines, 2 diagnols and 1 on top. I cannot change their starting points, color or anything else. Also why do I get more than 1 line, I am very confused. Thanks
Was This Post Helpful? 0
  • +
  • -

#12 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1049
  • View blog
  • Posts: 4,076
  • Joined: 02-July 08

Re: Lineshape power packs

Posted 06 November 2010 - 07:06 PM

The drawline method does not except a parameter of type Line, you must follow the parameters given. You also boxed the e object and did not cast it as type paintEventArgs. If you are going to pass the an object use the proper definition:

Private Sub DrawLine(ByVal e As PaintEventArgs)
'''
End Sub


But why do that when you can just iterate the list and draw in the paint event.

I don't see how you are adding more than 1 line. So you should only have one line.

Accessing members is just like any array, Lines(0).x1 is the x1 field of the first Line element in the array.
Was This Post Helpful? 1
  • +
  • -

#13 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1049
  • View blog
  • Posts: 4,076
  • Joined: 02-July 08

Re: Lineshape power packs

Posted 06 November 2010 - 07:41 PM

Example:
Dim Lines As New List(Of Line)
  Friend Class Line
    Friend x1 As Integer
    Friend y1 As Integer
    Friend x2 As Integer
    Friend y2 As Integer
    Friend cColor As Color
  End Class

  Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Dim l1 As New Line With {.x1 = 10, .x2 = 50, .y1 = 50, .y2 = 40, .cColor = Color.Navy}
    Dim l2 As New Line With {.x1 = 10, .x2 = 50, .y1 = 60, .y2 = 40, .cColor = Color.Black}
    Dim l3 As New Line With {.x1 = 10, .x2 = 50, .y1 = 70, .y2 = 40, .cColor = Color.Red}
    Lines.Add(l1)
    Lines.Add(l2)
    Lines.Add(l3)
  End Sub


  Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
    For Each l As Line In Lines
      e.Graphics.DrawLine(New Pen(l.cColor, 2), l.x1, l.y1, l.x2, l.y2)
      Threading.Thread.Sleep(500)
    Next
  End Sub

Was This Post Helpful? 1
  • +
  • -

#14 jaysnanavati  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 40
  • Joined: 25-December 08

Re: Lineshape power packs

Posted 07 November 2010 - 12:23 AM

Hi,

Thanks for that I have applied that and it works. However now the problem I am facing is that, when I want to add another line, the previous line disappears.

I only want to use one kind of line which is l1 and then want to add several lines on the canvas and then save those lines coordinates for future use.

Is this happening because we have declared the object as new and it is taking the newer values and redrawing it. I have no idea. Please help

Thanks
Was This Post Helpful? 0
  • +
  • -

#15 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1049
  • View blog
  • Posts: 4,076
  • Joined: 02-July 08

Re: Lineshape power packs

Posted 07 November 2010 - 05:56 AM

Adding a line the the Lines List persists, and that's why we simply iterate the list when painting. Show me your code.
Was This Post Helpful? 1
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3