none
Treeview replaces ampersand with underscore for node labels RRS feed

  • Question

  • Hi all.  I'm at a bit of a loss here -- I understand that labels expose a UseMemnomic property that allows you to specify the behavior regarding ampersands as a sort of hot key.  Setting the property to False will alow ampersands to appear naturally rather than being replaced with an underscore character.

    However, I can't seem to find a similar property on the TreeView control nor on the TreeNode class. 

    So let's say I have a label "A & P Groceries", it is displayed as "A _P Groceries" in the treenode label.  Does anyone know how to stop this behavior?  I am using inherited treenode classes so I do have access to protected members but I can't seem to find anything there either (assuming the node itself can resolve the issue).


    Blog: http://codemidden.wordpress.com

    Tuesday, July 17, 2012 2:33 PM

Answers

  • My bad...  I appreciate you trying to reproduce this Wyck.

    I hadn't realized that the treeview had been set to OwnerDraw mode, so in the DrawNode event handler I did not have the correct format flag set.  Apparently it requires the NoPrefix flag set (see here for more options http://msdn.microsoft.com/en-us/library/system.windows.forms.textformatflags.aspx)

    For the curious, the Ownder draw mode was set so that the user could see the selected node in the tree as blue (selected back color) when the tree was not the focused control.  This does not happen natively with the treeview control apparently.  Here is the corrected code of the event handler:

        Friend Sub TreeView1_DrawNode(sender As Object, e As System.Windows.Forms.DrawTreeNodeEventArgs)
    
            Dim state As TreeNodeStates = e.State
            Dim font As Font = If(e.Node.NodeFont, e.Node.TreeView.Font)
            Dim fore As Color = e.Node.ForeColor
            If fore = Color.Empty Then
                fore = e.Node.TreeView.ForeColor
            End If
            If e.Node Is e.Node.TreeView.SelectedNode Then
                fore = SystemColors.HighlightText
                e.Graphics.FillRectangle(SystemBrushes.Highlight, e.Bounds)
                ControlPaint.DrawFocusRectangle(e.Graphics, e.Bounds, fore, SystemColors.Highlight)
                TextRenderer.DrawText(e.Graphics, e.Node.Text, font, e.Bounds, fore, TextFormatFlags.NoPrefix)
            Else
                e.Graphics.FillRectangle(SystemBrushes.Window, e.Bounds)
                TextRenderer.DrawText(e.Graphics, e.Node.Text, font, e.Bounds, fore, TextFormatFlags.NoPrefix)
            End If
    
        End Sub


    Blog: http://codemidden.wordpress.com

    • Marked as answer by Dig-Boy Tuesday, July 17, 2012 3:26 PM
    Tuesday, July 17, 2012 3:26 PM

All replies

  • I have no problem setting that text on a regular TreeView.

    treeView1.Nodes.Add( "A & P Groceries" );

    My tree node's label looks normal.  The ampersand appears, and there no underlined mnemonic space character.

    So, am I missing something about how to reproduce this?

    Tuesday, July 17, 2012 2:49 PM
  • My bad...  I appreciate you trying to reproduce this Wyck.

    I hadn't realized that the treeview had been set to OwnerDraw mode, so in the DrawNode event handler I did not have the correct format flag set.  Apparently it requires the NoPrefix flag set (see here for more options http://msdn.microsoft.com/en-us/library/system.windows.forms.textformatflags.aspx)

    For the curious, the Ownder draw mode was set so that the user could see the selected node in the tree as blue (selected back color) when the tree was not the focused control.  This does not happen natively with the treeview control apparently.  Here is the corrected code of the event handler:

        Friend Sub TreeView1_DrawNode(sender As Object, e As System.Windows.Forms.DrawTreeNodeEventArgs)
    
            Dim state As TreeNodeStates = e.State
            Dim font As Font = If(e.Node.NodeFont, e.Node.TreeView.Font)
            Dim fore As Color = e.Node.ForeColor
            If fore = Color.Empty Then
                fore = e.Node.TreeView.ForeColor
            End If
            If e.Node Is e.Node.TreeView.SelectedNode Then
                fore = SystemColors.HighlightText
                e.Graphics.FillRectangle(SystemBrushes.Highlight, e.Bounds)
                ControlPaint.DrawFocusRectangle(e.Graphics, e.Bounds, fore, SystemColors.Highlight)
                TextRenderer.DrawText(e.Graphics, e.Node.Text, font, e.Bounds, fore, TextFormatFlags.NoPrefix)
            Else
                e.Graphics.FillRectangle(SystemBrushes.Window, e.Bounds)
                TextRenderer.DrawText(e.Graphics, e.Node.Text, font, e.Bounds, fore, TextFormatFlags.NoPrefix)
            End If
    
        End Sub


    Blog: http://codemidden.wordpress.com

    • Marked as answer by Dig-Boy Tuesday, July 17, 2012 3:26 PM
    Tuesday, July 17, 2012 3:26 PM
  • Hi Dig_Boy,

    I'm glad you solved the issue and thank you for sharing the solution.

    Best Regards,


    Bob Wu [MSFT]
    MSDN Community Support | Feedback to us

    Wednesday, July 18, 2012 2:34 AM
    Moderator
  • TreeNode node = new TreeNode();

    string strTest = "A & P Groceries";

    node.Text = strTest.Replace("&", "&&");

    Wednesday, May 8, 2013 3:41 AM