Reference views by number?

mjtaryan

Active Member
Licensed User
Longtime User
If you want to start or restart the app or activity with all views not visible and disabled (sort of a zero state where each view is turned on as needed) and you want to make sure that is done in code, is there anyway to reference the views that are used by number, such as the tab order? I know that in VS 8 this can be done, although I don't remember offhand the exact syntax. If not, I think it would be a nice feature to have -- make the code shorter at least.
 

nfordbscndrd

Well-Known Member
Licensed User
Longtime User
You can make them all not visible and disabled in Designer, then they will be that way when you start the app and if the app is restarted (as opposed to Resumed).

Otherwise, it depends on your views. In the unlikely even they are all the same type of view, you could make them part of an array and easily control them.

With different types of views, you would have to have an array for each type.

If you are already using the views in other arrays, it appears that you can have another array that contains all views including those which are part of other arrays. I say "appears" because I just tried it and it compiles okay, but I didn't try working with it and I'm on my way out the door.

Maybe one of the Three Wise Men (agraham, klaus, erel) could tell us if that would work.

Anyway, by the time you set up all those arrays, you are coming dangerously close to doing about as much work as if you just hard coded resetting each view in a Sub.
 
Upvote 0

kickaha

Well-Known Member
Licensed User
Longtime User
You call change all views visibility with the following code:
B4X:
   For i = 0 To Activity.NumberOfViews - 1
      Dim v As View
      v = Activity.GetView(i)
      v.Visible = False
   End If

Using that code, what would happen in the case of a panel on the activity that contained views?

Would each view have its visibility set, or would it just be the parent panel?
 
Upvote 0

rbsoft

Active Member
Licensed User
Longtime User
You call change all views visibility with the following code:
B4X:
   For i = 0 To Activity.NumberOfViews - 1
      Dim v As View
      v = Activity.GetView(i)
      v.Visible = False
   End If

That is a very handy piece of code!
Would there be a way to limit this to a certain type of view, for instance panels? Something similar like VB's 'TypeOf'?
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Here is a general code that can handle specific type of views and also child views:
B4X:
Sub Activity_Create(FirstTime As Boolean)
   For i = 0 To Activity.NumberOfViews - 1
      SetViewProperty(Activity.GetView(i))
   Next
End Sub
Sub SetViewProperty(v As View)
   If v Is Button Then 'change to other type of views as needed
      Dim b As Button
      b = v
      b.Text = "abc"
   End If
   If v Is Panel Then
      Dim p As Panel
      p = v
      For i = 0 To p.NumberOfViews - 1
         SetViewProperty(p.GetView(i))
      Next
   End If
End Sub
 
Upvote 0

rbsoft

Active Member
Licensed User
Longtime User
Absolutely great! Thanks for that code sample, Erel.

I had been thinking of a function that switches between panels. I had been thinking of something like this:

B4X:
Sub SwitchToPanel(pnl As View)
'Based on Erel's code sample

   For i = 0 To Activity.NumberOfViews - 1
      Dim v As View
      v = Activity.GetView(i)
      If v Is Panel  Then
         If v = pnl Then
            v.Visible = True
         Else
            v.Visible = False
         End If
      End If
   Next
End Sub

That should work, or? Will try it out later.

Rolf

Edit:
Works - There is a little sample attached.
 

Attachments

  • switchtopanel.zip
    5.9 KB · Views: 175
Last edited:
Upvote 0
Top