Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

context menu in toolbar 1

Status
Not open for further replies.

NICKYSUWANDI

Programmer
Jan 8, 2004
80
ID
hi guys,

i had write code to create toolbar with contextmenu inside the toolbar (using coding, toolbar and name reference from my treenode),like this :

-------------------
Dim Tnode As TreeNode, intno As Int16 = 0
For Each Tnode In e.Node.Nodes
TBar1.Buttons.Add(Tnode.Text)
TBar1.Buttons(intno).ToolTipText = Tnode.Text

Dim ncntnd As Integer = Tnode.GetNodeCount(True)
Dim Tnode1 As TreeNode
If ncntnd > 0 Then
'TBar1.DropDownArrows = False
TBar1.Buttons(intno).Style = ToolBarButtonStyle.DropDownButton
Dim cm As New ContextMenu
For Each Tnode1 In Tnode.Nodes
cm.MenuItems.Add(Tnode1.Text)
Next
TBar1.Buttons(intno).DropDownMenu = cm
End If
intno += 1
Next
---------

my problem is how i want know which menu has been clicked in context menu, please help and how to block the code in this web.

Thanks

Nicky
 
You have to add a handler to each menuitem you create. I would suggest changing your own code to something similar like this:

Code:
''
  Dim tNode As TreeNode
  Dim tButton As ToolBarButton
  Dim intNo As Int16

  For Each tNode In e.Node.Nodes
    tButton = TBar1.Buttons.Add(tNode.Text)
    tButton.ToolTipText = tNode.Text

    Dim ncntnd As Integer = tNode.GetNodeCount(True)

    If ncntnd > 0 Then
      Dim tNode1 As TreeNode
      Dim cMenu As New ContextMenu
      Dim cMenuItem As MenuItem

      TBar1.DropDownArrows = False
      tButton.Style = ToolBarButtonStyle.DropDownButton
        
      For Each tNode1 In tNode.Nodes
        cMenuItem = cMenu.MenuItems.Add(tNode1.Text)
        AddHandler cMenuItem.Click, AddressOf MenuClicked
      Next
      tButton.DropDownMenu = cMenu
    End If
  Next
''

Private Sub MenuClicked(ByVal sender As Object, ByVal e As EventArgs)
  'Here you can use the sender.Text property to see which menuitem was clicked.
End Sub

A couple of suggestions:

- try naming you variables more clearly, like cMenu instead of cm. This wil prove helpful if you have to alter the code months after you wrote it and still want to understand it.
- declare variables only when you need them. For example, you declared Tnode1 As TreeNode before the If ... End If block, when you only need it inside that block. This uses unnecessary memory.
- always try to assign a newly created control to an object. For example: cMenuItem = cMenu.MenuItems.Add("hello world"). This way, you can use the cMenuItem variable to set all properties of the newly added item without the use of arbitrary counters.

I hope this helps you a bit along the way.

Regards, Ruffnekk
---
Is it my imagination or do buffalo wings taste just like chicken?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top