Rectangle 27 1

With help of jquery we can send the selected node id to the query string and on document.ready we can read back and highlight that node. Have a look on the code:

public void TreeView1_OnClick(Object sender, EventArgs e)
{
    ScriptManager.RegisterStartupScript(
        Page,
        Page.GetType(),
        "HighlightSelectedNode",
        "HighlightSelectedNode();",
        true
    );
}

and the javascript:

<script type="text/javascript" language="javascript">
    function HighlightSelectedNode() {
        var selectedNodeID = $('#<%= TreeView1.ClientID %>_SelectedNode').val();
        if (selectedNodeID != "") {
                document.location.href = "http://" + window.location.host
                    + window.location.pathname
                    + "?Node=" + selectedNodeID;
                return false;
        } else {
            // alert("Not found");
        }
    }

    // Highlight active node on pageload.
    $(document).ready(function () {
        var querystring = location.search.replace('?', '').split('&');
        var queryObj = {};
        for (var i = 0; i < querystring.length; i++) {
            var name = querystring[i].split('=')[0];
            var value = querystring[i].split('=')[1];
            queryObj[name] = value;
        }
        var nodeID = queryObj["Node"];
        $('#' + nodeID).css({ 'background-color': '#888'});              
    });
</script>

c# - selecting a node of treeview after postback - asp.net - Stack Ove...

c# asp.net treeview
Rectangle 27 11

I find the standard windows treeview behavior selection behavior to be quite annoying. For example, if you are using Explorer and right click on a node and hit Properties, it highlights the node and shows the properties dialog for the node you clicked on. But when you return from the dialog, the highlighted node was the node previously selected/highlighted before you did the right-click. I find this causes usability problems because I am forever being confused on whether I acted on the right node.

So in many of our GUIs, we change the selected tree node on a right-click so that there is no confusion. This may not be the same as a standard iwndos app like Explorer (and I tend to strongly model our GUI behavior after standard window apps for usabiltiy reasons), I believe that this one exception case results in far more usable trees.

Here is some code that changes the selection during the right click:

private void tree_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
  {
     // only need to change selected note during right-click - otherwise tree does
     // fine by itself
     if ( e.Button == MouseButtons.Right )
     {         
        Point pt = new Point( e.X, e.Y );
        tree.PointToClient( pt );

        TreeNode Node = tree.GetNodeAt( pt );
        if ( Node != null )
        {
           if ( Node.Bounds.Contains( pt ) )
           {
              tree.SelectedNode = Node;
              ResetContextMenu();
              contextMenuTree.Show( tree, pt );
           }
        }
     }
  }

This is the best solution for me because it actually selects the node you clicked on.

c# - Find node clicked under context menu - Stack Overflow

c# winforms treeview contextmenu
Rectangle 27 0

$("#TreeView1t5").addClass("TreeView1_5");
#TreeView1t5
node
TreeView1_5
css class

ASP.NET Treeview control and Client side javascript - Highlight select...

treeview asp.net-2.0 selection treenode
Rectangle 27 0

I had an similar issue and wanted to have a TreeView node selected (highlighted) on form load. Maybe someone has the same problem, too.

I first tried Pondidum's solution. Without success. But then I found the solution in another thread: Simply set the TabIndex of the TreeView to 0. In that case you don't need to set the focus. Just choose the node that should be selected by using SelectedNode and set the TabIndex. That's it.

c# - WinForms TreeView - how to manually "highlight" node (like it was...

c# winforms treeview
Rectangle 27 0

Gets or sets a value indicating whether the selected tree node remains highlighted even when the tree view has lost the focus.

c# - Select 2 nodes in different treeview at the same time - Stack Ove...

c# winforms treeview
Rectangle 27 0

I find the standard windows treeview behavior selection behavior to be quite annoying. For example, if you are using Explorer and right click on a node and hit Properties, it highlights the node and shows the properties dialog for the node you clicked on. But when you return from the dialog, the highlighted node was the node previously selected/highlighted before you did the right-click. I find this causes usability problems because I am forever being confused on whether I acted on the right node.

So in many of our GUIs, we change the selected tree node on a right-click so that there is no confusion. This may not be the same as a standard iwndos app like Explorer (and I tend to strongly model our GUI behavior after standard window apps for usabiltiy reasons), I believe that this one exception case results in far more usable trees.

Here is some code that changes the selection during the right click:

private void tree_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
  {
     // only need to change selected note during right-click - otherwise tree does
     // fine by itself
     if ( e.Button == MouseButtons.Right )
     {         
        Point pt = new Point( e.X, e.Y );
        tree.PointToClient( pt );

        TreeNode Node = tree.GetNodeAt( pt );
        if ( Node != null )
        {
           if ( Node.Bounds.Contains( pt ) )
           {
              tree.SelectedNode = Node;
              ResetContextMenu();
              contextMenuTree.Show( tree, pt );
           }
        }
     }
  }

This is the best solution for me because it actually selects the node you clicked on.

c# - Find node clicked under context menu - Stack Overflow

c# treeview contextmenu
Rectangle 27 0

The below code will allow you to adjust the background colour you use, to ensure that all selected nodes are highlighted.

protected override void WndProc(ref Message m)
{
    switch (m.Msg) {
        // WM_REFLECT is added because WM_NOTIFY is normally sent just
        // to the parent window, but Windows.Form will reflect it back
        // to us, MFC-style.
        case Win32.WM_REFLECT + Win32.WM_NOTIFY: {
            Win32.NMHDR nmhdr = (Win32.NMHDR)m.GetLParam(typeof(Win32.NMHDR));
            switch((int)nmhdr.code) {
                case Win32.NM_CUSTOMDRAW:
                    base.WndProc(ref m);
                    Win32.NMTVCUSTOMDRAW nmTvDraw = (Win32.NMTVCUSTOMDRAW)m.GetLParam(typeof(Win32.NMTVCUSTOMDRAW));
                    switch (nmTvDraw.nmcd.dwDrawStage) {
                        case Win32.CDDS_ITEMPREPAINT:
                            // Find the node being painted.
                            TreeNode n = TreeNode.FromHandle(this, nmTvDraw.nmcd.lItemlParam);
                            if (allSelected.Contains(n))
                                // Override its background colour.
                                nmTvDraw.clrTextBk = ColorTranslator.ToWin32(SystemColors.Highlight);
                            m.Result = (IntPtr)Win32.CDRF_DODEFAULT;  // Continue rest of painting as normal
                            break;
                    }
                    Marshal.StructureToPtr(nmTvDraw, m.LParam, false);  // copy changes back
                    return;
            }
            break;
        }
    }
    base.WndProc(ref m);
}

// WM_NOTIFY notification message header.
[System.Runtime.InteropServices.StructLayout(LayoutKind.Sequential)]
public class NMHDR
{
    private IntPtr hwndFrom;
    public IntPtr idFrom;
    public uint code;
}

[StructLayout(LayoutKind.Sequential)]
public struct NMCUSTOMDRAW
{
    public NMHDR hdr;
    public int dwDrawStage;
    public IntPtr hdc;
    public RECT rc;
    public IntPtr dwItemSpec;
    public int uItemState;
    public IntPtr lItemlParam;
}

[StructLayout(LayoutKind.Sequential)]
public struct NMTVCUSTOMDRAW
{
    public NMCUSTOMDRAW nmcd;
    public int clrText;
    public int clrTextBk;
    public int iLevel;
}

public const int CDIS_SELECTED = 0x0001;
public const int CDIS_FOCUS = 0x0010;
public const int CDDS_PREPAINT = 0x00000001;
public const int CDDS_POSTPAINT = 0x00000002;
public const int CDDS_PREERASE = 0x00000003;
public const int CDDS_POSTERASE = 0x00000004;
public const int CDDS_ITEM = 0x00010000;  // item specific 
public const int CDDS_ITEMPREPAINT = (CDDS_ITEM | CDDS_PREPAINT);
public const int CDDS_ITEMPOSTPAINT = (CDDS_ITEM | CDDS_POSTPAINT);
public const int CDDS_ITEMPREERASE = (CDDS_ITEM | CDDS_PREERASE);
public const int CDDS_ITEMPOSTERASE = (CDDS_ITEM | CDDS_POSTERASE);
public const int CDDS_SUBITEM = 0x00020000;
public const int CDRF_DODEFAULT = 0x00000000;
public const int CDRF_NOTIFYITEMDRAW = 0x00000020;
public const int CDRF_NOTIFYSUBITEMDRAW = 0x00000020;  // flags are the same, we can distinguish by context

public const int WM_USER = 0x0400;
public const int WM_NOTIFY = 0x4E;
public const int WM_REFLECT = WM_USER + 0x1C00;

c# - How do I allow multi-select in a .NET TreeView? - Stack Overflow

c# .net winforms .net-2.0 treeview
Rectangle 27 0

That's because the highlight color performs two duties, it shows the selected node and shows the focused node. If you don't do anything with the right-click event then it jumps back to the selected node. The workaround is to select the node:

private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e) {
        if (e.Button == MouseButtons.Right) treeView1.SelectedNode = e.Node;
    }

Plus anything else you want to do, usually displaying a context menu.

.net - How can I right click to select a node in a treeview control - ...

.net vb.net winforms .net-4.0 treeview
Rectangle 27 0

By highlighting, I am assuming you mean to make the text bold. It's not as elegant as some of the other client side technologies, but you could handle the node being changed and make the selected node bold with something like

treeNode.Font = new Font(treeNode.Font, treeNode.Font.Style | treeNode.Bold);

c# - How can I programmatically click a TreeView TreeNode so it appear...

c# winforms treeview
Rectangle 27 0

For me the issue was that the TreeView control did not necessarily have focus and therefore the selected node was not showing up as highlighted. The following code worked for me:

treeView.Focus();
treeView.SelectedNode = myTreeNode;
TreeView

c# - How can I programmatically click a TreeView TreeNode so it appear...

c# winforms treeview
Rectangle 27 0

After a postback, everything is gone. You need to store the ID of the selected node in a cookie.

However, your nodes don't have ID's, so you could store the text or the href in a cookie and then when the page loads, look for the cookie, if found, use jQuery to find the node and select it.

javascript - How to maintain state to highlighting selected node of a ...

javascript jquery treeview
Rectangle 27 0

With help of jquery we can send the selected node id to the query string and on document.ready we can read back and highlight that node. Have a look on the code:

public void TreeView1_OnClick(Object sender, EventArgs e)
{
    ScriptManager.RegisterStartupScript(
        Page,
        Page.GetType(),
        "HighlightSelectedNode",
        "HighlightSelectedNode();",
        true
    );
}

and the javascript:

<script type="text/javascript" language="javascript">
    function HighlightSelectedNode() {
        var selectedNodeID = $('#<%= TreeView1.ClientID %>_SelectedNode').val();
        if (selectedNodeID != "") {
                document.location.href = "http://" + window.location.host
                    + window.location.pathname
                    + "?Node=" + selectedNodeID;
                return false;
        } else {
            // alert("Not found");
        }
    }

    // Highlight active node on pageload.
    $(document).ready(function () {
        var querystring = location.search.replace('?', '').split('&');
        var queryObj = {};
        for (var i = 0; i < querystring.length; i++) {
            var name = querystring[i].split('=')[0];
            var value = querystring[i].split('=')[1];
            queryObj[name] = value;
        }
        var nodeID = queryObj["Node"];
        $('#' + nodeID).css({ 'background-color': '#888'});              
    });
</script>

c# - selecting a node of treeview after postback - asp.net - Stack Ove...

c# asp.net treeview
Rectangle 27 0

Okay, so I was able to get part of the problem fixed. I can get one of the full length paths between any two random nodes in the graph using a variant of BFS where I maintain another array to keep track of the visited nodes, and then trace the path back recursively (if at least one path exists between the selected nodes that is). This way I am able to select two nodes (using clicks) in the graph, press the spacebar to highlight the path between the two nodes, and then click on any empty part of the page to remove the highlighting. Please note that I had to get a complete adjacency matrix for the nodes (bidirectional) in order to make this work.

In case anyone else is interested, I can share more details. From this, I figure it's just one hop more to get all the paths between two randomly selected nodes.

How to get full length link highlighting between two random nodes of a...

d3.js graph
Rectangle 27 0

The following works for me. Please note that I cancel the actual selection though, since otherwise the selection highlight would hide my highlightning. So you might have to keep track of which node is selected manually.

private void treeView1_BeforeSelect(object sender, TreeViewCancelEventArgs e)
{
   Dehighlight(treeView1.Nodes);
   e.Node.ForeColor = Color.Red;
   e.Cancel = true;
}

private void Dehighlight(TreeNodeCollection nodes)
{
   foreach (TreeNode node in nodes)
   {
      node.BackColor = Color.White;
      node.ForeColor = Color.Black;
      Dehighlight(node.Nodes);
   }
}

thanks for the insight ho...lol but it was stupid navigateURL not working with SelectedNodeChanged...

c# - Treeview nodes won't highlight using PopulateOndemand - Stack Ove...

c# treeview highlight
Rectangle 27 0

Try this on Server Control on the ASPX Page

$(".treeview a").click(function () { alert("Rohit"); $(this).css("background-color", "orange"); }); It's not working, neither the alert

asp.net - How to highlight the selected node in treeview(jquery)? - St...

jquery asp.net css treeview