Solved

How to Delete a LineShape Control

Posted on 2013-05-21
1
475 Views
Last Modified: 2013-05-21
The code below spawns a new lineshape when a user right-clicks and selects "connect" from a parent control's contextmenustrip.   Is there a way to add to the newly formed lineshape a right-click contextmenustrip with a "Delete" option to allow a user to delete a line that's not wanted?
  Private Sub ctmsconnect_Click(sender As System.Object, e As System.EventArgs) Handles ctmsconnect.Click
        ' Private Sub ctms_Click(sender As System.Object, e As System.EventArgs) Handles boxCtms1.Click
        mLineWidth = 1

        Dim myItem As ToolStripMenuItem = CType(sender, ToolStripMenuItem)
        Dim cms As ContextMenuStrip = CType(myItem.Owner, ContextMenuStrip)
        'MessageBox.Show(boxCtms1.SourceControl.Name)
        Dim x As Integer = cms.SourceControl.Right - 2
        Dim y As Integer = cms.SourceControl.Top + (cms.SourceControl.Height / 2 - 12)
        Dim LS As New LineShape
        NumLineShapes += 1
        LS.Name = "LineShape" & NumLineShapes
        LS.BorderColor = Color.Black
        LS.BorderWidth = 2
        LS.Cursor = Cursors.Hand

        ' set starting and ending coordinates for the line
        LS.StartPoint = New Point(x, y)
        LS.EndPoint = New Point(x + 80, y - 5)
        'sLSTemp.StartPoint = New System.Drawing.Point(siSCCount * 20, 60 + siSCCount * 60)
        'sLSTemp.EndPoint = New System.Drawing.Point(100 + siSCCount * 20, 110 + siSCCount * 60)

        ' set new LineShape to top of z-order
        LS.BringToFront()
        LS.BringToFront()

        ' connect ContextMenuStrip to LineShape
        'LS.ContextMenuStrip = mLsCtm1
        ' add LineShape to ShapeContainer
        'myLS.Parent = mySC
        '  'Me.Controls.Add(mySC)

        numconnectedlineendpoints += 1
        Dim myValues As New List(Of String)
        myValues.Add(cms.SourceControl.Name)
        DropLineOriginalObjectName = cms.SourceControl.Name
        OrigDropControl = cms.SourceControl
        myValues.Add(LS.Name)
        'myValues.Add(mySC.Name)
        myValues.Add("linestart")
        dicGUIControls.Add(numconnectedlineendpoints, myValues)
       
        Lines.Add(LS)
        SC.Shapes.Add(LS)
        Me.Refresh()
        ' add handlers

    End Sub

Open in new window

0
Comment
Question by:lep1
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 300 total points
ID: 39186215
This example should demonstrate what needs to be done:
Public Class Form1

    Private LS As LineShape

    Private R As New Random
    Private SC As New ShapeContainer
    Private CMS As New ContextMenuStrip
    Private WithEvents delete As ToolStripItem

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Me.Controls.Add(SC)
        delete = CMS.Items.Add("Delete")
    End Sub

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Dim LS As New LineShape
        LS.BorderWidth = 2
        LS.Cursor = Cursors.Hand
        LS.StartPoint = New Point(R.Next(Me.Width), R.Next(Me.Height))
        LS.EndPoint = New Point(R.Next(Me.Width), R.Next(Me.Height))

        LS.ContextMenuStrip = CMS
        AddHandler LS.MouseDown, AddressOf LS_MouseDown

        SC.Shapes.Add(LS)
    End Sub

    Private Sub LS_MouseDown(sender As Object, e As System.Windows.Forms.MouseEventArgs)
        If e.Button = Windows.Forms.MouseButtons.Right Then
            Me.LS = sender
        End If
    End Sub

    Private Sub delete_Click(sender As Object, e As System.EventArgs) Handles delete.Click
        If Not IsNothing(LS) Then
            SC.Shapes.Remove(LS)
            LS = Nothing
        End If
    End Sub

End Class

Open in new window

0

Featured Post

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question