I have an application where that does the same thing. The solution that I
used was to subclass the TreeView control and add a property to determine
if I want to display the context menu. Here's the code for the subclassed
TreeView:
public class SpecializedTreeView : TreeView
{
private const int WM_CONTEXTMENU = 0x007B;
private bool contextMenuEnabled;
public SpecializedTreeView()
{
this.contextMenuEnabled = true;
}
/// <summary>
/// Gets or sets a value to enable or disable the displaying of the
context menu for this control.
/// </summary>
public bool EnableContextMenu
{
get
{
return this.contextMenuEnabled;
}
set
{
this.contextMenuEnabled = value;
}
}
protected override void WndProc(ref Message m)
{
switch (m.Msg)
{
case WM_CONTEXTMENU:
if (!this.contextMenuEnabled)
return;
break;
}
base.WndProc(ref m);
}
}
On the MouseDown event for the SpecializedTreeView control I check if a
node was right-clicked:
// get the node that was clicked
TreeNode selectedNode = this.specializedTreeView.GetNodeAt(e.X, e.Y);
if(selectedNode != null && e.Button == MouseButtons.Right)
{
this.specializedTreeView.EnableContextMenu = true;
}
else
{
this.specializedTreeView.EnableContextMenu = false;
}
This way the context menu is only displayed if a node is right-clicked.
hth
-Joel
--------------------
From: "Dean L. Howen" <co*****@hotpop.com>
References: <#V**************@TK2MSFTNGP09.phx.gbl>
<ep**************@TK2MSFTNGP09.phx.gbl>Subject: Re: context menu
Date: Fri, 5 Nov 2004 03:13:57 +0700
Lines: 33
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1106
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1106
Message-ID: <#v**************@tk2msftngp13.phx.gbl>
Newsgroups: microsoft.public.dotnet.languages.csharp
NNTP-Posting-Host: ci76-243.netnam.vn 203.162.76.243
Path:
cpmsftngxa10.phx.gbl!TK2MSFTFEED01.phx.gbl!TK2MSFT NGP08.phx.gbl!tk2msftngp13
.phx.gblXref: cpmsftngxa10.phx.gbl microsoft.public.dotnet.languages.csharp:284561
X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
Yeah, I could.
But I want sometime the context menu disappears (I could find the support
method).
Why?
For example, I want the context menu work on TreeNode rather than TreeView,
but it seems for me to attach the context menu to TreeView.
What I'm doing is determine if Right-click clicks on the node, and disable
all the menu items in context menu. So it's better if context menu not show
in this situation
----------------
"Champika Nirosh" <te**@test.lk> wrote in message
news:ep**************@TK2MSFTNGP09.phx.gbl... Yes you can...
I guess you can take two path..
One is attaching the context menu to the Control using the "ContextMenu"
property of the control or else.. you can create a object of the Context
menu and use the .Show method..
Nirosh.
"Dean L. Howen" <co*****@hotpop.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl... > Dear friends,
> Could we determine when context menu should appear?
>
>
This reply is provided AS IS, without warranty (express or implied).