TreeView Nodes and ContextMenuStrip

G

Guest

Hi

I would like do display a custom ContextMenuStrip when I right-click on the
nodes in the TreeView. Depending on the selected TreeView node, some items in
the ContextMenuStrip must be deactivated.

How can I disable items in a ContextMenuStrip control when I right-click on
different nodes in the TreeView? Has someone a code example (C# preferred)?

Thanks and Regards,
andersch
 
L

Linda Liu [MSFT]

Hi Andersch,

Do you mean that you would like to show the same context menu strip no
matter on which tree node you right-click and just disable some items in
the context menu strip depending on the selected tree node?

If so, I think you could set the ContextMenuStrip property of the treeview
to the context menu strip and handle the Opening event of the context menu
strip, which occurs when the context menu strip is opening

The following is a sample for you. The sample requires you to set up a
Windows application project, add a TreeView control and a ContextMenuStrip
onto the form, add some nodes into the treeview and add some items into the
context menu strip.

public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.contextMenuStrip1.Opening += new
CancelEventHandler(contextMenuStrip1_Opening);
}

void contextMenuStrip1_Opening(object sender, CancelEventArgs e)
{
this.contextMenuStrip1.Items[0].Enabled = false;
this.contextMenuStrip1.Items[1].Enabled = false;
this.contextMenuStrip1.Items[2].Enabled = false;

switch (this.treeView1.SelectedNode.Index)
{
case 0 :
this.contextMenuStrip1.Items[0].Enabled = true;
break;
case 1:
this.contextMenuStrip1.Items[1].Enabled = true;
break;
case 2:
this.contextMenuStrip1.Items[2].Enabled = true;
break;
}
}
}

Build the project and run it. When you select the first tree node and
right-click on it, the context menu strip shows up and only the first item
in the context menu strip is enabled.

Hope this helps.
If the above sample isn't what you want, please feel free to let me know.


Sincerely,
Linda Liu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
G

Guest

Hi Linda

Thank you for you answer and the code example. And yes, you're right.

But I have now a second question:

How can I prevent that the ContextMenuStrip control is shown if the user
doesn't click on a TreeView/TreeView Node?

I have extended your example:

private void contextMenuStrip1_Opening(object sender,
CancelEventArgs e)
{
this.contextMenuStrip1.Items[0].Enabled = false;
this.contextMenuStrip1.Items[1].Enabled = false;
this.contextMenuStrip1.Items[3].Enabled = false;
this.contextMenuStrip1.Items[4].Enabled = false;

XmlNode xmlNode = (XmlNode)this.treeView1.SelectedNode.Tag;

switch (xmlNode.Name)
{
case "Account":
this.contextMenuStrip1.Items[4].Enabled = true;
break;
case "Category":
this.contextMenuStrip1.Items[0].Enabled = true;
this.contextMenuStrip1.Items[3].Enabled = true;

if (!xmlNode.HasChildNodes)
{
this.contextMenuStrip1.Items[4].Enabled = true;
}

break;
case "EasyCrypto.NET":
this.contextMenuStrip1.Items[1].Enabled = true;
break;
}
}

Now, when the user doesn't click on a TreeView node, en exception is thrown
at the line "XmlNode xmlNode = (XmlNode)this.treeView1.SelectedNode.Tag;".

How can I query where the user clicked?

Thanks and Regards,
andersch
Linda Liu said:
Hi Andersch,

Do you mean that you would like to show the same context menu strip no
matter on which tree node you right-click and just disable some items in
the context menu strip depending on the selected tree node?

If so, I think you could set the ContextMenuStrip property of the treeview
to the context menu strip and handle the Opening event of the context menu
strip, which occurs when the context menu strip is opening

The following is a sample for you. The sample requires you to set up a
Windows application project, add a TreeView control and a ContextMenuStrip
onto the form, add some nodes into the treeview and add some items into the
context menu strip.

public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.contextMenuStrip1.Opening += new
CancelEventHandler(contextMenuStrip1_Opening);
}

void contextMenuStrip1_Opening(object sender, CancelEventArgs e)
{
this.contextMenuStrip1.Items[0].Enabled = false;
this.contextMenuStrip1.Items[1].Enabled = false;
this.contextMenuStrip1.Items[2].Enabled = false;

switch (this.treeView1.SelectedNode.Index)
{
case 0 :
this.contextMenuStrip1.Items[0].Enabled = true;
break;
case 1:
this.contextMenuStrip1.Items[1].Enabled = true;
break;
case 2:
this.contextMenuStrip1.Items[2].Enabled = true;
break;
}
}
}

Build the project and run it. When you select the first tree node and
right-click on it, the context menu strip shows up and only the first item
in the context menu strip is enabled.

Hope this helps.
If the above sample isn't what you want, please feel free to let me know.


Sincerely,
Linda Liu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
B

Bojan Mijuskovic

How can I query where the user clicked?

Is that what you really want? To know where the user clicked? Or you
rather want to prevent the exception being thrown?

For the latter, I would suggest to wrap the XmlNode initialization (and its
usage) in an IF statement checking if the selected TreeNode is null:

if (treeView1.SelectedNode != null)
{
XmlNode xmlNode = (XmlNode)this.treeView1.SelectedNode.Tag;
...

Or maybe just simply:

if (treeView1.SelectedNode == null)
return;

to stop processing the method.


Regards,
Bojan Mijuskovic [MVP]
 
B

Bojan Mijuskovic

if (treeView1.SelectedNode == null)

In addition to the above you can also use the Cancel property of the
CancelEventArgs class:

e.Cancel = true;


Regards,
Bojan Mijuskovic [MVP]
 
L

Linda Liu [MSFT]

Hi Andersch,

Have you set the ContextMenuStrip property of the tree view to the context
menu strip? If so, the context menu strip won't appear when the user click
outside of the tree view control.

In fact, if there're tree nodes in the tree view control, one tree node
will be selected when the user clicks on the tree view control(no matter
left-click or right-click). In this case, your code won't throw any
exception.

However, if there's no tree node in the tree view control, an exception
will occur when the user right-click on the tree view control. To prevent
this error, you may add a IF statement before the line of code ' XmlNode
xmlNode = (XmlNode)this.treeView1.SelectedNode.Tag;' to check whether the
SelectedNode property of the tree view is null.

The following is a sample.

if (this.treeView1.SelectedNode != null)
{
XmlNode xmlNode = (XmlNode)this.treeView1.SelectedNode.Tag;
.....
}

Hope this helps.
If you have anything unclear, please feel free to let me know.


Sincerely,
Linda Liu
Microsoft Online Community Support
 
L

Linda Liu [MSFT]

Hi Andersch,

How about the problem now?

If the problem is not solved and you need our further assistance, please
feel free to let me know.

Thank you for using our MSDN Managed Newsgroup Support Service!


Sincerely,
Linda Liu
Microsoft Online Community Support
 
G

Guest

Hi Linda

Sorry for the delay.

The problem is now fixed with your and Bojan's help.

Thanks a lot,
andersch
 

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