13 Replies - 457 Views - Last Post: 06 February 2013 - 05:26 PM Rate Topic: -----

#1 stenhoeve  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 06-February 13

Sytem.Drawing

Posted 06 February 2013 - 06:32 AM

Hi, perhaps someone can get me started with the following. I have a windows form with an empty picturebox, 4 textboxes with editable values and two buttons. If one button (draw line) is clicked than a line is drawn using the values of the 4 textboxes (start-x, start-y, end-x and end-y), when the button is clicked again another line is drawn (with other changed values). If the other button (cancel) is clicked the most recent drawn line is deleted, if the cancel button is clicked again the second most recent line is deleted, etc… I can’t figure out how to get started.

Is This A Good Question/Topic? 0
  • +

Replies To: Sytem.Drawing

#2 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3824
  • View blog
  • Posts: 13,556
  • Joined: 12-December 12

Re: Sytem.Drawing

Posted 06 February 2013 - 07:11 AM

After a minutes' Googling:
http://www.dotnettut...drawing-vb.aspx

or there may be tutorials available here at DIC.
Was This Post Helpful? 0
  • +
  • -

#3 stenhoeve  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 06-February 13

Re: Sytem.Drawing

Posted 06 February 2013 - 07:27 AM

View Postandrewsw, on 06 February 2013 - 07:11 AM, said:

After a minutes' Googling:
http://www.dotnettut...drawing-vb.aspx

or there may be tutorials available here at DIC.


Thanks for your reaction. Indeed I found some also but the issue is how to keep track of the lines drawn in order to delete them separately without freshing the entire picturebox. That I didn't find.
Was This Post Helpful? 0
  • +
  • -

#4 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1067
  • View blog
  • Posts: 4,174
  • Joined: 02-July 08

Re: Sytem.Drawing

Posted 06 February 2013 - 07:34 AM

There is another part to what your wanting - adding/removing lines. For this you must make a collection. A collection of a custom class would be a good idea. Are you familiar with making classes and collections - like a List(Of T)?
Was This Post Helpful? 0
  • +
  • -

#5 stenhoeve  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 06-February 13

Re: Sytem.Drawing

Posted 06 February 2013 - 07:48 AM

View Post_HAWK_, on 06 February 2013 - 07:34 AM, said:

There is another part to what your wanting - adding/removing lines. For this you must make a collection. A collection of a custom class would be a good idea. Are you familiar with making classes and collections - like a List(Of T)?

Hi _HAWK_, I am a total NOOB in VB.NET I am doing some self study and got stuck on this one, no experience in classes or collections. So you mean to make a collection of drawing points?
Was This Post Helpful? 0
  • +
  • -

#6 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1067
  • View blog
  • Posts: 4,174
  • Joined: 02-July 08

Re: Sytem.Drawing

Posted 06 February 2013 - 08:18 AM

No, but close! A custom class that holds the points. The collection holds each new custom class object. In the Paint event you simply iterate the collection and draw the lines. To remove last line you remove the last custom class object in the collection. Look into List(Of T)
Was This Post Helpful? 0
  • +
  • -

#7 stenhoeve  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 06-February 13

Re: Sytem.Drawing

Posted 06 February 2013 - 08:29 AM

View Post_HAWK_, on 06 February 2013 - 08:18 AM, said:

No, but close! A custom class that holds the points. The collection holds each new custom class object. In the Paint event you simply iterate the collection and draw the lines. To remove last line you remove the last custom class object in the collection. Look into List(Of T)

Thanks, I'll try to work this out!
Was This Post Helpful? 0
  • +
  • -

#8 stenhoeve  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 06-February 13

Re: Sytem.Drawing

Posted 06 February 2013 - 12:41 PM

View Poststenhoeve, on 06 February 2013 - 08:29 AM, said:

View Post_HAWK_, on 06 February 2013 - 08:18 AM, said:

No, but close! A custom class that holds the points. The collection holds each new custom class object. In the Paint event you simply iterate the collection and draw the lines. To remove last line you remove the last custom class object in the collection. Look into List(Of T)

Thanks, I'll try to work this out!

You'll probably laugh you pants of but I tried something. The code is below. I use the msgbox to see if anything happened. What I am trying to do is ad the textbox.text values to the list... I think I am totally of the road here any tips :)/>

Public Class form1
    Dim drawpoints As New List(Of String)
    Public Sub lines(ByVal e As Object)
        Dim drawpoints As New List(Of String)
        'Dim graphics As Graphics
        drawpoints.Add(startX.Text)
        drawpoints.Add(startY.Text)
        drawpoints.Add(endX.Text)
        drawpoints.Add(endY.Text)
        MsgBox(drawpoints.Count)
        'graphics = PictureBox1.CreateGraphics
        'Dim pen As New Pen(Color.Red)
        'graphics.DrawLine(pen, CInt(TextBox1.Text), CInt(TextBox2.Text), CInt(TextBox3.Text), CInt(TextBox4.Text))
    End Sub
    Private Sub btnDrawLine_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDrawLine.Click
        lines(e)
    End Sub
    Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
        drawpoints.Remove(startX.Text)
        drawpoints.Remove(startY.Text)
        drawpoints.Remove(endX.Text)
        drawpoints.Remove(endY.Text)
        MsgBox(drawpoints.Count)
        'PictureBox1.Refresh()
    End Sub
End Class


Was This Post Helpful? 0
  • +
  • -

#9 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1067
  • View blog
  • Posts: 4,174
  • Joined: 02-July 08

Re: Sytem.Drawing

Posted 06 February 2013 - 02:02 PM

Good attempt though. Little example:

Class object
Public Class Line
  Public Property pt1 As Point
  Public Property pt2 As Point
  ' poss more stuff here
End Class


Usage:
'inside your form
Private Lines As New List(Of Line)
'in your button click event
Dim newline As New Line
newline.pt1 = New Point(Convert.ToInt32(startx.Text), Convert.ToInt32(starty.Text))
newline.pt2 = New Point(Convert.ToInt32(endx.Text), Convert.ToInt32(endy.Text))
Lines.Add(newline)


Your job iterate the List in the pain event and draw the lines based on the points we stored in the class objects.
Was This Post Helpful? 0
  • +
  • -

#10 stenhoeve  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 06-February 13

Re: Sytem.Drawing

Posted 06 February 2013 - 02:32 PM

View Post_HAWK_, on 06 February 2013 - 02:02 PM, said:

Good attempt though. Little example:

Class object
Public Class Line
  Public Property pt1 As Point
  Public Property pt2 As Point
  ' poss more stuff here
End Class


Usage:
'inside your form
Private Lines As New List(Of Line)
'in your button click event
Dim newline As New Line
newline.pt1 = New Point(Convert.ToInt32(startx.Text), Convert.ToInt32(starty.Text))
newline.pt2 = New Point(Convert.ToInt32(endx.Text), Convert.ToInt32(endy.Text))
Lines.Add(newline)


Your job iterate the List in the pain event and draw the lines based on the points we stored in the class objects.


OK, That'll get me started. But before I take off I placed the code in Visual studio and got 4 errors, should I cope with it or is it explainable?

Error 1 'pt2' is not a member of 'WindowsApplication1.Line'.
Error 2 Property without a 'ReadOnly' or 'WriteOnly' specifier must provide both a 'Get' and a 'Set'.
Error 3 Statement cannot appear within a property body. End of property assumed.
Error 4 Property missing 'End Property'.
Was This Post Helpful? 0
  • +
  • -

#11 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1067
  • View blog
  • Posts: 4,174
  • Joined: 02-July 08

Re: Sytem.Drawing

Posted 06 February 2013 - 02:48 PM

You must be using a lesser framework. It is totally legitimate to declare a property like that. For you:

Private _pt1 As Point
Public Property pt1 As Point
Get
  Return _pt1
End Get
Set(value As Point)
  _pt1 = value
End Set
End Property

This post has been edited by _HAWK_: 06 February 2013 - 02:48 PM

Was This Post Helpful? 0
  • +
  • -

#12 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3824
  • View blog
  • Posts: 13,556
  • Joined: 12-December 12

Re: Sytem.Drawing

Posted 06 February 2013 - 03:18 PM

I've been playing with this but in a slightly different way, using Tuples. I'll post the code here in case it is of interest.

Public Class frmDrawing

    Dim pts As New List(Of Tuple(Of Point, Point))

    Private Sub btnNewLine_Click(sender As Object, e As EventArgs) Handles btnNewLine.Click
        Dim g As System.Drawing.Graphics
        Dim pen1 As New System.Drawing.Pen(Color.Blue, 2)
        Dim pt1 As Point = New Point(CInt(TextBox1.Text), CInt(TextBox2.Text))
        Dim pt2 As Point = New Point(CInt(TextBox3.Text), CInt(TextBox4.Text))

        Dim newLine As New Tuple(Of Point, Point)(pt1, pt2)

        g = picBox1.CreateGraphics
        pts.Add(newLine)
        g.DrawLine(pen1, pts.Last().Item1, pts.Last().Item2)

    End Sub

    Private Sub btnClearLine_Click(sender As Object, e As EventArgs) Handles btnClearLine.Click
        Dim g As System.Drawing.Graphics
        Dim deadLine As Tuple(Of Point, Point)
        Dim eraser As New System.Drawing.Pen(SystemColors.Control, 2)

        g = picBox1.CreateGraphics
        If pts.Count >= 1 Then
            deadLine = pts.Last()
            pts.RemoveAt(pts.Count - 1)
            g.DrawLine(eraser, deadLine.Item1, deadLine.Item2)
        End If

    End Sub
End Class


It took a while to work out how to erase a line :censored: but eventually I achieved it by re-drawing the same line, but using the colour of the Control. If there is another way it would be nice to know :)
Was This Post Helpful? 0
  • +
  • -

#13 stenhoeve  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 06-February 13

Re: Sytem.Drawing

Posted 06 February 2013 - 03:23 PM

View Post_HAWK_, on 06 February 2013 - 02:48 PM, said:

You must be using a lesser framework. It is totally legitimate to declare a property like that. For you:

Private _pt1 As Point
Public Property pt1 As Point
Get
  Return _pt1
End Get
Set(value As Point)
  _pt1 = value
End Set
End Property

Thanks!

View Postandrewsw, on 06 February 2013 - 03:18 PM, said:

I've been playing with this but in a slightly different way, using Tuples. I'll post the code here in case it is of interest.

Public Class frmDrawing

    Dim pts As New List(Of Tuple(Of Point, Point))

    Private Sub btnNewLine_Click(sender As Object, e As EventArgs) Handles btnNewLine.Click
        Dim g As System.Drawing.Graphics
        Dim pen1 As New System.Drawing.Pen(Color.Blue, 2)
        Dim pt1 As Point = New Point(CInt(TextBox1.Text), CInt(TextBox2.Text))
        Dim pt2 As Point = New Point(CInt(TextBox3.Text), CInt(TextBox4.Text))

        Dim newLine As New Tuple(Of Point, Point)(pt1, pt2)

        g = picBox1.CreateGraphics
        pts.Add(newLine)
        g.DrawLine(pen1, pts.Last().Item1, pts.Last().Item2)

    End Sub

    Private Sub btnClearLine_Click(sender As Object, e As EventArgs) Handles btnClearLine.Click
        Dim g As System.Drawing.Graphics
        Dim deadLine As Tuple(Of Point, Point)
        Dim eraser As New System.Drawing.Pen(SystemColors.Control, 2)

        g = picBox1.CreateGraphics
        If pts.Count >= 1 Then
            deadLine = pts.Last()
            pts.RemoveAt(pts.Count - 1)
            g.DrawLine(eraser, deadLine.Item1, deadLine.Item2)
        End If

    End Sub
End Class


It took a while to work out how to erase a line :censored:/> but eventually I achieved it by re-drawing the same line, but using the colour of the Control. If there is another way it would be nice to know :)/>


Thanks, I appreciate it!
Was This Post Helpful? 0
  • +
  • -

#14 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1067
  • View blog
  • Posts: 4,174
  • Joined: 02-July 08

Re: Sytem.Drawing

Posted 06 February 2013 - 05:26 PM

The whole reason for the collection is if you want to remove the last line it is as simple as removing the last element in the collection. The reason I used a class was to allow for additions. What if you want to give each line a different color - make a property for that and set it when you create the new line, then while your iterating you get the the color from each line when painting.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1