Results 1 to 7 of 7

Thread: [RESOLVED] pictureboxes and right click menu

Threaded View

  1. #4
    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.

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