AnsweredAssumed Answered

Can the Property Manager Page & Folder Browser Dialog class play nice together?

Question asked by Steve Fry on Jul 26, 2010
Latest reply on Aug 3, 2010 by Carlos Almeida

I'm not having much success using the FolderBrowserDialog class in a property manager page. I receive an exception 'System.Threading.ThreadStateException' occurred in System.Windows.Forms.dll and the folder dialog does not appear. It appears to be a threading issue which I have not been able to work out.


Can anyone shed any light on this?


Below is the code I am using. I only included the event handlers that I used for the button.


Imports SolidWorks.Interop.sldworks
Imports SolidWorks.Interop.swconst
Imports SolidWorks.Interop.swpublished
Imports System.Runtime.InteropServices
Imports System.Diagnostics
Imports System.Windows.Forms


<ComVisibleAttribute(True)> _
Public Class clsPropMgr
    Implements PropertyManagerPage2Handler6

    Dim FolderBrowser As New FolderBrowserDialog

    'General objects required for the PropertyManager page
    Dim pm_Page As PropertyManagerPage2

    Dim pm_Group As PropertyManagerPageGroup
    Dim pm_FolderBtn As PropertyManagerPageButton

    'Each object in the page needs a unique ID
    Const GrpID As Integer = 100
    Const FolderBtnID As Integer = 145


    Sub Show()
    End Sub


    Public Sub New(ByVal swApp As SldWorks)
        Dim PageTitle As String
        Dim caption As String
        Dim tip As String
        Dim options As Long
        Dim longerrors As Long
        Dim controlType As Integer
        Dim alignment As Integer


        'Set the variables for the page
        PageTitle = "Folder Browser"
        options = swPropertyManagerButtonTypes_e.swPropertyManager_OkayButton _
            + swPropertyManagerButtonTypes_e.swPropertyManager_CancelButton _
            + swPropertyManagerPageOptions_e.swPropertyManagerOptions_LockedPage _
            + swPropertyManagerPageOptions_e.swPropertyManagerOptions_PushpinButton


        'Create the PropertyManager page
        pm_Page = CType(swApp.CreatePropertyManagerPage(PageTitle, options, Me, longerrors), PropertyManagerPage2)


        'Make sure that the page was created properly
        If longerrors = swPropertyManagerPageStatus_e.swPropertyManagerPage_Okay Then
            'Begin adding the controls to the page


            'Create the group box
            caption = "Group"
            options = swAddGroupBoxOptions_e.swGroupBoxOptions_Visible + swAddGroupBoxOptions_e.swGroupBoxOptions_Expanded
            pm_Group = pm_Page.AddGroupBox(GrpID, caption, options)


            'Create Save Folder Button
            controlType = swPropertyManagerPageControlType_e.swControlType_Button
            caption = "Select Folder"
            alignment = swPropertyManagerPageControlLeftAlign_e.swControlAlign_Indent
            options = swAddControlOptions_e.swControlOptions_Visible + swAddControlOptions_e.swControlOptions_Enabled
            tip = ""
            pm_FolderBtn = pm_Group.AddControl(FolderBtnID, controlType, caption, alignment, options, tip)


        Else  'If the page is not created
            MsgBox("An error occurred while attempting to create the " & "PropertyManager Page", vbCritical)
        End If
    End Sub

    Public Sub OnButtonPress(ByVal Id As Integer) Implements SolidWorks.Interop.swpublished.PropertyManagerPage2Handler6.OnButtonPress
        Select Case Id
            Case FolderBtnID
                Debug.Print("Folder Button Pressed...")

                FolderBrowser.Description = "Select Folder to Save To"
                Dim results As DialogResult = FolderBrowser.ShowDialog
        End Select
    End Sub