Hide Contextmenu in treeview

N

Nickneem

I have a treeview with which I want to use a context menu for certain
nodes to fill a memo property of the node.
So when I add the nodes I set a property 'ItemNode' to either true or
false.
If the ItemNode is true I want the contextmenu to pop-up if not I want
to prevent it from popping up.

Here's my code which won't work...

Private Sub ContextMenu1_Popup(ByVal sender As Object, ByVal e As
System.EventArgs) Handles ContextMenu1.Popup
Dim rClickedNode As InheritTreeNode
Try
rClickedNode = CType(treeMenu.SelectedNode,
InheritTreeNode)
If rClickedNode.ItemNode = False Then
MsgBox("Memo's only available for items!")
Me.Focus()
Dim hWnd As IntPtr = GetFocus()
SendMessage(hWnd, WM_LBUTTONDOWN, 0, 0)
SendMessage(hWnd, WM_LBUTTONUP, 0, 0)
End If
Catch ex As Exception

End Try
End Sub

Thanks in advance,

Mike
 
G

Guest

The approach I used to solve a similar problem (picking one of three context
menus to display) was to create a class that inherited from TreeView and put
this code in that class.

Public Event ContextChanged(ByVal sender As Object, ByVal e As
System.Windows.Forms.TreeViewEventArgs)

Protected Overrides Sub OnMouseDown(ByVal e As
System.Windows.Forms.MouseEventArgs)

MyBase.OnMouseDown(e)

' A right mouse down doesn't seem to cause a before select message.
' So we intercept it here and do it ourself.
Try
If (e.Button = MouseButtons.Left Or e.Button =
MouseButtons.Right) Then

Dim oNode As TreeNode
oNode = Me.GetNodeAt(e.X, e.Y)
Me.SelectedNode = oNode

Dim oEventArgs As New
System.Windows.Forms.TreeViewEventArgs(oNode, TreeViewAction.ByMouse)
RaiseEvent ContextChanged(Me, oEventArgs)
End If

Catch oEx As System.Exception
WriteEx(oEx)
End Try

End Sub

Then in the context changed event in the Form I put the code I needed to
determine which menu to show.

I think that is all I needed to to fix this particular problem (but for
other things it ended up a big class so I can't be sure).

Hope this helps

Gav
 
N

Nickneem

Thanks Gav, it seems to work in my win app.
I'm writing the same thing for the compact framework too but I'm stuck
there since there's no GetNodeAt Function but more important there's no
MouseDown event when I 'Tap and Hold'.

Kind regards,
Mike
 
G

Guest

Hi Mike,

For compact framework to enable/disble the context menu for a tree view
following this step:

1. Add a context menu to form, drag and drop the context menu from the tool
box to the form.

2. Add a handler for AfterSelect event, for example:
this.treeView.AfterSelect += new
System.Windows.Forms.TreeViewEventHandler(this.treeView_AfterSelect);

3. Inside the callback set the context menu based on your logic, for example:

private void treeView_AfterSelect(object sender,
System.Windows.Forms.TreeViewEventArgs e)
{
if ( e.Node.Text.Equals("Documents & Settings") )
{
this.treeView.ContextMenu = this.contextMenu;
}
else
{
this.treeView.ContextMenu = null;
}
}

In this example the only the node has text equals to "Documents & Settings"
will have a context menu, other one will not.

Hope this help,

Dinhduy Tran
[CSDP/MCSD]
 
N

Nickneem

Thanks Dinhduy, works like a charm!

To make things complete, here's most of the code:

Public Sub New()
MyBase.New()

'This call is required by the Windows Form Designer.
InitializeComponent()
AddHandler treeMenu.AfterSelect, New
System.windows.forms.TreeViewEventHandler(AddressOf
treeView_AfterSelect)
End Sub

#Region "ContextMenu"
Private Sub mnuMemo_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles mnuMemo.Click
'Right Click / Tap and hold = memo meegeven
Dim rClickedNode As New InheritTreeNode
Dim MemoString As String
Try
rClickedNode = CType(treeMenu.SelectedNode,
InheritTreeNode)
If rClickedNode.ItemNode = True Then
InputPanel1.Enabled = True
MemoString = InputBox("Memo?", "DigiWaiter PDA")
If MemoString <> "" Then 'Alleen als er wat ingegeven
is
rClickedNode.notitie = MemoString
End If
InputPanel1.Enabled = False
End If
Catch ex As Exception
End Try
End Sub
Private Sub treeView_AfterSelect(ByVal sender As Object, ByVal e As
System.Windows.Forms.TreeViewEventArgs)
Dim rClickedNode As New InheritTreeNode
Try
rClickedNode = CType(e.Node, InheritTreeNode)
If rClickedNode.ItemNode.Equals(True) Then
Me.treeMenu.ContextMenu = Me.ContextMenu1
Else
Me.treeMenu.ContextMenu = Nothing
End If
Catch ex As Exception
End Try
End Sub
#Region "ContextMenu"

Public Class InheritTreeNode
Inherits TreeNode
' The Name property uniquely identifies the user connection.
Private _notitie As String
Private _ItemNode As Boolean
Public Property notitie() As String
Get
Return _notitie
End Get
Set(ByVal Value As String)
_notitie = Value
End Set
End Property
Public Property ItemNode() As Boolean
Get
Return _ItemNode
End Get
Set(ByVal Value As Boolean)
_ItemNode = Value
End Set
End Property
End Class

Thanks for your help guys!
Regards,

Michael
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top