TreeStructure problem
-
Hi! In my program I'm using a class, which can nested like the TreeNodes in a TreeView, thus I also want to use a TreeView to display some of the content (a short string) of these nested classes. The user shall then be able to take further actions when by clicking on the TreeNodes. Here comes the problem: How do I map the currently selected TreeNode to the appropriate instance of the nested class?
-
Hi! In my program I'm using a class, which can nested like the TreeNodes in a TreeView, thus I also want to use a TreeView to display some of the content (a short string) of these nested classes. The user shall then be able to take further actions when by clicking on the TreeNodes. Here comes the problem: How do I map the currently selected TreeNode to the appropriate instance of the nested class?
-
Hi! In my program I'm using a class, which can nested like the TreeNodes in a TreeView, thus I also want to use a TreeView to display some of the content (a short string) of these nested classes. The user shall then be able to take further actions when by clicking on the TreeNodes. Here comes the problem: How do I map the currently selected TreeNode to the appropriate instance of the nested class?
-
Or, the other way around, you can inherit from TreeView, create its nodes dynamically, and set each node's Tag property with an appropriate nested class instance.
Thanks a lot! That's exactly what I was looking for. Now I've discovered another problem: The TreeView apparently always selects one node, no matter where I click. I, however have a context menu, which has to react differently, depending on whether the user clicks on a node or in some empty space. So I tried to determine the node at the clicking position with the GetNodeAt() method. This weardily always returns null. Here's the code (tvAlbums is the TreeView and contMenu the contextMenu): private void tvAlbums_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e) { if(e.Button == MouseButtons.Right) { Point pos = new Point(e.X, e.Y); contMenu.Show(tvAlbums, pos); pos = tvAlbums.PointToClient(pos); if(tvAlbums.GetNodeAt(pos) != null) { /* a node was selected - adjust contMenu accordingly */} else { /* no node is selected - adjust contMenu accordingly */} } }
-
Thanks a lot! That's exactly what I was looking for. Now I've discovered another problem: The TreeView apparently always selects one node, no matter where I click. I, however have a context menu, which has to react differently, depending on whether the user clicks on a node or in some empty space. So I tried to determine the node at the clicking position with the GetNodeAt() method. This weardily always returns null. Here's the code (tvAlbums is the TreeView and contMenu the contextMenu): private void tvAlbums_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e) { if(e.Button == MouseButtons.Right) { Point pos = new Point(e.X, e.Y); contMenu.Show(tvAlbums, pos); pos = tvAlbums.PointToClient(pos); if(tvAlbums.GetNodeAt(pos) != null) { /* a node was selected - adjust contMenu accordingly */} else { /* no node is selected - adjust contMenu accordingly */} } }
-
Try checking the tvAlbums.SelectedNode property instead of calling GetNodeAt - does it give you the correct Node reference? whoops -- i mean "in addition to calling GetNodeAt"
-
tvAlbums.SelectedNode does return a correct referece to a node, the problem however is that it always returns a node reference, even if the user clicked into the void.
-
tvAlbums.SelectedNode does return a correct referece to a node, the problem however is that it always returns a node reference, even if the user clicked into the void.
-
tvAlbums.SelectedNode does return a correct referece to a node, the problem however is that it always returns a node reference, even if the user clicked into the void.
I've just found a working solution, which for which I cannot find any logical reason though: When I create a point based on some EventArgs e, I first have to let the point go through a PointToClient operation and let it go through PointToScreen to make the GetNodeAt method to work properly: Point p = new Point(e.X, e.Y); p = tvAlbums.PointToClient(p); p = tvAlbums.PointToScreen(p); // now tvAlbums.GetNodeAt(p) will work properly Thanks once again for your help!
-
I've just found a working solution, which for which I cannot find any logical reason though: When I create a point based on some EventArgs e, I first have to let the point go through a PointToClient operation and let it go through PointToScreen to make the GetNodeAt method to work properly: Point p = new Point(e.X, e.Y); p = tvAlbums.PointToClient(p); p = tvAlbums.PointToScreen(p); // now tvAlbums.GetNodeAt(p) will work properly Thanks once again for your help!
you're very welcome. Also, I realize upon reading your last message why you need to convert the Point. The MouseEventArgs X and Y properties relate to screen co-ordinates. Controls use co-ordinates based on their parent control's ClientRectangle. The conversion forces the TreeView to determine the screen co-ordinates of its own ClientRectangle, which it otherwise has no need to know. I don't suppose you know a fast and reliable way to serialize rich text, do you?:)
-
you're very welcome. Also, I realize upon reading your last message why you need to convert the Point. The MouseEventArgs X and Y properties relate to screen co-ordinates. Controls use co-ordinates based on their parent control's ClientRectangle. The conversion forces the TreeView to determine the screen co-ordinates of its own ClientRectangle, which it otherwise has no need to know. I don't suppose you know a fast and reliable way to serialize rich text, do you?:)