c# winforms context menu events problem - c#

I added a context menu (add, cancel) to tree view dynamically. Now I want to show the selected tree node value when I click on context menu item click.
How can I do that?

I assume you want to know which node was right-clicked when the context menu is opened?
To determine this you can handle the mousedown event on the treeview and ensure the node you right-clicked is selected before the context menu is displayed.
private void treeView1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
var node = treeView1.HitTest(e.X, e.Y).Node;
treeView1.SelectedNode = node;
}
}
In the ToolStripMenuItem's click handler you can check treeView1.SelectedNode, it will be null if the user right clicked the treeview outside a node.
private void addToolStripMenuItem_Click(object sender, EventArgs e)
{
if (treeView1.SelectedNode != null) MessageBox.Show("Node selected: " + treeView1.SelectedNode.Text);
}

I assume you just need to know the text of the treenode? This code should do the job
string treeNodeText = this.treeView1.SelectedNode.Text;

Related

Why in every right click, the context menustrip pop up,how to enable it in a specify position by controlling the right click event?

Why in every right click, the contextmenustrip pop up,how to enable it in a specify position by controlling the right click event??
Try to remove the default contextmenustrip and create a new one.
With HitTest on a control you can check if the clicked position is on a element
private void MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Right)
{
if (dataGrid.HitTest(e.X, e.Y).Type == DataGridViewHitTestType.ColumnHeader)
{
new ContextMenuStrip().Show(dataGrid, e.Location);
}
}
}
See: How do I correctly position a Context Menu when I right click a DataGridView's column header?

toolstripmenu handle drop down

I have toolstrip menu item with some other toolstrip menu items under it. How can i handle first toolstrip menu (parent) for not opening and showing children if some condition is met.
So i have:
Documents: (parent toolstrip item)
----Document1 (child)
----Document2 (child)
----DOcument3 (child)
and on documentsToolStripMenuItem_Click(object sender, EventArgs e) i have
if(CurrentUser.HasPermission(0001))
{
MessageBox.Show("You do not have permission to access this module!");
//Here i need to prevent showing children of this parent
}
So to sum up, user need permission to press parent toolstrip menu item (documents) and when he press it and doesn't have premission i do not want to let him see what is under that parent (it's children).
I know i can make that toolstripmenu hidden or enabled = false but i have other things because of what it needs to be clickable.
You would have to use the DropDownOpening event:
void parentItem_DropDownOpening(object sender, EventArgs e) {
allowedItem.Visible = true;
notAllowedItem.Visible = false;
}

how do i restrict not to select parent node in WinForm TreeView

In winform I want to restrict only select child nodes not to select only parent node.
I tried like following checked parent click and show message box.
TreeNode node = treeView.SelectedNode;
while (node.Parent != null)
{
node = node.Parent;
}
A
+-B
+-C
Is there any other way to do so?
I want user able to select B , C but When He select A i want to show popup like please select child node
Why 'winforms-to-web' and 'node.js' tags?
On a c# Winform, maybe something like this, handling Treeview's AfterSelect event:
private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
{
if (e.Node.Parent == null && e.Node.Nodes.Count>0){
MessageBox.Show("only child nodes must be selected", "warning");
treeView1.SelectedNode=e.Node.Nodes[0];
}
}
And to avoid an initial message you can do this
private void Form1_Load(object sender, EventArgs e)
{
//tree load
treeView1.SelectedNode = treeView1.Nodes[0].Nodes[0];
treeView1.ExpandAll();
}
Or assign the handler after the tree load through code.

TreeNode click vs TreeView click

I have a TreeView and I need two things.
A Right-Click support if I click on a specific node.
A Right-Click support if I click anywhere else on the tree (where there is no nodes).
The two options would both give me a different ContextMenuStrip.
My two program now supports both type of clicks like this.
Specific Node click :
var someNode = e.Node.Tag as SomeNode;
if (someNode != null)
{
someContextMenu.Show(someTree, e.Location);
return;
}
Anywhere on the tree click :
The problem is that the Anywhere on the tree click event will fire before checking if I clicked on the node or on a blank spot from the TreeView.
Any Idea how I could change that behaviour ?
Assuming you are asking about winforms.
You can use the TreeView.HitTest method which return a TreeViewHitTestInfo where you can know if you hit a node or not.
private void treeView1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Right)
{
TreeViewHitTestInfo info = treeView1.HitTest(e.Location);
treeView1.SelectedNode = info.Node;
if (info.Node == null)
{
contextMenuStrip1.Show(Cursor.Position);
}
else
{
contextMenuStrip2.Show(Cursor.Position);
}
}
}
Or the mouse up event depending on your needs. Also you can use GetNodeAt(e.Location) instead of the TreeViewHitTestInfo class.

Right click select on .Net TreeNode

I am trying to show a popup menu on my treeview when users right click - allowing them to choose context sensitive actions to apply against the selected node.
At the moment the user has to left click node and then right click to choose.
Is it possible to make a right click on a node select that node - and if so what is the best method to do this.
Both left and right clicks fire a click event and cause the selection to change. However, in certain circumstances (that I haven't yet bothered to trace down) the selection will change from the node that was right clicked to the originally selected node.
In order to make sure that the right click changes the selection, you can forcibly change the selected node by using the MouseDown event:
treeView.MouseDown += (sender, args) =>
treeView.SelectedNode = treeView.GetNodeAt(args.X, args.Y);
A little better, as one of the other posters pointed out, is to use the NodeMouseClick event:
treeView.NodeMouseClick += (sender, args) => treeView.SelectedNode = args.Node;
yes. Here is processing for NodeMouseClick event:
private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
{
treeView1.SelectedNode = e.Node;
}
Drag a context menu strip onto the form then:
private void treeView1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
// Display context menu for eg:
ContextMenu1.Show();
}
}

Categories