Why is my form remembering?

286
14
Jump to solution
01-22-2014 10:20 AM
Highlighted
Occasional Contributor III
Hey all,

I have an Add-In where the user browses to a FGDB (returns a string of the FGDB path and adds it to a label on the form). I have another label that has the string of a file path, and then 2 combo boxes (choose a layer).

The user is unable to proceed unless all parameters are filled out, so if they navigate to a FGDB, but forget to add the required layers, they close the form, add the data, and re-open the form.

EDIT: "Close the form" = stop the Add-in, add the data, then re-launch the add-in.

However, my form is remembering the file paths (they're also stored in a public string variable).

My question is, what am I doing incorrectly about closing the form and not disposing (?) of all the variables. The combo boxes are cleared, but the variables and the labels which store the paths remain intact.

Here's the code for the form:

Public Class frmProgress      Private Sub btnBrowseGPX_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBrowseGPX.Click         Call Procedures.GetGPXPath()     End Sub      Private Sub btnBrowseGDB_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBrowseGDB.Click         Call Procedures.GetGDBPath()     End Sub      Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click         ' Close the form, but continue with the code in Procedures.         Me.Hide()         bContinue = True     End Sub      Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click         ' Exit the Do loop in Procedures using the continue boolean and close the form.         bContinue = False         Me.Close()     End Sub      Private Sub frmProgress_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing         If e.CloseReason = Windows.Forms.CloseReason.UserClosing Then             bContinue = False             e.Cancel = False         End If     End Sub End Class


The program has a Do loop so that if they attempt to proceed without filling out the parameters, it will get mad, but keep the form open. Thus the reason for the Me.FormClosing handle.

Anyways, I'm going to review some other Add-Ins I have which have similar functionality, I'm sure it's just a small oversight, but if you can think of anything of the bat, that would be greatly appreciated!
Reply
0 Kudos
1 Solution

Accepted Solutions
Highlighted
Regular Contributor
Once you fire the event of your add-in that creates your intance variables, unless you are de-referencing them or resetting them back to their default values at some point along the way, you will get the same values you entered before when the form is re-opened.

You have a button on your form that closes your form and you have a form closing event as well.  In both of those places, you need to set your string variables back to an empty string, or have both of those places call a procedure that does that for you.

View solution in original post

Reply
0 Kudos
14 Replies
Highlighted
Regular Contributor
What does "stop the Add-in" mean?  Are you closing out of ArcMap?
Reply
0 Kudos
Highlighted
Occasional Contributor III
Once they hit OK on the form (assuming all parameters have been filled out), the program does a whole bunch of stuff (irrelevant for now).

If they hit cancel or the 'X' button, the intention is to quit out of the Add-In (don't proceed with aforementioned stuff). ArcMap would remain open in this case. The issue is that, if they click the Add-In again, I want it to be a clean slate. Since it's not, that means my code is somehow doing something it shouldn't be...e.g. remembering variables.
Reply
0 Kudos
Highlighted
Regular Contributor
Once you fire the event of your add-in that creates your intance variables, unless you are de-referencing them or resetting them back to their default values at some point along the way, you will get the same values you entered before when the form is re-opened.

You have a button on your form that closes your form and you have a form closing event as well.  In both of those places, you need to set your string variables back to an empty string, or have both of those places call a procedure that does that for you.

View solution in original post

Reply
0 Kudos
Highlighted
Occasional Contributor III
Thanks for the helpful reply.

That makes some sense to me. That behaviour wasn't intentional, but I don't see it as being bad (at least in this particular case)...is that bad practice that the variables are remaining as is?

I have an additional question as well. When the form closes (either cancel or 'X') it should be setting bContinue = False. Shouldn't this exiting of the main Subroutine call the garbage collection and dereference any variables? Mainly wondering if I should "fix" the fact that the variables aren't being dereferenced.

Thanks again!

Here's the code that the Add-In button click runs, if it helps make sense of my problem:

        Do
            ' Declare and initialize variables.
            Dim pUID As IUID
            Dim pLayer As ILayer
            Dim pEnumLayer As IEnumLayer
            Dim pMxDoc As IMxDocument
            Dim pMap As IMap

            pMxDoc = My.ArcMap.Document
            pMap = pMxDoc.FocusMap

            ' Set UID to retrieve IFeatureLayer which can be QI'd to ILayer.
            pUID = New UID
            pUID.Value = "{40A9E885-5533-11d0-98BE-00805F7CED21}"

            ' Get list of all feature layers in the map. True means recursive so it will return all feature layers in the table of contents.
            ' Set iterator to the front of the list.
            pEnumLayer = pMap.Layers(pUID, True)
            pEnumLayer.Reset()

            ' Get first layer in list, and QI to ILayer (to make use of the .Name property).
            pLayer = pEnumLayer.Next

            ' Clear the combo boxes in case the user has already attempted to proceed without filling all parameters out.
            frmProgress.cbTWPLayer.Items.Clear()
            frmProgress.cbLSDLayer.Items.Clear()

            ' Iterate through layers to populate combo box list.
            Do Until pLayer Is Nothing
                frmProgress.cbLSDLayer.Items.Add(pLayer.Name)
                frmProgress.cbTWPLayer.Items.Add(pLayer.Name)
                pLayer = pEnumLayer.Next()
            Loop

            ' Display the form.
            frmProgress.ShowDialog()

            ' OK and Cancel buttons determine whether or not to proceed.
            If Not Procedures.bContinue Then
                Exit Sub
            Else
                ' Store information from combo boxes.
                sLSD = frmProgress.cbLSDLayer.Text
                sTWP = frmProgress.cbTWPLayer.Text

                If sGPXPathPublic = "" Or sGDBPathPublic = "" Or sLSD = "" Or sTWP = "" Then
                    MsgBox("You must fill out all parameters.")
                    Continue Do
                Else
                    Call Procedures.Main(frmProgress)
                    Exit Sub
                End If
            End If
        Loop
Reply
0 Kudos
Highlighted
Regular Contributor
Thanks for the helpful reply.
That makes some sense to me. That behaviour wasn't intentional, but I don't see it as being bad (at least in this particular case)...is that bad practice that the variables are remaining as is?

Bad practice? It's your application, only you know what the flow is supposed to do.


I have an additional question as well. When the form closes (either cancel or 'X') it should be setting bContinue = False. Shouldn't this exiting of the main Subroutine call the garbage collection and dereference any variables? Mainly wondering if I should "fix" the fact that the variables aren't being dereferenced.


I'm not sure I understand why you have an outer do loop.  You are not looping.
Reply
0 Kudos
Highlighted
Occasional Contributor III
Fair answer, in this case I don't think it's a bad thing. In the bigger picture, because it wasn't intended, I'm not 100% happy with it, but that's ok for now. At least I've been given a good explanation.

As for the Do Loop, it's purpose is to prevent the form from closing when the user hits 'OK' without having filled out the required parameters.

Without it, they're warned that they didn't fill everything out, but because the OK button hides the form (with the expectation that it will proceed with the rest of the code), they have to re-run the Add-In. It's not required, but I like the idea that if they forget 1 thing, they don't have to go back and re-do everything.

Thus the reason as well to handle form closing event. If they hit cancel or 'X' I know they don't want to continue, but the code doesn't unless I tell it.

My knowledge is rather limited, so I may be misunderstanding things, but that's the reason I have those in place.
Reply
0 Kudos
Highlighted
Regular Contributor
Fair answer, in this case I don't think it's a bad thing. In the bigger picture, because it wasn't intended, I'm not 100% happy with it, but that's ok for now. At least I've been given a good explanation.

It's fine with me.  I have certain forms that leave the position of sliders where they were so the user can see where they left off.


As for the Do Loop, it's purpose is to prevent the form from closing when the user hits 'OK' without having filled out the required parameters.

I hear what you are saying, but you don't need a do loop for that.


Without it, they're warned that they didn't fill everything out, but because the OK button hides the form (with the expectation that it will proceed with the rest of the code), they have to re-run the Add-In. It's not required, but I like the idea that if they forget 1 thing, they don't have to go back and re-do everything.

Thus the reason as well to handle form closing event. If they hit cancel or 'X' I know they don't want to continue, but the code doesn't unless I tell it.

Would you be willing to post a screen shot of your form?
Reply
0 Kudos
Highlighted
Occasional Contributor III
Interesting...I haven't found out how to accomplish what I want without a Do Loop. I'll have to look into that.

Absolutely, screen shot is attached.

Standard form, nothing too fancy (one hidden label to be used later in the code, and 2 empty labels for the paths).

Let me know if you want to see how it looks with data (though I'm sure you can imagine), or if you want any other info!
Reply
0 Kudos
Highlighted
Regular Contributor
which components are required to be completed before the form closes?
Reply
0 Kudos