 |
|
|
 |
|
 |
Not sure why but the gui is correct on the first treeview but when i try to clone multiple nodes and add them to a second on the same form the second treeview has all the nodes highlighted and control doesnt not behave correctly.
|
|
|
|
 |
|
 |
Hello,
I'm block trying to make this to work after migrating from a regular TreeView to this new extension. In the original code, TreeView calls a method for drawing the node text. The code simply worked fine. In the new code the selected node(s) are never properly painted. They are always a white line. Don't know how to move forward on this img src="/script/Forums/Images/smiley_confused.gif" align="top" alt="Confused | :confused:" />
|
|
|
|
 |
|
 |
hello,
first of all thanks for this work - made my life a lot easier.
I am trying to prevent a user from selecting Level-1 nodes a level -0 node is selected and vice-verse.
I implemented some code for this and it should work .. but doesn't, the control ignores e.Cancel =true in the event.
Does anybody have a clue how I could get around that issue?
Thank's in advance,
Thomas
|
|
|
|
 |
|
 |
Since the code is not actually using the built-in code for treeview to select its nodes, trying to cancel a select through the normal means won't work. You'll have to put a check into its OnMouseUp, where it implements its own selection functionality, to not select the node if a certain selection is not allowed.
|
|
|
|
 |
|
 |
Many thanks go to the guy who made this. It is just what I was looking for!!
|
|
|
|
 |
|
 |
By calling OnAfterSelect at the end of the SelectNode method, the progam using this will recieve on AfterSelect BEFORE it recieves the BeforeSelect, and then it will recieve a second AfterSelect. So far this seems to be preventing me from cancelling a node selection if, for instance, I only want level 0 nodes to be selectable in a multi-select. I tried to add a call to OnBeforeSelect at the top of SelectNode at which time I discovered the Focus issue (I had a couple break points on). Even with the added OnBeforeSelect, the cancel does not seem to make it back to SelectNode. Still working on it.
|
|
|
|
 |
|
 |
The standard TreeView's default behavior is to select the first node if there are no nodes selected when it gets focus. By crippling the base.SelectedNode by setting it to null all the time, you end up re-generating the OnBeforeSelect & OnAfterSelect events every time the control gets focus. This can be as simple as the app itself gaining focus. If you have a break-point in the select chain of events, you can end up with an end-less recursion loop as focus shifts back & forth from the VS IDE to the application.
So, what is the main purpose of crippling the base.SelectedNode? What happens if we leave it uncrippled?
|
|
|
|
 |
|
 |
Select all (CTRL-A) is not supported. Add the code in bold to the OnKeyDown() function.
else if (e.Control && (e.KeyCode == Keys.A))
{
this.ClearSelectedNodes();
this.CollapseAll();
TreeNode ndCurrent = this.TopNode;
while (ndCurrent != null)
{
ToggleNode(ndCurrent, true);
ndCurrent = ndCurrent.NextNode;
}
}
else
{
|
|
|
|
 |
|
 |
If you press a shift key by itself, OnKeyDown correctly ignores it. But if you press a control key by itself, OnKeyDown runs through the code. This is OK unless you have added a call to OnAfterSelect at the bottom of the function because most keys which change the selection do not call that function.
In OnKeyDown, I made the following changes in bold to my version:
if (e.KeyCode == Keys.ShiftKey) return;
if (e.KeyCode == Keys.ControlKey) return;
|
|
|
|
 |
|
 |
From the Microsoft documentation for TreeView.HideSelection Property:
"When this property is set to false, selected nodes in the TreeView control remain highlighted in a different color than the current selection color when the TreeView control loses focus."
This behavior is ignored by this class.
|
|
|
|
 |
|
 |
I have an app which monitors tree selection. It really needed multiple selection, but when I changed over to this class I started noticing behavior differences. So I built one version of the app with TreeView and another with this class and ran them side-by-side to look for differences. I'm documenting the ones I find.
|
|
|
|
 |
|
 |
In my copy, I added always change selected nodes to highlight color in OnGotFocus. Added function OnLeaveFocus which checks HideSelection, and if it is enabled then dim the selected nodes. (Pretty simple change.)
|
|
|
|
 |
|
 |
Add the following bold code to OnGotFocus()
HighlightSelection();
base.OnGotFocus(e);
Also add this function
protected override void OnLostFocus(EventArgs e)
{
try
{
// Handle if HideSelection property is in use.
if (this.HideSelection)
{
DimSelection();
}
base.OnLostFocus(e);
}
catch (Exception ex)
{
HandleException(ex);
}
}
|
|
|
|
 |
|
 |
All your code additions have been great. However, you did not include your code for DimSelection() or HighlightSelection(). I can try to write my own, but it would be great to see what you did.
|
|
|
|
 |
|
 |
They are pretty simple and use the color values from ToggleNode().
private void HighlightSelection()
{
foreach (TreeNode node in SelectedNodes)
{
node.BackColor = SystemColors.Highlight;
node.ForeColor = SystemColors.HighlightText;
}
}
private void DimSelection()
{
foreach (TreeNode node in SelectedNodes)
{
node.BackColor = SystemColors.Control;
node.ForeColor = this.ForeColor;
}
}
|
|
|
|
 |
|
 |
In TreeView, the Home and End keys respectively cause selection to go to the highest entry and lowest entries in the entire tree. In this class the End key only goes to the last node withing the top outermost node!
modified on Friday, July 31, 2009 3:11 PM
|
|
|
|
 |
|
 |
Turns out to be a simple fix. In the function OnKeyDown, inside of the block of code for else if( e.KeyCode == Keys.End ) change this line TreeNode ndLast = this.Nodes[0].LastNode; to this TreeNode ndLast = this.Nodes[this.Nodes.Count - 1].LastNode;
|
|
|
|
 |
|
 |
OnKeyDown handles key navigation/selection. But when selection changes the AfterSelect event is not fired in most cases. Of all the keys supported, only Up and Down call AfterSelect.
modified on Friday, July 31, 2009 2:24 PM
|
|
|
|
 |
|
 |
The OnGotFocus function will select the top node if nothing is already selected. So the selection changes but the OnAfterSelect event is not fired. Add the following bold line of code to OnGotFocus:
if( m_SelectedNode == null && this.TopNode != null )
{
ToggleNode( this.TopNode, true );
OnAfterSelect(new TreeViewEventArgs(m_SelectedNode));
}
|
|
|
|
 |
|
 |
It is not documented, but after running the code it appears that SelectedNode is the last node that was selected in multiple selection. Is this correct?
|
|
|
|
 |
|
 |
The current version does not allow changing selection behavior between single and multiple selection. Only a few modications were needed to add this to my copy.
|
|
|
|
 |
|
 |
May I ask how you added the ability to switch between multi and single select? I tried it and with the following:
Add 'AllowMultiSelect' property.
In the mousedown and mouseup events in place of selectnode(node) I added:
If AllowMultiSelect Then
selectnode(node)
Else
selectsinglenode(node)
End If
Why would this not work and what did you do to get it to work? Mine puts the AllowMultiSelect property in the property grid but it doesn't seem to do anything...
*edit* Nevermind, I'm an idiot... Referencing the wrong .dll, long day. Sorry
modified on Tuesday, September 20, 2011 2:24 PM
|
|
|
|
 |
|
 |
The Microsoft documentation for the TreeView.SelectedNode Property says:
"When you set this property, the specified node is scrolled into view and any parent nodes are expanded so that the specified node is visible."
The new class as provided does not do this. To add this support, in the SelectNodes:set function add the new line of code:
foreach( TreeNode node in value )
{
ToggleNode( node, true );
node.EnsureVisible();
}
and in the SelectedNode:set function add the new line of code:
if( value != null )
{
SelectNode( value );
m_SelectedNode.EnsureVisible();
}
|
|
|
|
 |
|
 |
hi,
I'm trying to do the same thing in vb, not C#...any suggestions? thanks.
|
|
|
|
 |