Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

.NET 

Custom TreeView DesignMode Ignored Alternative

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!
Would love to see your custom treeview... ;)
BTC - bc1q3efew3yzfwhdz290dafykaw0a2w56g2j69g2c6
ETH - 0xf1c873217032468990D3313da6509D1b813c4671
USDT - 0xf1c873217032468990D3313da6509D1b813c4671
PP - [email protected]


Possibly Related Threads...
Thread / Author Replies Views Last Post
[C#] by twistedwebAug 2, 2018 07:53 AM 1 116 Aug 2, 2018 07:53 AM
Last Post: HFDone
[.NET] by [Eprouvez]Feb 1, 2018 12:18 PM 6 301 Feb 1, 2018 12:18 PM
Last Post: EdwardSn0wdent
[.NET] by Shadow ReignMar 4, 2017 02:08 AM 2 166 Mar 4, 2017 02:08 AM
Last Post: GuRuHaX