.VB Store value in variable for use on form load

.VB Store value in variable for use on form load

Frank-Oosterwaal
Collaborator Collaborator
219 Views
2 Replies
Message 1 of 3

.VB Store value in variable for use on form load

Frank-Oosterwaal
Collaborator
Collaborator

Hi all,

 

I feel the answer is right in front of me but I can't seem to find why this doesn't work. I want to populate a combobox on each form load and the value to be the same as selected on the previous load.

 

The combobox is populated using this sub:

    'Populate combobox Type based on part or assembly
    Private Sub Set_Combobox_Type()

        'Get the active instance of Autodesk Inventor
        Dim inventorApp As Inventor.Application = System.Runtime.InteropServices.Marshal.GetActiveObject("Inventor.Application")
        Dim Doc As Document = inventorApp.ActiveDocument

        If TypeOf Doc Is PartDocument Then
            ' Populate cboType for Part documents
            cboType.Items.Clear()
            cboType.Items.AddRange(New String() {"A", "B", "C"})
        ElseIf TypeOf Doc Is AssemblyDocument Then
            ' Populate cboType for Assembly documents
            cboType.Items.Clear()
            cboType.Items.AddRange(New String() {"D", "E"})
        End If
    End Sub

 

I made a variable to store the selected type, call the sub in the form load and try to store the selected value on form close so I can use it to put the combobox to the right value on the next form load.

 

     'Variable to store the selected value of combobox 'Type'
    Private SelectedType As String

Private Sub ModelMenu_Load(sender As Object, e As EventArgs) Handles MyBase.Load

            'Populate combobox for type
            Set_Combobox_Type()

            'Check if there is a previously selected type and set it
            If Not String.IsNullOrEmpty(SelectedType) AndAlso cboType.Items.Contains(SelectedType) Then
                cboType.SelectedItem = SelectedType
            End If
End Sub

Private Sub ModelMenu_FormClosed(sender As Object, e As FormClosedEventArgs) Handles Me.FormClosed
    'Save the selected value when the form is closed
    If Not IsNothing(cboType.SelectedItem) Then
        SelectedType = cboType.SelectedItem.ToString()
    End If
End Sub

 

But the value of 'SelectedType' is empty on form load. Is it not possible to use a variable on form load? It's stores values just fine if you call it with a button for example.

Anyone can point me in the right direction?

Thanks in advance!

 

Greets,

Frank

---------------------------------------------------------------------------------------------------------
0 Likes
Accepted solutions (1)
220 Views
2 Replies
Replies (2)
Message 2 of 3

WCrihfield
Mentor
Mentor
Accepted solution

Hi @Frank-Oosterwaal.  This is just an educated guess, but maybe it is not storing the value, because it is not obtaining the value during the Form.FormClosed Event.  That event is fired 'after' the form has closed.  You may need to monitor the Form.FormClosing Event, which fires 'before' the form closes, that way you can obtain the value from the form, before it closes.  One other thing comes to mind...once the form is closed, wouldn't that dispose of the resources being held by the routine that launched it, and in the process, dispose of the value of that variable, if that variable was declared within that Class / routine?

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 3 of 3

Frank-Oosterwaal
Collaborator
Collaborator

Hi Wesley,

 

Yeah see I'm an complete idiot. As soon as I read your comment about the dispose of the recourses I though, yeah well duhhh. Can't believe how I missed that😂.

Thanks allot for your comment, much appreciated. To anyone who runs into this issue, this is the answer:
once the form is closed, wouldn't that dispose of the resources being held by the routine that launched it, and in the process, dispose of the value of that variable, if that variable was declared within that Class / routine?


So you'll need to store it in a different class and call that in your forms class. Works like a charm.

Greets,

Frank

---------------------------------------------------------------------------------------------------------