Hack Forums
[.NET] Custom TreeView DesignMode Ignored Alternative - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [.NET] Custom TreeView DesignMode Ignored Alternative (/showthread.php?tid=5754399)



Custom TreeView DesignMode Ignored Alternative - [Eprouvez] - Nov 17, 2017

Hey guys,

Long time no see, thought I'd post this up really quickly as I found it useful.

I had a problem with creating a custom TreeView, I was coming across an issue where even though I was checking to see if the control was in DesignMode, it would execute the code at design time anyway.

For example:

Code
Public Class Test
   Inherits TreeView

   Public Sub New()

       Nodes.Clear()

       If Not DesignMode Then
           Nodes.Add(New TreeNode("TEST NODE"))
       End If

   End Sub

End Class


This creates a node at design time, and seems to be a bug when inheriting some controls. To over come this I found you can use LicenseManager.UsageMode in System.ComponentModel to check the state instead (LicenseUsageMode.DesignTime). Because it is ignoring DesignMode = False, we end up with duplicate nodes at runtime.

Here is the working code:

Code
'Imports System.ComponentModel

Public Class Test
   Inherits TreeView

   Public Sub New()

       Nodes.Clear()

       If Not System.ComponentModel.LicenseManager.UsageMode = System.ComponentModel.LicenseUsageMode.Designtime Then
           Nodes.Add(New TreeNode("TEST NODE"))
       End If

   End Sub

End Class

This prevents the code executing at design time, and also prevents duplicate 'phantom' nodes as well.

All credit goes to Keith Barrow, with his notes on auto-loading data. Provided the fix I needed.

Thanks for reading!

Eprouvez

P.S - I'll release my custom TreeView component for testing soon, if anyone is interested let me know here!


RE: Custom TreeView DesignMode Ignored Alternative - SaketSaket - Dec 8, 2017

Would love to see your custom treeview... ;)