Page 1 of 2

Sender from contextmenustrip

Posted: Sat Feb 09, 2013 10:12 pm
by muttley1968
Hi i am attempting to make a context menu with a few controls one of wich removes controls from
a panel that are added by a users not the program so i attempted to use this code
Code: Select all
    Private Sub RemoveImageToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RemoveImageToolStripMenuItem.Click
        MsgBox("image Removed")
    End Sub

    Private Sub RemoveImageToolStripMenuItem_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles RemoveImageToolStripMenuItem.MouseUp
        If e.Button = MouseButtons.Right Then
            Panel1.Controls.Remove(sender)
        End If
    End Sub
But it does not work the control remains active inside of the panel
does anyone know how to fix this please

Re: Sender from contextmenustrip

Posted: Sat Feb 09, 2013 10:25 pm
by Dummy1912
hi bud,

well maybe try something like this
maybe you have to play little with this code
because i toke this from my hmm file :)
maybe this can help you a little.

goodluck
Code: Select all
Private Sub RemoveImageToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RemoveImageToolStripMenuItem.Click

		Dim menu As New ToolStripMenuItem
                menu.Text = "Testing"
                menu.Name = "Testing"
                panel1.controls.Add(menu) : AddHandler menu.Click, AddressOf RemoveImageToolStripMenuItem_DropDownItemClicked
End Sub
	
					
					
	Private Sub RemoveImageToolStripMenuItem_DropDownItemClicked(ByVal sender As Object, ByVal e As EventArgs)
        Dim clicked = CType(sender, ToolStripMenuItem)
        Dim Program As String = clicked.Name
      If e.Button = MouseButtons.Right Then
            Panel1.Controls.Remove(program)
        End If
    End Sub	

Re: Sender from contextmenustrip

Posted: Sat Feb 09, 2013 10:43 pm
by muttley1968
nope that dont work sory its same as mine just dont do anything

Re: Sender from contextmenustrip

Posted: Sun Feb 10, 2013 12:16 am
by Scottie1972
muttley1968 wrote:
Hi i am attempting to make a context menu with a few controls one of wich removes controls from
a panel that are added by a users not the program so i attempted to use this code
Code: Select all
    Private Sub RemoveImageToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RemoveImageToolStripMenuItem.Click
        MsgBox("image Removed")
    End Sub

    Private Sub RemoveImageToolStripMenuItem_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles RemoveImageToolStripMenuItem.MouseUp
        If e.Button = MouseButtons.Right Then
            Panel1.Controls.Remove(sender)
        End If
    End Sub
But it does not work the control remains active inside of the panel
does anyone know how to fix this please

"sender" will always refer to he control you just clicked or hovered over or whatever.
so i your case the "sender" is the ( RemoveImageToolStripMenuItem )

The easiest way is 1 line of code.
Code: Select all
   Me.Controls.ReMoveByKey("control_name")
the "control_name" is just that. the Control.Name property for that control.
only thing is ReMoveByKey("") looks for a String() value, after all, that is what the Control.Name property really is.
just a simple string value.


easy right?
here i made an example for you.

WindowsApplication1.zip
this example uses a button to do the very same thing.
WindowsApplication1 (2).zip

Re: Sender from contextmenustrip

Posted: Sun Feb 10, 2013 12:25 am
by muttley1968
Allow me to show you the hole code :P

The user will press the button called Add image and it will run this code (they can press and add as many as they want)
Code: Select all
   Dim openfiledialog1 As New OpenFileDialog
        openfiledialog1.FileName = "Select a Image file"
        openfiledialog1.Filter = "All Type Of Image Files|*.*|Joint Photographic Experts Group [JPEG]|*.jpg|Bitmap [BMP|*.bmp|Tagged Image File Format [TIFF]|*.tiff|Portable Network Graphics [PNG]|*.png"
        If (openfiledialog1.ShowDialog() = Windows.Forms.DialogResult.OK) Then
            Dim PB As New PictureBox
            PB.Size = New Size(100, 100)
            PB.ContextMenuStrip = BackTrackContextMenuStrip1
            PB.SizeMode = PictureBoxSizeMode.StretchImage
            PB.BringToFront()
            PB.Image = System.Drawing.Image.FromFile(openfiledialog1.FileName)
            PB.BackColor = Color.Transparent
            AddHandler PB.MouseMove, AddressOf PictureBox_MouseMove
            AddHandler PB.MouseDown, AddressOf PictureBox_MouseDown
            Panel1.Controls.Add(PB)
        End If

this is the handlers for the above code
Code: Select all
    Dim P As New Point
    Private Sub PictureBox_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs)
        P = e.Location
    End Sub

    Private Sub PictureBox_MouseMove(ByVal sender As Object, ByVal e As MouseEventArgs)
        If e.Button = MouseButtons.Left Then
            sender.Location = New Point(sender.Location.X + (e.X - P.X), sender.Location.Y + (e.Y - P.Y))
        End If
    End Sub

Now what i want is to be able to remove the image which i can do on a right click event using this code
Code: Select all
    Private Sub Picturebox_MouseUp(ByVal sender As Object, ByVal e As MouseEventArgs)
        If e.Button = MouseButtons.Right Then
            Panel1.Controls.Remove(sender)
        End If
    End Sub
But i need to do it Via a context menu As the right click will show the menu which will have the option to make the picture biger or smaller and also some effects that you can apply to the image

Re: Sender from contextmenustrip

Posted: Sun Feb 10, 2013 12:41 am
by Scottie1972
muttley1968 wrote:
Allow me to show you the hole code :P

The user will press the button called Add image and it will run this code (they can press and add as many as they want)
Code: Select all
   Dim openfiledialog1 As New OpenFileDialog
        openfiledialog1.FileName = "Select a Image file"
        openfiledialog1.Filter = "All Type Of Image Files|*.*|Joint Photographic Experts Group [JPEG]|*.jpg|Bitmap [BMP|*.bmp|Tagged Image File Format [TIFF]|*.tiff|Portable Network Graphics [PNG]|*.png"
        If (openfiledialog1.ShowDialog() = Windows.Forms.DialogResult.OK) Then
            Dim PB As New PictureBox
            PB.Size = New Size(100, 100)
            PB.ContextMenuStrip = BackTrackContextMenuStrip1
            PB.SizeMode = PictureBoxSizeMode.StretchImage
            PB.BringToFront()
            PB.Image = System.Drawing.Image.FromFile(openfiledialog1.FileName)
            PB.BackColor = Color.Transparent
            AddHandler PB.MouseMove, AddressOf PictureBox_MouseMove
            AddHandler PB.MouseDown, AddressOf PictureBox_MouseDown
            Panel1.Controls.Add(PB)
        End If

this is the handlers for the above code
Code: Select all
    Dim P As New Point
    Private Sub PictureBox_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs)
        P = e.Location
    End Sub

    Private Sub PictureBox_MouseMove(ByVal sender As Object, ByVal e As MouseEventArgs)
        If e.Button = MouseButtons.Left Then
            sender.Location = New Point(sender.Location.X + (e.X - P.X), sender.Location.Y + (e.Y - P.Y))
        End If
    End Sub

Now what i want is to be able to remove the image which i can do on a right click event using this code
Code: Select all
    Private Sub Picturebox_MouseUp(ByVal sender As Object, ByVal e As MouseEventArgs)
        If e.Button = MouseButtons.Right Then
            Panel1.Controls.Remove(sender)
        End If
    End Sub
But i need to do it Via a context menu As the right click will show the menu which will have the option to make the picture biger or smaller and also some effects that you can apply to the image



ok first off what are these for
Code: Select all
AddHandler PB.MouseMove, AddressOf PictureBox_MouseMove
AddHandler PB.MouseDown, AddressOf PictureBox_MouseDown
they seem to only get the PictureBox's Location.
btw, will do you no go for what you want todo. i would remove them.


2nd, Why are you doing it in a mouse event?
Code: Select all
   
 Private Sub Picturebox_MouseUp(ByVal sender As Object, ByVal e As MouseEventArgs)
        If e.Button = MouseButtons.Right Then
            Panel1.Controls.Remove(sender)
        End If
    End Sub
why dont you just add the code i gave you above to a "Remove" item within the ContextMemnu.

Re: Sender from contextmenustrip

Posted: Sun Feb 10, 2013 12:51 am
by muttley1968
the handlers are for moving the picturebox so if you left click it it will the move :P

And the code you Gave will only work if i know the name of the picturebox which i will not

Re: Sender from contextmenustrip

Posted: Sun Feb 10, 2013 1:02 am
by Scottie1972
muttley1968 wrote:
the handlers are for moving the picturebox so if you left click it it will the move :P

And the code you Gave will only work if i know the name of the picturebox which i will not

then give the PictureBox a name.

you have Dim pb as New PictureBox

just add pb.Name = "some_name"

Re: Sender from contextmenustrip

Posted: Sun Feb 10, 2013 1:23 am
by muttley1968
Wont that apply the same name to all the pictureboxes :(
https://dl.dropbox.com/u/44654220/Alexandru%20Demo.zip
Here look click on the one that says Collarge maker and then add a few pics maybe it will help u see what i mean

Re: Sender from contextmenustrip

Posted: Sun Feb 10, 2013 1:36 am
by Scottie1972
ok try this,
add this to your Form
Dim pbNum as integer = 0

now in you button where you add a picturebox add this
pb.Name = "PictureBox" & pbNum.ToString()

now your picture is named.

in your mouse_down event add this
RemoveImageMenuItem.Tag = sender.name

this will add the PictureBoxXX name to the RemoveImageMenuItem.

then in the remove code,
dim pbname as string = RemoveImageMenuItem.Tag
Panel1.Controls.RemoveByKey(pbname)


see if that helps.