none
context menus RRS feed

  • Question

  • i am trying to insert context menus in my web browser (not context menu strips because context menus look better) but can't get the to appear either by code or design here is my code below:

    Note: if you can't help me don't post anything just leave.

    Note: don't tell me to use context menu strips because they look like crap.

    Note: I am not a big techy so try to use small words as much as possible.

    code:

     

     

    If E.Button = MouseButtons.Right Then
    
    	Dim x As Integer
    
            Dim y As Integer
    
            x = Control.MousePosition().X - Me.Location.X - 5  'Adjust Here If Needed
    
            y = Control.MousePosition().Y - Me.Location.Y - 23 'Adjust Here If Needed
    
            Dim xy As New Point(x, y)
    
            ContextMenu1.Show(Me, xy)
    
    
    
    End If

     

    (i am in the mouse click event)


    • Edited by zak23 Thursday, March 24, 2011 2:36 AM
    Sunday, March 20, 2011 11:31 PM

Answers

  • Hi Zak23:

    I can think of two options for you.

    Option 1: The easy way.

     

       Private Sub Form1_Load(ByVal sender As Object, _
       ByVal e As System.EventArgs) Handles Me.Load

          WebBrowser1.ContextMenu = ContextMenu1
       End Sub

     

    Option 2: Using your code as basis and subscribing to the webbrowser's document mouse events.

     

    Public Class Form1
       Private Sub Form1_Load(ByVal sender As Object, _
          ByVal e As System.EventArgs) Handles Me.Load
          'Force a page navigation so that there is a document loaded
          WebBrowser1.Navigate("about:blank")
          AddHandler WebBrowser1.Document.MouseDown, AddressOf HTMLDOCMouseClick
       End Sub

       Sub HTMLDOCMouseClick(ByVal sender As Object, _
          ByVal e As System.Windows.Forms.HtmlElementEventArgs)

          If e.MouseButtonsPressed = MouseButtons.Right Then

                Dim x As Integer
                Dim y As Integer
                x = Control.MousePosition().X - Me.Location.X - 5  'Adjust Here If Needed
                y = Control.MousePosition().Y - Me.Location.Y - 23 'Adjust Here If Needed
                Dim xy As New Point(x, y)
                ContextMenu1.Show(Me, xy)

          End If
       End Sub

    End Class

     

    • Proposed as answer by John Anthony Oliver Monday, March 21, 2011 3:33 AM
    • Marked as answer by zak23 Thursday, March 24, 2011 11:20 PM
    Monday, March 21, 2011 3:02 AM

All replies

  • The webbrowser class doesn't support a mouse click event.  What is the signature of the method where you have placed this code?

    http://msdn.microsoft.com/en-us/library/system.windows.forms.webbrowser.aspx

    Monday, March 21, 2011 12:56 AM
  • Hi Zak23:

    I can think of two options for you.

    Option 1: The easy way.

     

       Private Sub Form1_Load(ByVal sender As Object, _
       ByVal e As System.EventArgs) Handles Me.Load

          WebBrowser1.ContextMenu = ContextMenu1
       End Sub

     

    Option 2: Using your code as basis and subscribing to the webbrowser's document mouse events.

     

    Public Class Form1
       Private Sub Form1_Load(ByVal sender As Object, _
          ByVal e As System.EventArgs) Handles Me.Load
          'Force a page navigation so that there is a document loaded
          WebBrowser1.Navigate("about:blank")
          AddHandler WebBrowser1.Document.MouseDown, AddressOf HTMLDOCMouseClick
       End Sub

       Sub HTMLDOCMouseClick(ByVal sender As Object, _
          ByVal e As System.Windows.Forms.HtmlElementEventArgs)

          If e.MouseButtonsPressed = MouseButtons.Right Then

                Dim x As Integer
                Dim y As Integer
                x = Control.MousePosition().X - Me.Location.X - 5  'Adjust Here If Needed
                y = Control.MousePosition().Y - Me.Location.Y - 23 'Adjust Here If Needed
                Dim xy As New Point(x, y)
                ContextMenu1.Show(Me, xy)

          End If
       End Sub

    End Class

     

    • Proposed as answer by John Anthony Oliver Monday, March 21, 2011 3:33 AM
    • Marked as answer by zak23 Thursday, March 24, 2011 11:20 PM
    Monday, March 21, 2011 3:02 AM
  • Hi TnTinMN,

    You could have also used

    e.MousePosition

    ContextMenu was not in my ToolBox but I've added one in code here.

    ( I now have ContextMenu and a few other items in my ToolBox. )

     

    Public Class Form1
    
     Friend WithEvents ContextMenu1 As New ContextMenu
    
     Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    
      'Force a page navigation so that there is a document loaded
      WebBrowser1.Navigate("about:blank")
      AddHandler WebBrowser1.Document.Click, AddressOf HTMLDOCMouseClick
     End Sub
    
     Sub HTMLDOCMouseClick(ByVal sender As Object, _
      ByVal e As System.Windows.Forms.HtmlElementEventArgs)
    
      ContextMenu1.Show(Me, e.MousePosition)
    
     End Sub
    
    End Class
    

     



    Regards,

    John

    Click this link to see how to insert a picture into a forum post.

    Installing VB6 on Windows 7

    Monday, March 21, 2011 3:42 AM
  • John,

    Most of that 2nd option was from the Zak23s posting.  I just changed enough to make it work the way I believe the OP intendeds it to work.  Personally, I would not do it that way but use as I had shown in option 1.  Fortunately, there are many logic paths that will produce a desired effect;  I don't like telling people that their method is wrong just because it is not how I would have approached the problem, hence I try to present options including making their code work if it is within my ability to do so.

    Monday, March 21, 2011 4:13 AM
  • "mousebuttonspressed" doesn't work

    but the firs option works like a dream

    thanks alot!

    Monday, March 21, 2011 11:53 PM