Sender from contextmenustrip
If you need help with a project or need to know how to do something specific in VB.NET then please ask your questions in here.
Forum rules
Please LOCK your topics once you have found the solution to your question so we know you no longer require help with your query.
Please LOCK your topics once you have found the solution to your question so we know you no longer require help with your query.
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
does anyone know how to fix this please
a panel that are added by a users not the program so i attempted to use this code
Code: Select all
But it does not work the control remains active inside of the panel 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
does anyone know how to fix this please
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
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
visit us on:
http://www.softpedia.com/get/System/Lau ... -Run.shtml
Check it out ! http://www.softpedia.com/publisher/I-A- ... 90017.html
Check it out ! http://www.softpedia.com/get/Desktop-En ... lock.shtml
http://www.softpedia.com/get/System/Lau ... -Run.shtml
Check it out ! http://www.softpedia.com/publisher/I-A- ... 90017.html
Check it out ! http://www.softpedia.com/get/Desktop-En ... lock.shtml
nope that dont work sory its same as mine just dont do anything
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 allBut it does not work the control remains active inside of the panelPrivate 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
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
the "control_name" is just that. the Control.Name property for that control. Me.Controls.ReMoveByKey("control_name")
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.
this example uses a button to do the very same thing.
You do not have the required permissions to view the files attached to this post.
Last edited by Scottie1972 on Sun Feb 10, 2013 12:29 am, edited 1 time in total.
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)
this is the handlers for the above code
Now what i want is to be able to remove the image which i can do on a right click event using this code
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
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 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
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 allDim 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 codeCode: Select allDim 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 codeCode: Select allBut 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 imagePrivate 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
ok first off what are these for
Code: Select all
they seem to only get the PictureBox's Location.AddHandler PB.MouseMove, AddressOf PictureBox_MouseMove
AddHandler PB.MouseDown, AddressOf PictureBox_MouseDown
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
why dont you just add the code i gave you above to a "Remove" item within the ContextMemnu.
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
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
And the code you Gave will only work if i know the name of the picturebox which i will not
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"
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
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
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.
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.
Copyright Information
Copyright © Codenstuff.com 2020 - 2023