Clearing a ScrollView Dim'd Local in a Sub

William Hunter

Active Member
Licensed User
Longtime User
Is there a way to clear a ScrollView, when it has been Dim’d Local in a Sub?

Any help greatly appreciated. :sign0163:

Regards
 

William Hunter

Active Member
Licensed User
Longtime User
Clearing a ScrollView Dim'd Local

Why don't you assign it to a global variable?

Thank you Erel for your reply. I know I could assign it to a Global variable, but there are times that I don’t want to Re-Dim a ScrollView on rotate. Things would work if I could assign it as a Process_Globals variable, but you can't access an activity object from Sub Process_Globals.

I can resolve the issue by making a few code changes, but was wondering if there was a simple way to clear a ScrollView Dim’d in a Sub. Now I can stop wondering!

Regards
 
Upvote 0

kickaha

Well-Known Member
Licensed User
Longtime User
You can loop through the views until you find the scrollview (if you have more than one then use the tag property to differentiate), then you can clear it.

Look at Activity.GetView and Activity.NumberOfViews, and indeed the upcoming For Each feature.
 
Upvote 0

William Hunter

Active Member
Licensed User
Longtime User
Clearing a ScrollView Dim'd Local

You can loop through the views until you find the scrollview (if you have more than one then use the tag property to differentiate), then you can clear it.

Look at Activity.GetView and Activity.NumberOfViews, and indeed the upcoming For Each feature.

Thank you kickaha for your reply. Would something like the code below work for me? I take it that the For Each feature will be included in a future release of B4A.

Best regards
B4X:
Sub ClearScrollView
   For i = 0 To Activity.NumberOfViews - 1
       If Activity.GetView(i) Is ScrollView Then
           Activity.GetView(i).RemoveView
      End If
    Next
End Sub
 
Upvote 0

Informatix

Expert
Licensed User
Longtime User
Thank you kickaha for your reply. Would something like the code below work for me? I take it that the For Each feature will be included in a future release of B4A.

Best regards
B4X:
Sub ClearScrollView
   For i = 0 To Activity.NumberOfViews - 1
       If Activity.GetView(i) Is ScrollView Then
           Activity.GetView(i).RemoveView
      End If
    Next
End Sub

You should add an Exit after the RemoveView. Continuing to loop is not necessary once the ScrollView is removed.
And you should always check the views in the reverse order because of "Activity.NumberOfViews - 1".

B4X:
For i = Activity.NumberOfViews - 1 To 0 Step - 1
 
Upvote 0

kickaha

Well-Known Member
Licensed User
Longtime User
The code looks ine.

As stated above there is no need to continue the loop once you have removed the view, so you coould add a RETURN after the RemoveView.

The reason you would loop in reverse is that when you delete a view all the higher numbered views are shifted down one and your loop exit condition is now set at more than the number of views.
 
Upvote 0

Informatix

Expert
Licensed User
Longtime User
The code looks ine.

As stated above there is no need to continue the loop once you have removed the view, so you coould add a RETURN after the RemoveView.

The reason you would loop in reverse is that when you delete a view all the higher numbered views are shifted down one and your loop exit condition is now set at more than the number of views.

Thank you for taking the time to explain my brief advice. I'm a bit short on time these days.
 
Upvote 0

William Hunter

Active Member
Licensed User
Longtime User
Clearing a ScrollView Dim'd Local

@ kickaha & Informatix - Thank you both very much for the informative explanations. Your suggested code below works very well.

Best regards
B4X:
Sub ClearScrollView
   For i = Activity.NumberOfViews - 1 To 0 Step - 1
      If Activity.GetView(i) Is ScrollView Then
         Activity.GetView(i).RemoveView
         Return
      End If
   Next
End Sub
 
Upvote 0
Top