Solved

How to Delete a LineShape Control

Posted on 2013-05-21
1
471 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
1 Comment
 
LVL 85

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
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…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

770 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