Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Adding events to line (shape)

Adding events to line (shape)

Scheduled Pinned Locked Moved Visual Basic
graphicscsharptutorialquestion
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    helelark123
    wrote on last edited by
    #1

    Hello, I am programming in vb.net 2005 I have two classes: Point and Line My application is drawing ~30 to 50 lines on a picturebox. I don't know if it is possible but I am trying to add an eventhandler to allow user to click on specific line and then to change the color of selected line. I can't success to give to this line object a click event ability. Is it possible? How to do it? Here is my snippet code: Option Explicit On '********START POINT******************* Public Class MainForm Private Line1 As Line Private Sub MainForm_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint Dim P1 As New Point(10, 30), P2 As New Point(120, 210) Line1 = New Line(P1, P2) Line1.DrawLine(e) AddHandler Line1.LineClick, AddressOf LineClicked End Sub Private Sub LineClicked(ByVal sender As Object, ByVal e As EventArgs) 'Do something End Sub End Class '********DELEGATE******************* Public Delegate Sub LineEventHandler(ByVal sender As Object, ByVal e As EventArgs) '********LINE CLASS******************* Public Class Line Inherits EventArgs Private P1 As Point, P2 As Point Sub New(ByVal P1 As Point, ByVal P2 As Point) Me.P1 = P1 Me.P2 = P2 End Sub Sub New(ByVal x1 As Single, ByVal y1 As Single, ByVal x2 As Single, ByVal y2 As Single) Me.P1 = New Point(x1, y1) Me.P2 = New Point(x2, y2) End Sub Public Sub DrawLine(ByVal e As PaintEventArgs) Dim p As New Pen(Color.Black, 1) Dim g As Graphics = e.Graphics g.DrawLine(p, P1.x, P1.y, P2.x, P2.y) End Sub Public Event LineClick As LineEventHandler Protected Overridable Sub OnLineClick(ByVal e As EventArgs) RaiseEvent LineClick(Me, e) End Sub End Class '********POINTC CLASS******************* Public Class Point Public x As Single, y As Single Sub New(ByVal x As Single, ByVal y As Single) Me.x = x Me.y = y End Sub Property xCoord() As Single Get Return x End Get Set(ByVal value As Single) x = value End Set End Property Property yCoord() As Single Get Return y End Get Set(ByVal value As Single) y = value End Set End Property End Class

    Shay Noy

    D 1 Reply Last reply
    0
    • H helelark123

      Hello, I am programming in vb.net 2005 I have two classes: Point and Line My application is drawing ~30 to 50 lines on a picturebox. I don't know if it is possible but I am trying to add an eventhandler to allow user to click on specific line and then to change the color of selected line. I can't success to give to this line object a click event ability. Is it possible? How to do it? Here is my snippet code: Option Explicit On '********START POINT******************* Public Class MainForm Private Line1 As Line Private Sub MainForm_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint Dim P1 As New Point(10, 30), P2 As New Point(120, 210) Line1 = New Line(P1, P2) Line1.DrawLine(e) AddHandler Line1.LineClick, AddressOf LineClicked End Sub Private Sub LineClicked(ByVal sender As Object, ByVal e As EventArgs) 'Do something End Sub End Class '********DELEGATE******************* Public Delegate Sub LineEventHandler(ByVal sender As Object, ByVal e As EventArgs) '********LINE CLASS******************* Public Class Line Inherits EventArgs Private P1 As Point, P2 As Point Sub New(ByVal P1 As Point, ByVal P2 As Point) Me.P1 = P1 Me.P2 = P2 End Sub Sub New(ByVal x1 As Single, ByVal y1 As Single, ByVal x2 As Single, ByVal y2 As Single) Me.P1 = New Point(x1, y1) Me.P2 = New Point(x2, y2) End Sub Public Sub DrawLine(ByVal e As PaintEventArgs) Dim p As New Pen(Color.Black, 1) Dim g As Graphics = e.Graphics g.DrawLine(p, P1.x, P1.y, P2.x, P2.y) End Sub Public Event LineClick As LineEventHandler Protected Overridable Sub OnLineClick(ByVal e As EventArgs) RaiseEvent LineClick(Me, e) End Sub End Class '********POINTC CLASS******************* Public Class Point Public x As Single, y As Single Sub New(ByVal x As Single, ByVal y As Single) Me.x = x Me.y = y End Sub Property xCoord() As Single Get Return x End Get Set(ByVal value As Single) x = value End Set End Property Property yCoord() As Single Get Return y End Get Set(ByVal value As Single) y = value End Set End Property End Class

      Shay Noy

      D Offline
      D Offline
      dan sh
      wrote on last edited by
      #2

      Well, there are a few problems here. Your class line does not need to inherit from EventArgs. Instead it should inherit from Control/Usercontrol class since controls raise event. Next, the EventArgs can be replaced by LineClickEventArgs and that class should inherit from EventArgs class. This class should have properties that the parameter should provide. Then, you need to have a virtual overridable method say OnLineClick which should take in LineClickEventArgs as the parameter. The only thing that remains now is that from where will you call OnLineClick. For this, you will need to identify if the click was on the line. You can have some property that will be set when you click the line. And in the set block, you can call the OnLineClick method. AFAIK you can get to know if the line was clicked only in the MouseDown/MouseUp of your form. And there you can change the property of the Line class which will raise the event. Other way of changing the line color can be this: 1. In the MouseDown/MouseUp event of the form, see if click was on line. 2. Have a flag say IsLineClicked in your form. 3. Add a bool parameter in your DrawLine and in the method have an if else that will set the color in which line will be drawn.

      It's not necessary to be so stupid, either, but people manage it. - Christian Graus, 2009 AD

      H 1 Reply Last reply
      0
      • D dan sh

        Well, there are a few problems here. Your class line does not need to inherit from EventArgs. Instead it should inherit from Control/Usercontrol class since controls raise event. Next, the EventArgs can be replaced by LineClickEventArgs and that class should inherit from EventArgs class. This class should have properties that the parameter should provide. Then, you need to have a virtual overridable method say OnLineClick which should take in LineClickEventArgs as the parameter. The only thing that remains now is that from where will you call OnLineClick. For this, you will need to identify if the click was on the line. You can have some property that will be set when you click the line. And in the set block, you can call the OnLineClick method. AFAIK you can get to know if the line was clicked only in the MouseDown/MouseUp of your form. And there you can change the property of the Line class which will raise the event. Other way of changing the line color can be this: 1. In the MouseDown/MouseUp event of the form, see if click was on line. 2. Have a flag say IsLineClicked in your form. 3. Add a bool parameter in your DrawLine and in the method have an if else that will set the color in which line will be drawn.

        It's not necessary to be so stupid, either, but people manage it. - Christian Graus, 2009 AD

        H Offline
        H Offline
        helelark123
        wrote on last edited by
        #3

        Thank you much

        Shay Noy

        D 1 Reply Last reply
        0
        • H helelark123

          Thank you much

          Shay Noy

          D Offline
          D Offline
          dan sh
          wrote on last edited by
          #4

          Welcome. :)

          It's not necessary to be so stupid, either, but people manage it. - Christian Graus, 2009 AD

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups