Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm General / August 2005

Tip: Looking for answers? Try searching our database.

Hide Contextmenu in treeview

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Nickneem - 16 Aug 2005 07:09 GMT
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
GavCost - 16 Aug 2005 14:08 GMT
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

> I have a treeview with which I want to use a context menu for certain
> nodes to fill a memo property of the node.
[quoted text clipped - 26 lines]
>
> Mike
Nickneem - 16 Aug 2005 21:08 GMT
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
Dinhduy Tran - 16 Aug 2005 21:43 GMT
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]           

> 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
[quoted text clipped - 3 lines]
> Kind regards,
> Mike
Nickneem - 18 Aug 2005 20:57 GMT
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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.