Results 1 to 7 of 7

Thread: [RESOLVED] pictureboxes and right click menu

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Feb 2012
    Posts
    616

    Resolved [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.

  2. #2
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    Re: pictureboxes and right click menu

    Quote Originally Posted by Goshx View Post
    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

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Feb 2012
    Posts
    616

    Re: pictureboxes and right click menu

    Quote Originally Posted by Edgemeal View Post
    I think you're looking for SourceControl?, something like,...
    [CODE]

    ContextMenuStrip.SourceControl Property

    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.

  5. #5
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    Re: pictureboxes and right click menu

    Quote Originally Posted by Goshx View Post
    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.

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Feb 2012
    Posts
    616

    Re: pictureboxes and right click menu

    Quote Originally Posted by Edgemeal View Post
    Last guess, good luck
    But good one. It works fine. Thank you!

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Feb 2012
    Posts
    616

    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
  •  



Click Here to Expand Forum to Full Width