|
-
May 18th, 2013, 09:37 AM
#1
Thread Starter
Fanatic Member
[RESOLVED] pictureboxes and right click menu
We have a situation like this:
1. 4 picturebox controls
2. I want to add or remove picture from picturebux using context meny
3. Contextmenu have to be done with code (no control)
Code:
Dim ctxmnu As New ContextMenu
Dim item1 As New MenuItem("Add Picture")
Dim item2 As New MenuItem("Remove")
AddHandler item1.Click, AddressOf ContextMenuHandler
AddHandler item2.Click, AddressOf ContextMenuHandler
ctxmnu.MenuItems.Add(i1)
ctxmnu.MenuItems.Add(i2)
What we have to do that our program konws which picturebox we want to fill opening fileopen dalog?
Code:
Private Sub InputPicture()
If OpenFileDialog1.ShowDialog(Me) = DialogResult.OK Then
PictureBox1.Image = Image.FromFile(OpenFileDialog1.FileName)
PictureBox1.Tag = OpenFileDialog1.FileName
Else
OpenFileDialog1.Tag = ""
End If
End Sub
I've tried many things to send an argument via InputPicture() which should point to the picturebox we need but without success.
Last edited by Goshx; May 18th, 2013 at 09:45 AM.
-
May 18th, 2013, 10:03 AM
#2
Re: pictureboxes and right click menu
 Originally Posted by Goshx
I've tried many things to send an argument via InputPicture() which should point to the picturebox we need but without success.
I think you're looking for SourceControl?, something like,...
Code:
Public Class Form1
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
PictureBox1.ContextMenuStrip = Me.ContextMenuStrip1
PictureBox2.ContextMenuStrip = Me.ContextMenuStrip1
End Sub
' context menu item clicked -
Private Sub ToolStripMenuItem1_Click(sender As System.Object, e As System.EventArgs) Handles ToolStripMenuItem1.Click
Dim c As Control = DirectCast(DirectCast(sender, ToolStripMenuItem).Owner, ContextMenuStrip).SourceControl
Dim picBox = DirectCast(c, PictureBox)
InputPicture(picBox)
End Sub
Private Sub InputPicture(p As PictureBox)
Using ofd As New OpenFileDialog
If ofd.ShowDialog = Windows.Forms.DialogResult.OK Then
p.Image = Image.FromFile(ofd.FileName)
p.Tag = ofd.FileName
End If
End Using
End Sub
End Class
ContextMenuStrip.SourceControl Property
-
May 18th, 2013, 10:06 AM
#3
Re: pictureboxes and right click menu
Firstly, I would ask why the menu has to be created in code. Unless this is a specific learning exercise that requires then I can't see why you would do so. Secondly, if you're using .NET 2.0 or later (VB 2005 or later) then you should be using the newer ContextMenuStrip class rather then the old ContextMenu.
Anyway, whether you use a ContextMenu or ContextMenuStrip, there is a SourceControl property that will provide a reference to the control that the menu was last displayed against.
-
May 18th, 2013, 10:14 AM
#4
Thread Starter
Fanatic Member
Re: pictureboxes and right click menu
 Originally Posted by Edgemeal
No... on the contrary!
It must be done via coding (there's a reason for that).
I know how it is possible to do that via ContextMenuStrip but in this case I have to solve that on this way.
For an experienced programmer, it is almost the same thing, I think.
-
May 18th, 2013, 11:19 AM
#5
Re: pictureboxes and right click menu
 Originally Posted by Goshx
No... on the contrary!
It must be done via coding (there's a reason for that).
I know how it is possible to do that via ContextMenuStrip but in this case I have to solve that on this way.
For an experienced programmer, it is almost the same thing, I think.
Last guess, good luck 
Code:
Public Class Form1
Dim ctxmnu As New ContextMenu
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
' add menu items
Dim item1 As New MenuItem("Add Picture")
Dim item2 As New MenuItem("Remove")
ctxmnu.MenuItems.Add(item1)
ctxmnu.MenuItems.Add(item2)
' add handlers
AddHandler item1.Click, AddressOf Menu_Items_Click
AddHandler item2.Click, AddressOf Menu_Items_Click
' set context menu
PictureBox1.ContextMenu = ctxmnu
PictureBox2.ContextMenu = ctxmnu
End Sub
Private Sub Menu_Items_Click(sender As Object, e As EventArgs)
Dim mnu As MenuItem = DirectCast(sender, MenuItem)
If mnu.Text = "Add Picture" Then
InputPicture(DirectCast(ctxmnu.SourceControl, PictureBox))
ElseIf mnu.Text = "Remove" Then
DirectCast(ctxmnu.SourceControl, PictureBox).Image = Nothing
End If
End Sub
Private Sub InputPicture(p As PictureBox)
Using ofd As New OpenFileDialog
If ofd.ShowDialog = Windows.Forms.DialogResult.OK Then
p.Image = Image.FromFile(ofd.FileName)
p.Tag = ofd.FileName
End If
End Using
End Sub
End Class
ContextMenu.SourceControl Property
Last edited by Edgemeal; May 18th, 2013 at 11:27 AM.
-
May 18th, 2013, 01:08 PM
#6
Thread Starter
Fanatic Member
Re: pictureboxes and right click menu
 Originally Posted by Edgemeal
Last guess, good luck 
But good one. It works fine. Thank you!
-
May 19th, 2013, 04:15 PM
#7
Thread Starter
Fanatic Member
Re: [RESOLVED] pictureboxes and right click menu
Another one question, closely related wit the previous topic.
If we have:
Code:
Private Sub Menu_Items_Click(sender As Object, e As EventArgs)
Dim mnu As MenuItem = DirectCast(sender, MenuItem)
If mnu.Text = "Add Picture" Then
InputPicture(DirectCast(ctxmnu.SourceControl, PictureBox))
ElseIf mnu.Text = "Remove" Then
DirectCast(ctxmnu.SourceControl, PictureBox).Image = Nothing
' go from here to delete image from the selected picturebox into database
End If
End Sub
In the case that I want to remove selected picture from database, how I could know which picture to delete? I put Tag value from 1-4 for every picturebox on the form but I don't know how to transfer that value to a SQL query. (Previously I added a new database column which keeps Tag value)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|