I have this code:
private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
{
if (treeView1.SelectedNode.Nodes.Count == 0)
{
MessageBox.Show("The node does not have any children.");
}
}
When a treeView node which has no children is clicked, there seems to be a lag on the event firing.
For example:
I click on a parent node with children - MessageBox does not fire
I click on a child node without children - MessageBox does not fire
I click on a child node without children again - Messagebox fires
I click on a parent node with children - Messagebox fires
I click on a parent node with children again - messagebox does not fire.
During debug, the SelectedNode.Count value seems to be the number from the click before it.
What is going on here?
Your problem stems from the fact that the OnNodeMouseClick is fired before any selection-related events (OnBeforeSelect & OnAfterSelect) which means the SelectedNode you're inspecting has not been updated yet.
If you only care about the selection changing, then subscribe to BeforeSelect (with the ability to cancel the selection) or AfterSelect instead. This will handle changing selections using the keyboard as well.
Unlike the selection-related events, NodeMouseClick will still trigger even if the the selected node is not changing (e.g. you're clicking on an already-selected node). Also, as the name implies, this only works on mouse click and not when using the keyboard to navigate your tree.
To see what is actually happening in the background, you can have a look at the source code for TreeView, specifically the WmNotify method. You'll see that NodeMouseClick is triggered by a windows NM_CLICK message. It then performs a Hit Test at the clicked (x,y) coordinate to look for a node under the mouse, and if found, give it back to you inside the TreeNodeMouseClickEventArgs argument of the event.
TLDR: When subscribing to NodeMouseClick, your selection hasn't changed yet (and it might not be changing), but you can see the clicked node by inspecting the event arg. Only works when using mouse, not keyboard.
Related
I have canvas with listbox inside it.
each child element of listbox sets eventhandler for Click event.
On canvas I set eventhandlers for
ManipulationStarted="canvas_ManipulationStarted"
ManipulationDelta="canvas_ManipulationDelta"
ManipulationCompleted="canvas_ManipulationCompleted"
My code for swiping works perfect accept one thing, it fires Click eventhandler before ManipulationCompleted eventhandler.
But for example listbox in the same time scrolls perfectly and do not fire Click event.
So basically what I need is to handle manipulation events in same way listbox do.
If this condition is true:
private void canvas_ManipulationDelta(object sender, ManipulationDeltaEventArgs e)
{
e.DeltaManipulation.Translation.X > [some value]
....
}
I need to disable firing Click event on any child element of canvas, doesn't matter if it is inside listbox or not.
Why are you setting click handlers if you don't want them to fire?
Click fires on pointer pressed, so there's no way to tell if the user wanted to Click or to start a manipulation. You'll need to either decide based on the location clicked or a later event if you want to differentiate between "clicks" and swipes.
Instead of Click you can handle the Tap gesture along with the manipulation events. Since Tap fires on the pointer released the manipulation system will fire it if the user tap and releases in one spot and it will trigger the manipulations if the user presses and moves the pointer.
See How to handle manipulation events for Windows Phone 8 for more details.
I have a Devexpress.XtraTreeList component. I want to catch in click event where the user is clicked on expand button or the node? How can I understand this?
Edit: Actually I am trying to do something like outlook using treelist. When I click the node, for example inbox, the messages in inbox are shown right side on the screen. When the user click another node, the treelist must be updated because some messages may be read. I did this functions in click event. It ıs OK. But in this case expand buttons functionality does not working normally.
I found the solution..
Thanks everyone..
private void treeList1_Click(object sender, System.EventArgs e) {
DevExpress.XtraTreeList.TreeList tree = sender as DevExpress.XtraTreeList.TreeList;
DevExpress.XtraTreeList.TreeListHitInfo info = tree.CalcHitInfo(tree.PointToClient(MousePosition));
if(info.HitInfoType == DevExpress.XtraTreeList.HitInfoType.Cell)
... // your code is here
}
There is no event that fires when a Node is clicked. However, here are some other events that might interest you:
AfterExpand - Fires immediately after a Node has been expanded.
BeforeExpand - Fires before a Node is expanded.
FocusedNodeChanged - Fires immediately after changing the focused Node (which happens when the user selects a Node, regardless of whether they clicked on it or used an arrow key to get there).
I'll also note that DevExpress has their own knowledge base with examples and sample code. It would be a great place to start your research for future questions: http://www.devexpress.com/Support/Center/
private void xtraTree_AfterFocusNode(object sender, NodeEventArgs e)
{
}
You can handle the above event on the XtraTreeList control and then extract the Node that was clicked on from the NodeEventArgs - e.Node
I am using monodevelop, and did a nodeview, now I want to get the data of the selected field just clicking the mouse
how can ido that?
I would like to try to use target signals. but what should I use?
You can access the data of the selected node by your_nodeview.NodeSelection.SelectedNode.
Depending on what you mean by "just clicking the mouse", you can use either the NodeView.NodeSelection.Changed event, or the EventBox.ButtonPressEvent event.
1) The NodeView.NodeSelection.Changed event is raised whenever the selected node of your nodeview is changed. In particular, when you click the mouse on some node that is not selected, it becomes selected and the event is raised. You can see an example of using this event here.
2) If you wrap your nodeview in an eventbox, then you can catch the event that the mouse is clicked on your nodeview. You can see an example here.
I have a c# windows form application where I have a treeView inside a tabPage of a tabControl which is a part of the main form.
For the tree view, I click on the items of the treeView which I want to select then some change happens based on my selected Items.
I am using the AfterSelect event for item selection and and the mouseUp event for undoing the selection.
The item selection and deselection happens right away with a single click (no problem). The other change with should happen based on the selected items happens after two clicks! A single click either on the item node or outside the node's area do not trigger this change. I have to click again in order to see the change. That is wired. I am not using mouse double click events for this or something similar, I am only using the events I described above.
How can this be happening? and How to resolve it? Thanks.
EDIT: I am using my own multi-selection version of the treeView and I found (using debug) that when I get the selected nodes of the tree in the AfterSelect event after the first click is zero, then it is the number of selected nodes with the second click. How come this is happening when selected nodes are added and to the current selectedNodes list with every click in the overrided OnAfterSelect event of the treeView?
here is part of tree view code:
public List<TreeNode> SelectedNodes
{
get
{
return selectedNodes;
}
set
{
removeSelectionFromNodes();
selectedNodes = value;
selectNodes();
}
}
protected override void OnAfterSelect(TreeViewEventArgs e)
{
base.OnAfterSelect(e);
base.SelectedNode = null;
List<MSTreeNode> nodes = new List<MSTreeNode>();
.
.
.
removeSelectionFromNodes();
selectedNodes.Clear();
selectedNodes.AddRange(nodes);
selectNodes();
}
Maybe Treeview is losing focus in between clicks (?). You could try setting Treeview HideSelection property to False to keep the currently selected item highlighted when the control loses focus.
I tried to use the MouseDown event instead of the AfterSelect event. I override it in the my own multi-selection version of the treeView and used in the c# application I am developing but still it did not work. I am not sure how mouse events really work. If not used carefully, you may see wired behaviors.
Well, I ended up overriding the MouseUp and MouseUp events in my treeView subclass then I created an event which listens for changes in the selectedNodes list. If a change to the selectedNodes happnes in any of the mouse events this event is triggered. Then, I used the ChangedSelectedNodes event handler of the treeview instance in my application to do the other changes when there is a change in the node selection. This time it worked as expected.
I posted this in hope that it would be beneficial to anyone else who ran into the same problem like me.
P.S. Sometimes things do not work as you expect them to be and you just have fight and go through every other possibility until you find the solution.
Disable the hide selection option and use afterSelect option
in my project that works well
I'm working with a .NET Treeview control (not WPF, but regular winforms) and am having trouble with the right-click event (or any click event) not firing when the control has no nodes inside of it. As per the response to another thread on Stackoverflow, my event handler code is as follows:
private void tvTest_MouseClick(object sender, MouseEventArgs e)
{
// Note: this block below is needed so that the menu appears on
// the correct node when right-clicking.
if (e.Button == MouseButtons.Right)
{
tvTest.SelectedNode = tvTest.GetNodeAt(e.X, e.Y);
if (tvTest.SelectedNode != null)
{
tvTestContextMenuStrip.Show(tvTest, e.Location);
}
else
{
tvTestContextMenuStrip.Show(tvTest, tvTest.Location);
}
}
}
The problem comes in that while this works fine when nodes are present, if the control is empty, I can't right-click the control and choose "add a node" to add to the root. The handler isn't entered AT ALL, as I set a breakpoint right at the beginning, and it appears the method is never entered.
Does anybody know how to get "something" to happen when the Treeview is empty?
I was curious about this particlar problem you described so I created a new project then added a Treeview Control to the form.
I then created an event handler for the MouseDown event, and made it show a message, when the right button is pressed. If you need the code I am happy to provided upon request, based on the fact its about 2 lines and Visual Studio created the event method I don't see the point.
I don't think this was explicitly identified above, but the original poster implies that he's using MouseClick from his code:
private void tvTest_MouseClick
But the answer responds with an answer to use MouseDown:
I then created an event handler for the MouseDown event...
I was able to reproduce the original problem by using MouseClick. It seems as if MouseClick is only raised if clicking on a node, but the MouseDown is raised regardless of whether you are on a node or not - so you can choose one or the other depending on the behavior you want.