TheSwamp
Code Red => .NET => Topic started by: 57gmc on April 29, 2010, 11:32:54 AM
-
I had created the function below. It was working, but now, when you run it, it finds the cui and executes the RemovePartialMenu method, and the count decrements, but the cui doesn't actually get removed. The next time you run it, PartialCuiFiles.Count is back to what it was and the cui is still there. I'm on 2010.
Friend Function UnloadPartialCui(ByVal PartialCuiPath As String) As Boolean
'CuiPath parameter is the fully qualified path of the partial cui to be removed from main cui.
Try
Dim strParentCui As String = Application.GetSystemVariable("MENUNAME") & ".cuix"
Dim oMainCui As CustomizationSection = New CustomizationSection(strParentCui)
For i = 0 To oMainCui.PartialCuiFiles.Count - 1
If oMainCui.PartialCuiFiles.Item(i).ToLower = PartialCuiPath.ToLower Then
oMainCui.RemovePartialMenu(PartialCuiPath)
End If
Next
Catch ex As Autodesk.AutoCAD.Runtime.Exception
Return False
Catch ex As System.IO.FileNotFoundException
Return False
Finally
UnloadPartialCui = True
End Try
End Function
-
This is what I ended up with.
Friend Function UnloadPartialCui(ByVal PartialCuiPath As String) As Boolean
'CuiPath parameter is the fully qualified path of the partial cui to be removed from main cui.
Try
Dim strParentCui As String = Application.GetSystemVariable("MENUNAME") & ".cuix"
Dim oMainCui As CustomizationSection = New CustomizationSection(strParentCui)
For i = 0 To oMainCui.PartialCuiFiles.Count - 1
If oMainCui.PartialCuiFiles.Item(i).ToLower = PartialCuiPath.ToLower Then
oMainCui.RemovePartialMenu(PartialCuiPath)
Return True 'step out of loop, now that it is out of sync with Count.
End If
Next
Catch ex As Autodesk.AutoCAD.Runtime.Exception
Return False
Catch ex As System.IO.FileNotFoundException
Return False
Finally
End Try
End Function
-
A better way is to iterate backwards:
for (int i = somevar.Count - 1; i >=0; i--) {
// some intersting mojo to remove here...
}
Or, if the collection supports it, the generic RemoveAll(predicate goes here) method.