VB .NET User Control Weirdness

VB .NET User Control Weirdness

jasonlake
Contributor Contributor
640 Views
1 Reply
Message 1 of 2

VB .NET User Control Weirdness

jasonlake
Contributor
Contributor

My solution has multiple projects, with one of the projects acting as the entry point, built with the ACAD Wizard. All of the projects use 1 or more PaletteSets that are set up in their own classes alongside the CommandClass..

The issue I am having is this... after opening one of these PaletteSets during an AutoCAD session, if it is left open when I close AutoCAD, the next time AutoCAD opens, during ACAD startup the PalleteSet tries to reopen itself. In this case the "User Defined Part" palette was left open.

Screenshot 2021-11-12 192225.png

I have recently undertaken rebuilding this set of projects from ones that were organically developed over many years, where PaletteSets worked just fine. Here's a sample of how I'm setting up my palettes.

' (C) Copyright 2021 by JL
'
Imports System.Drawing
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.EditorInput
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.Windows
Imports FreemanCAD

' This line is not mandatory, but improves loading performances
<Assembly: CommandClass(GetType(UserDefinedParts.MyCommands))>
Namespace UserDefinedParts

  ' This class is instantiated by AutoCAD for each document when
  ' a command is called by the user the first time in the context
  ' of a given document. In other words, non static data in this class
  ' is implicitly per-document!
  Public Class MyCommands
    Friend Shared UDP_ps As PaletteSet = Nothing

    <CommandMethod("USERDEFINEDPARTS", "UDP", "UDP", CommandFlags.Transparent)>
    Public Sub UserDefinedParts()
      If UDP_ps Is Nothing Then
        UDP_ps = New UDP_Palette
      End If
      UDP_ps.Visible = True
    End Sub
  End Class

  Public Class UDP_Palette
    Inherits PaletteSet

    Private Shared m_DocData As MyDocData = Nothing
    Private doc As Document = Core.Application.DocumentManager.MdiActiveDocument
    Private ed As Editor = doc.Editor
    Private db As Database = doc.Database

    Private m_PalState As Boolean = False
    Private ReadOnly paletteVisibility As Boolean = False

    Public Sub New()
      MyBase.New("User Defined Parts", New Guid("331F70E2-4651-4575-98C3-D65557CEAA22"))
      Style = PaletteSetStyles.ShowCloseButton Or PaletteSetStyles.ShowTabForSingle Or PaletteSetStyles.ShowAutoHideButton

      ed.WriteMessage(vbCrLf & "Loading UDP Tool")
      Dim form_UDP = New FRM_UDP()
      Add("UDP", form_UDP)

      DarkThemedIcon = My.Resources.userdefinedpart
      LightThemedIcon = My.Resources.userdefinedpart
      DockEnabled = DockSides.Left + DockSides.Right
      Size = New Size(330, 650)
      Visible = True
      paletteVisibility = True
      m_PalState = True
      KeepFocus = True

      If m_DocData Is Nothing Then m_DocData = New MyDocData
      AddHandler Core.Application.DocumentManager.DocumentActivated, AddressOf DocumentManager_DocumentActivated
      AddHandler StateChanged, AddressOf GetPalleteSetState

    End Sub


    Private Sub GetPalleteSetState(sender As Object, e As PaletteSetStateEventArgs)
      m_PalState = e.NewState.ToString = "Show"
    End Sub

    Private Sub DocumentManager_DocumentActivated(sender As Object, e As DocumentCollectionEventArgs)
      If m_DocData IsNot Nothing Then
        Visible = Core.Application.DocumentManager.Count >= 1 AndAlso m_PalState
        doc = Core.Application.DocumentManager.MdiActiveDocument
        If doc IsNot Nothing Then
          db = doc.Database
          ed = doc.Editor
        End If
      End If
    End Sub
  End Class
End Namespace

Am I missing anything there that would be causing this startup issue? Any help is greatly appreciated!!

0 Likes
Accepted solutions (1)
641 Views
1 Reply
Reply (1)
Message 2 of 2

jasonlake
Contributor
Contributor
Accepted solution

After a bit more digging I found that this line...

MyBase.New("User Defined Parts", New Guid("331F70E2-4651-4575-98C3-D65557CEAA22"))

...is where the issue was. It seems that if the Palette is left open at ACAD close, the Palette Name is called when AutoCAD reopens.

But, with even further digging, I found that this New has 2 overloads...

Public Sub New(name As String, toolID As Guid)
Public Sub New(name As String, cmd As String, toolID As Guid)

So rewriting that line like this...

MyBase.New("User Defined Parts", "UDP", New Guid("80A6FDB4-37A1-45E8-B66C-5A176324B1D7"))

...fixed the issue, and the palette reopens nicely. 

NOTE: You need to create a new GUID when making this change, as ACAD uses that to "remember" your old palette info.

0 Likes