Tuesday, 7 October 2014

Making of Purge Variables for Solid Edge - Part 4

...continued from Part 3

Another important function of the Purge button is to also delete the names from the list box so it appears in sync with the Solid Edge document's Variable Table:

For this a reverse For loop is used as below which checks each item in the List box for being selected and removes it from the list.

For i As Integer = lstVars.Items.Count - 1 To 0 Step -1
  If lstVars.GetSelected(i) Then
    lstVars.Items.RemoveAt(i)
  End If
Next

The reason for using the For loop in reverse manner is important to understand.

Consider a list of 5 items to be removed from a list box. Each item's index within the list is shown in brackets.

India (0)

Russia (1)

America (2)

Canada (3)

Australia (4)

When i = 0, RemoveAt(0) can find India and removes it.

The list now appears with new indices as below:

Russia (0)

America (1)

Canada (2)

Australia (3)

Next i = 1, so the item with index 1 is sought:

RemoveAt(1) seeks America in the list above and removes it, so the remaining list with the new indices is:

Russia (0)

Canada (1)

Australia (2)

Next, i = 2 and RemoveAt(2) seeks the third item Australia and also removes it.

The list is now as below and i = 3 seeks the item at an index which does not exist and the loop and function fails.

Russia (0)

Canada (1)

Running the For loop in the reverse manner solves this problem as below.

Consider the original list:

India (0)

Russia (1)

America (2)

Canada (3)

Australia (4)

The reverse For loop starts at index 4 down to 1:

At i = 4, Australia goes leaving the list as below:

India (0)

Russia (1)

America (2)

Canada (3)

Next, at i = 3, Canada is removed from the list which looks like:

India (0)

Russia (1)

America (2)

Further at, i = 2 America still exits and so on till i = 0 when India is finally removed from the list. Thus the loop does not fail.

The functioning of the Select All, Invert Selection and Clear all button follows in the next concluding part along with the program file and source files downloads.

Continued to Part 5...

Index of all Solid Edge Tutorials, Tips, Videos...