none
如何針對MenuStrip下的項目設計滑鼠右鍵快顯功能表? RRS feed

  • 問題

  • 請問各位前輩~
    要如何在MenuStrip下的每個項目滑鼠按右鍵時都會出現快顯功能表呢?
    我知道如果是整個MenuStrip按右鍵的內容功能表是設定在 ContentMenuStrip屬性上
    但是我是想設計在每個項目上,可以做得到嗎?觸發的事件又是什麼呢?
    麻煩大大們幫我解答一下,謝謝^^
    2007年7月10日 上午 09:01

解答

  • Hi,

    我把程式碼改成這樣,看看是不是會比較合理、順暢一點

    Code Snippet

    Dim Mnu1, Mnu2, SubMnu1, SubMnu2 As ToolStripMenuItem

        Dim ItemName As String

     

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

            ''宣告功能表大項

            Mnu1 = New ToolStripMenuItem("MNU1")

            Mnu1.Name = "Mnu1"

            ''宣告功能表大項

            Mnu2 = New ToolStripMenuItem("MNU2")

            Mnu2.Name = "Mnu2"

            ''宣告功能表子項

            SubMnu1 = New ToolStripMenuItem("SubMnu1")

            SubMnu1.Name = "SubMnu1"

            SubMnu2 = New ToolStripMenuItem("SubMnu2")

            SubMnu2.Name = "SubMnu2"

            ''掛載事件

            AddHandler Mnu1.MouseUp, AddressOf ItemMouseUp

            AddHandler Mnu2.MouseUp, AddressOf ItemMouseUp

            AddHandler SubMnu1.MouseUp, AddressOf ItemMouseUp

            AddHandler SubMnu2.MouseUp, AddressOf ItemMouseUp

     

            AddHandler Mnu1.MouseEnter, AddressOf ItemMouseEnter

            AddHandler Mnu2.MouseEnter, AddressOf ItemMouseEnter

            AddHandler SubMnu1.MouseEnter, AddressOf ItemMouseEnter

            AddHandler SubMnu2.MouseEnter, AddressOf ItemMouseEnter

            ''將相關項目加到Form

            Mnu1.DropDownItems.Add(SubMnu1)

            Mnu2.DropDownItems.Add(SubMnu2)

            Me.MenuStrip1.Items.Add(Mnu1)

            Me.MenuStrip1.Items.Add(Mnu2)

        End Sub

     

        Private Sub ItemMouseUp(ByVal sender As System.Object, ByVal e As Windows.Forms.MouseEventArgs)

            If e.Button = Windows.Forms.MouseButtons.Right Then

                Me.ContextMenuStrip1.AutoClose = False

                ItemName = CType(sender, ToolStripMenuItem).Name

                ContextMenuStrip1.Show(Control.MousePosition.X, Control.MousePosition.Y)

            End If

        End Sub

     

        Private Sub ItemMouseEnter(ByVal sender As System.Object, ByVal e As System.EventArgs)

            If CType(sender, ToolStripMenuItem).Name <> ItemName Then

                If ContextMenuStrip1.Visible = True Then ContextMenuStrip1.Close()

            End If

        End Sub

     

     

    2007年7月11日 下午 03:19
    版主

所有回覆

  • Hi,

    不知道您是想要什麼樣子的功能,如果在每個子項目上按右鍵都要能顯示快顯功能表(ContextMenuStrip)的話,功能表(MenuStrip)上的項目要利用程式碼來新增、掛載相關事件,您可以參考看看下面程式碼

    From上有一個MenuStrip1與ContextMenuStrip1

    Code Snippet

    Dim Mnu1, Mnu2, SubMnu1 As ToolStripMenuItem

     

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

            ''宣告功能表大項

            Mnu1 = New ToolStripMenuItem("MNU1")

            Mnu1.Name = "Mnu1"

            ''宣告功能表大項

            Mnu2 = New ToolStripMenuItem("MNU2")

            Mnu2.Name = "Mnu2"

            ''宣告功能表子項

            SubMnu1 = New ToolStripMenuItem("SubMnu1")

            SubMnu1.Name = "SubMnu1"

            ''掛載事件

            AddHandler Mnu1.MouseUp, AddressOf ItemMouseUp

            AddHandler Mnu2.MouseUp, AddressOf ItemMouseUp

            AddHandler SubMnu1.MouseUp, AddressOf ItemMouseUp

            ''將相關項目加到Form

            Mnu1.DropDownItems.Add(SubMnu1)

            Me.MenuStrip1.Items.Add(Mnu1)

            Me.MenuStrip1.Items.Add(Mnu2)

        End Sub

     

        Private Sub ItemMouseUp(ByVal sender As System.Object, ByVal e As Windows.Forms.MouseEventArgs)

            If e.Button = Windows.Forms.MouseButtons.Right Then

                ContextMenuStrip1.Show(e.X + Me.Left, e.Y + Me.Top)

            End If

        End Sub

     

    2007年7月10日 下午 12:22
    版主
  • 謝謝你提供的程式碼,我要的就是這樣子的功能。

    但是,當在功能表子項按滑鼠右鍵快顯功能表出現之後,整個功能表子項就會都縮回功能表大項中了,不能夠維持展開的狀態。

    於是我就在按滑鼠右鍵事件中加了一段程式 如下:

        Private Sub ItemMouseUp(ByVal sender As System.Object, ByVal e As Windows.Forms.MouseEventArgs)

            If e.Button = Windows.Forms.MouseButtons.Right Then

                            ' 不讓功能表子項縮回功能表中

                            Me.ContextMenuStrip1.AutoClose = False

                ContextMenuStrip1.Show(Control.MousePosition.X, Control.MousePosition.Y)

            End If

        End Sub

    雖然加了這一段程式,不讓功能表子項縮回去,但是卻造成另一種不太合理的現象。

    就是右鍵快顯功能表出現 而功能表子項又不會縮回時,滑鼠指標移到NUM2功能表大項或是其它功能表子項,

    原本所選取的子項就會跑掉了。

    請幫幫忙解答一下要如何處理~謝謝你 ~

    2007年7月11日 上午 02:19
  • Hi,

    我把程式碼改成這樣,看看是不是會比較合理、順暢一點

    Code Snippet

    Dim Mnu1, Mnu2, SubMnu1, SubMnu2 As ToolStripMenuItem

        Dim ItemName As String

     

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

            ''宣告功能表大項

            Mnu1 = New ToolStripMenuItem("MNU1")

            Mnu1.Name = "Mnu1"

            ''宣告功能表大項

            Mnu2 = New ToolStripMenuItem("MNU2")

            Mnu2.Name = "Mnu2"

            ''宣告功能表子項

            SubMnu1 = New ToolStripMenuItem("SubMnu1")

            SubMnu1.Name = "SubMnu1"

            SubMnu2 = New ToolStripMenuItem("SubMnu2")

            SubMnu2.Name = "SubMnu2"

            ''掛載事件

            AddHandler Mnu1.MouseUp, AddressOf ItemMouseUp

            AddHandler Mnu2.MouseUp, AddressOf ItemMouseUp

            AddHandler SubMnu1.MouseUp, AddressOf ItemMouseUp

            AddHandler SubMnu2.MouseUp, AddressOf ItemMouseUp

     

            AddHandler Mnu1.MouseEnter, AddressOf ItemMouseEnter

            AddHandler Mnu2.MouseEnter, AddressOf ItemMouseEnter

            AddHandler SubMnu1.MouseEnter, AddressOf ItemMouseEnter

            AddHandler SubMnu2.MouseEnter, AddressOf ItemMouseEnter

            ''將相關項目加到Form

            Mnu1.DropDownItems.Add(SubMnu1)

            Mnu2.DropDownItems.Add(SubMnu2)

            Me.MenuStrip1.Items.Add(Mnu1)

            Me.MenuStrip1.Items.Add(Mnu2)

        End Sub

     

        Private Sub ItemMouseUp(ByVal sender As System.Object, ByVal e As Windows.Forms.MouseEventArgs)

            If e.Button = Windows.Forms.MouseButtons.Right Then

                Me.ContextMenuStrip1.AutoClose = False

                ItemName = CType(sender, ToolStripMenuItem).Name

                ContextMenuStrip1.Show(Control.MousePosition.X, Control.MousePosition.Y)

            End If

        End Sub

     

        Private Sub ItemMouseEnter(ByVal sender As System.Object, ByVal e As System.EventArgs)

            If CType(sender, ToolStripMenuItem).Name <> ItemName Then

                If ContextMenuStrip1.Visible = True Then ContextMenuStrip1.Close()

            End If

        End Sub

     

     

    2007年7月11日 下午 03:19
    版主
  • 非常感謝你提供的方法~謝謝 ^^
    2007年7月12日 上午 01:07