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
Related
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.
I have a Treeview with nodes. If the user doubleclicks a node, an editdialog for the node opens where he can modify the data etc.
There is a problem, if the user clicks fastly twice onto the collapsebutton of a node - this also counts a double click. Is there a way to avoid this? I searched the Web but i found nothing really helpfull. Detecting if the click is within a specific area is useless, cause the Treeview is dynamic and scrollable.
Many thanks in advance.
You can just call HitTest and find out where the user clicked.
private void treeView1_MouseDoubleClick(object sender, MouseEventArgs e)
{
var hitTest = treeView1.HitTest(e.Location);
if (hitTest.Location == TreeViewHitTestLocations.PlusMinus)
{
//expand collapse clicked
}
}
I'm trying to call a method as soon as a TextBox on my screen gets 'un-focused' if that makes any sense? The user types in a username and as soon as that textbox loses focus I want to fire an event that checks if that username is indeed available.
Thanks!
There is a Control.Leave in C#, which I think is perfect for your purpose.
you can go to events of the textbox in visual studio, and find the Leave event.
The code generated will be like :
private void txtbox_Leave(object sender, EventArgs e)
{
//Check for available operation Code
}
Please explore the LostFocus event in a textbox. Hope it helps
select your object and to choose the panel of the properties and click on event.To unroll until LEAVE and double-click above.that will give you:
private void object_Leave( object sender, EventArgs e)
{
////put your code here
}
In WPF Try this:
TextBox.LostFocus
This will work:
Go to the "properties" of your textbox. You will see a yellow lightning bolt in the first line tab. There you will find all possible events that can be triggered. Search for "Leave" entry, double-click it. There you can put whatever you want.
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.